!!!参考 https://employment.en-japan.com/engineerhub/entry/2019/04/12/103000 https://qiita.com/346/items/00122556cb2bd6f57998 https://dev.classmethod.jp/server-side/ansible/introduction_about_role/ https://tekunabe.hatenablog.jp/entry/2018/06/11/ansible_inventory_list https://docs.ansible.com/ansible/2.3/intro_inventory.html#list-of-behavioral-inventory-parameters https://docs.ansible.com/ansible/latest/index.html https://github.com/ansible/ansible-examples !!!概要 構成管理ツールの一つ。設定先にエージェントを必要としないエージェントレスな構成ツール。 !!!公式ドキュメント https://docs.ansible.com/ansible/latest/index.html !!!使い方 !!ディレクトリ構成 . ├── inventory ├── playbook.yml └── roles └── helloworld └── tasks └── main.yml !inventory 構成先の情報を記載する。 指摘できるパラメータは [公式ドキュメント|https://docs.ansible.com/ansible/2.3/intro_inventory.html#list-of-behavioral-inventory-parameters] を参照する [hoge] 192.168.1.10 [hoge2] # 設定するときに使うsshのユーザを指定するときは ansible_ssh_user を指定する 192.168.1.20 ansible_ssh_user=hoge [hoge3] # パスワードも指定できる 192.168.1.30 ansible_ssh_user=hoge3 ansible_ssh_pass=centos [hoge4] # 鍵ファイルを指定する場合 192.168.1.30 ansible_ssh_user=hoge4 ansible_ssh_private_key_file=/home/hoge4/.ssh/hoge4 !playbook.yml 呼び出すロールを指定する。 - hosts: all roles: - helloworld 条件に合わせて実行するロールを切り替えることもできる --- - import_playbook: common.yml - import_playbook: centos.yml when: - ansible_distribution == "CentOS" - import_playbook: ubuntu.yml when: - ansible_distribution == "Ubuntu" - hosts: all roles: - clamav !roleディレクトリ 実際に実行する処理を書く。 ansible-galaxy init roles/helloworld でディレクトリ構成を自動的に作成してくれる。作成後に不要なディレクトリを消しても良い。 tasksディレクトリがあれば動く !main.yml 実際の処理を書く --- # tasks file for helloworld - name: Hello World! debug: msg: "Hello World!" !実行 ansible-playbook -i inventory playbook.yml inventoryのグループを指定する場合 ansible-playbook -i inventory playbook.yml -l centos,ubuntu !!設定先ホストの情報(変数) ansible -i inventory -m setup all で接続先の情報を取得できる。この値を参照したり条件分岐に使える。 インベントリファイルを用意しなくても、ホスト名(IPアドレス)を指定することもできる。 ansible -m setup -i 192.168.38.148, -u ubuntu -k all :-i:インベントリファイルの代わりにIPアドレス等を指定する。カンマ区切りで複数指定できるが、一つだけの場合でもカンマが必要。 :-u:ユーザを指定 :-k:パスワードの入力を求める :all:本来はインベントリファイルの中からパターンにマッチした接続先だけを表示するのだが、IPを指定する場合はallで良い !!サンプル {{ref ansible_sample.zip}} {{category2 OS,Linux}}