LoginSignup
5
9

More than 5 years have passed since last update.

[Ansible] Ansible使用方法まとめ

Last updated at Posted at 2016-12-28

サーバーの構成管理の自動化をshellからAnsibleに移行するためのメモ(更新中)

検証環境

$ ansible --version
ansible 2.3.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]

公式ドキュメント

Ansible Documentation — Ansible Documentation

YAMLの文法

YAML Syntax — Ansible Documentation

公式の実装サンプル

ansible/ansible-examples: A few starter examples of ansible playbooks, to show features and how they work together. See http://galaxy.ansible.com for example roles from the Ansible community for deploying many popular applications.

インストール

パッケージでインストールする

Ubuntuでは以下のようにしてインストールして下さい。

Ubuntuでのインストール方法
$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible

その他の環境での方法は以下を参照して下さい。

Installation — Ansible Documentation

Vagrantで使用する(ansible_local)

Vagrantのansible_localでの使用例
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.box_check_update = true

  # Run Ansible from the Vagrant VM
  config.vm.provision "ansible_local" do |ansible|
    ansible.playbook = "playbook.yml"
    ansible.verbose = true
    ansible.install = true
  end
end

Ansible Local - Provisioning - Vagrant by HashiCorp

ansible.install = true で、ゲストOS側にAnsibleがインストールされます。(デフォルトでtrue)

proxyの解決には前段でpipの設定をするか vagrant-proxyconfを使用して下さい。

関連:
[Vagrant][Windows] WindowsでVagrantを使用してVM環境を作成する - Qiita

gitignore

gitignore/Ansible.gitignore at master · github/gitignore

実行方法

localhostに対して実行

[Ansible] AnsibleのPlaybookをlocalhostに対して実行する - Qiita

Playbook構成パターン

Ansible inventoryパターン | Developers.IO

変数

Ansible固有変数

[Ansible] ansible_*変数の値を確認するには - Qiita

ファイル操作

Files Modules — Ansible Documentation
Ansibleでよく使うファイル操作モジュール | Developers.IO

ファイル内容の書き換え

Ansibleでファイルの行を書き換える3つの方法 - Qiita

lineinfile : 行書き換え

lineinfile - Ensure a particular line is in a file, or replace an existing line using a back-referenced regular expression. — Ansible Documentation

blockinfile : 複数行書き換え

blockinfile - Insert/update/remove a text block surrounded by marker lines. — Ansible Documentation

template : Jinja2テンプレートからファイル生成

template - Templates a file out to a remote server. — Ansible Documentation
Template Designer Documentation — Jinja2 Documentation (2.9)

Ansibleの正規表現内でエスケープが必要な文字

【保存版】正規表現でエスケープが必要な文字一覧表 - Qiita

コマンド

shellの実行

shell - Execute commands in nodes. — Ansible Documentation
Ansibleでシェルコマンドを実行させるときのノウハウ - Qiita

プロンプト

※注意: Vagrantのansible_localではプロンプトは使用できない

プロンプトから変数の設定

Prompts — Ansible Documentation

パッケージ

パッケージのインストール

apt

[Ansible][apt] Ansibleでのapt設定方法 - Qiita

System関連

[Linux][Ansible] timezoneの設定 - Qiita
[Linux][Ansible] localeの設定 (ja_JP.UTF-8) に設定 - Qiita

タスクの実行条件を指定

Conditionals — Ansible Documentation

ファイルが存在しているときのみ実行

ansible でファイルがある場合のみ実行したい場合のベストプラクティス - Qiita

パラメータでタスクを繰り返し実行

with_fileglobなどを使ってファイルが存在しているときのみ実行も可能
Loops — Ansible Documentation

デバッグ

デバッグメッセージの出力

debug - Print statements during execution — Ansible Documentation

設定例

postgresql

ansibleでpostgresqlインストール - Qiita

参考

Vagrant + Ansible で開発環境を作るなら ansible_local プロビジョナがいい! - Shin x Blog
Ansibleでのパスワードの取り扱い - Qiita
Ansibleの冪等性とPlaybook | Developers.IO

5
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
9