Ansible : Playbook を利用する (変数)2024/09/11 |
Ansible Playbook では変数も利用できます。 |
|
[1] | 変数の利用例です。[packages] に値を格納し、{{ packages }} で参照しています。 |
freebsd@dlp:~ $
vi playbook_sample.yml - hosts: target_servers become: yes become_method: sudo tasks: - name: General packages are installed pkgng: name: "{{ packages }}" state: present vars: packages: - curl - wget - unzip tags: General_Packages ansible-playbook playbook_sample.yml --ask-become-pass BECOME password: PLAY [target_servers] *********************************************************************** TASK [Gathering Facts] ********************************************************************** ok: [10.0.0.52] ok: [10.0.0.51] TASK [General packages are installed] ******************************************************* 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 # 確認 freebsd@dlp:~ $ ansible target_servers -m shell -a "which curl; which wget; which unzip;" 10.0.0.52 | CHANGED | rc=0 >> /usr/local/bin/curl /usr/local/bin/wget /usr/bin/unzip 10.0.0.51 | CHANGED | rc=0 >> /usr/local/bin/curl /usr/local/bin/wget /usr/bin/unzip |
[2] | Playbook を実行すると、最初に [GATHERING FACTS] タスクが必ず実行されます。 これは対象ホストの情報を収集している処理で、ここで収集された情報は変数に格納されているため、参照して利用することができます。 なお、[GATHERING FACTS] で収集している情報と同じ情報は [setup] モジュールで確認できます。 |
# GATHERING FACTS の情報は setup モジュールで確認可能 freebsd@dlp:~ $ ansible 10.0.0.51 -m setup 10.0.0.51 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "10.0.0.51" ], "ansible_all_ipv6_addresses": [ "fe80::5054:ff:fec6:38d1%vtnet0" ], "ansible_apparmor": { "status": "disabled" }, "ansible_architecture": "amd64", "ansible_bios_date": "NA", "ansible_bios_vendor": "NA", "ansible_bios_version": "NA", "ansible_board_asset_tag": "NA", ..... ..... # GATHERING FACTS 情報の参照例 freebsd@dlp:~ $ vi playbook_sample.yml # [ansible_distribution], [ansible_distribution_version] を参照 - hosts: target_servers tasks: - name: Refer to Gathering Facts command: echo "{{ ansible_distribution }} {{ ansible_distribution_version }}" register: dist - debug: msg="{{ dist.stdout }}" ansible-playbook playbook_sample.yml PLAY [target_servers] ******************************************************************* TASK [Gathering Facts] ****************************************************************** ok: [10.0.0.52] ok: [10.0.0.51] TASK [Refer to Gathering Facts] ********************************************************* changed: [10.0.0.52] changed: [10.0.0.51] TASK [debug] **************************************************************************** ok: [10.0.0.51] => { "msg": "FreeBSD 14.1" } ok: [10.0.0.52] => { "msg": "FreeBSD 14.1" } PLAY RECAP ****************************************************************************** 10.0.0.51 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 10.0.0.52 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
Sponsored Link |