Ansible : Playbook を利用する (基本)2021/06/16 |
Ansible Playbook の基本的な利用方法です。
Playbook は冪等性 (べきとうせい) を確保するための処理をまとめたもので、YAML 形式で記述します。
|
|
[1] | 例として、あるファイルが常に同じ属性で存在するシンプルな Playbook を作成します。 |
[cent@dlp ~]$
vi playbook_sample.yml # 対象ホスト名 または グループ名 - hosts: target_servers tasks: # 任意のタスク名 - name: Test Task # [file] モジュールを利用してファイルの状態を記述 file: path: /home/cent/test.conf state: touch owner: cent group: cent mode: 600 # Playbook 実行 [cent@dlp ~]$ ansible-playbook playbook_sample.yml PLAY [target_servers] ********************************************************** TASK [Gathering Facts] ********************************************************* ok: [10.0.0.51] ok: [10.0.0.52] TASK [Test Task] *************************************************************** changed: [10.0.0.51] changed: [10.0.0.52] PLAY RECAP ********************************************************************* 10.0.0.51 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 10.0.0.52 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 # 確認 [cent@dlp ~]$ ansible target_servers -m command -a "ls -l /home/cent" 10.0.0.52 | CHANGED | rc=0 >> total 0 ---x-wx--T. 1 cent cent 0 Jun 15 21:09 test.conf 10.0.0.51 | CHANGED | rc=0 >> total 0 ---x-wx--T. 1 cent cent 0 Jun 15 21:09 test.conf |
[2] | 例として、Apache httpd がインストールされ、起動した状態とする Playbook です。 |
[cent@dlp ~]$
vi playbook_sample.yml - hosts: target_servers # 他のユーザー権限を利用 (デフォルト : root) become: yes # 他のユーザー権限を利用する方法 become_method: sudo tasks: # タスクの定義 - name: httpd is installed yum: name: httpd state: present - name: httpd is running and enabled service: name: httpd state: started enabled: yes ansible-playbook playbook_sample.yml --ask-become-pass BECOME password: PLAY [target_servers] ********************************************************** TASK [Gathering Facts] ********************************************************* ok: [10.0.0.51] ok: [10.0.0.52] TASK [httpd is installed] ****************************************************** changed: [10.0.0.51] changed: [10.0.0.52] TASK [httpd is running and enabled] ******************************************** changed: [10.0.0.51] changed: [10.0.0.52] PLAY RECAP ********************************************************************* 10.0.0.51 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 10.0.0.52 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 # 確認 [cent@dlp ~]$ ansible target_servers -m shell -a "/bin/systemctl status httpd | head -3" -b --ask-become-pass BECOME password: 10.0.0.52 | CHANGED | rc=0 >> * httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2021-06-15 21:13:59 CDT; 35s ago 10.0.0.51 | CHANGED | rc=0 >> * httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2021-06-15 21:13:59 CDT; 35s ago |
Sponsored Link |
|