説明
演習問題
Q1. プレイブックの RedHat 系用のタスクと Debian 系用のタスクを ansible.builtin.import_tasks
モジュールを使用して取り込むよう書き直してください。
- 処理手順
- RedHat 系の部分を「 radhat_apache.yml 」ファイルに書き出す。
- Deabian 系の部分を「 debian_apache.yml 」ファイルに書き出す。
- 書き出したタスクから次の条件を削除する。
- web グループかどうか判断する条件
- RedHat 系かどうか判断する条件 / debian 系かどうか判断する条件
-
ansible.builtin.import_tasks
モジュールを使用し、書き出したそれぞれのタスクを取り込む -
ansible.builtin.import_tasks
モジュールを使用したそれぞれのタスクにwhen
ディレクティブで必要な条件を追加する
- インベントリーファイルの内容hosts.yml
--- all: children: web: hosts: marutamachi: # CentOS/8.3 (RedHat) takeyamachi: # CentOS/7.8 (RedHat) oshikoji: # Debian10 (Debian) mail: hosts: nijyo: ebisugawa: database: hosts: oshikoji: oike:
group_vars/--- ansible_user: vagrant ansible_password: vagrant
group_vars/web.yml--- http_port: 8080
host_vars/marutamachi.yml--- ansible_host: 192.168.111.101
host_vars/takeyamachi.yml--- ansible_host: 192.168.111.102
host_vars/ebisugawa.yml--- ansible_host: 192.168.111.103
host_vars/nijyo.yml--- ansible_host: 192.168.111.104
host_vars/oshikoji.yml--- ansible_host: 192.168.111.105 ansible_python_interpreter: /usr/bin/python3
host_vars/oike.yml--- ansible_host: 192.168.111.106 ansible_python_interpreter: /usr/libexec/platform-python
- 変更対象のプレイブックinstall_apache.yml
--- - name: Identify the OS and install Apache. hosts: all become: yes tasks: - name: RedHat family with major version 7. ansible.builtin.yum: name: httpd state: present when: - inventory_hostname in groups['web'] - ansible_facts['os_family'] == "RedHat" - ansible_facts['distribution_major_version'] == "7" - name: RedHat family with major version 8. ansible.builtin.dnf: name: httpd state: present when: - inventory_hostname in groups['web'] - ansible_facts['os_family'] == "RedHat" - ansible_facts['distribution_major_version'] == "8" - name: Start httpd service. ansible.builtin.systemd: name: httpd.service state: started when: - inventory_hostname in groups['web'] - ansible_facts['os_family'] == "RedHat" - name: Debian family. ansible.builtin.apt: name: apache2 state: present when: - inventory_hostname in groups['web'] - ansible_facts['os_family'] == "Debian" - name: Start apache2 service. ansible.builtin.systemd: name: apache2.service state: started when: - inventory_hostname in groups['web'] - ansible_facts['os_family'] == "Debian"
解答
install_apache.yml
---
- name: Identify the OS and install Apache.
hosts: all
become: yes
tasks:
- name: Install Apache on a RedHat-based managed node.
ansible.builtin.import_tasks:
file: ./redhat_apache.yml
when:
- inventory_hostname in groups['web']
- ansible_facts['os_family'] == "RedHat"
- name: Install Apache on a Debian-based managed node.
ansible.builtin.import_tasks:
file: ./debian_apache.yml
when:
- inventory_hostname in groups['web']
- ansible_facts['os_family'] == "Debian"
redhat_apache.yml
---
- name: RedHat family with major version 7.
ansible.builtin.yum:
name: httpd
state: present
when:
- ansible_facts['distribution_major_version'] == "7"
- name: RedHat family with major version 8.
ansible.builtin.dnf:
name: httpd
state: present
when:
- ansible_facts['distribution_major_version'] == "8"
- name: Start httpd service.
ansible.builtin.systemd:
name: httpd.service
state: started
debian_apache.yml
---
- name: Debian family.
ansible.builtin.apt:
name: apache2
state: present
- name: Start apache2 service.
ansible.builtin.systemd:
name: apache2.service
state: started
y_mrok@ctrl:~/code/chap18$ ansible-playbook -i hosts.yml install_apache.yml
PLAY [Identify the OS and install Apache.] ***********************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************
ok: [oshikoji]
ok: [takeyamachi]
ok: [marutamachi]
ok: [nijyo]
ok: [ebisugawa]
ok: [oike]
TASK [RedHat family with major version 7.] ***********************************************************************************************************
skipping: [marutamachi]
skipping: [oshikoji]
skipping: [nijyo]
skipping: [ebisugawa]
skipping: [oike]
changed: [takeyamachi]
TASK [RedHat family with major version 8.] ***********************************************************************************************************
skipping: [takeyamachi]
skipping: [oshikoji]
skipping: [nijyo]
skipping: [ebisugawa]
skipping: [oike]
changed: [marutamachi]
TASK [Start httpd service.] **************************************************************************************************************************
skipping: [oshikoji]
skipping: [nijyo]
skipping: [ebisugawa]
skipping: [oike]
changed: [takeyamachi]
changed: [marutamachi]
TASK [Debian family.] ********************************************************************************************************************************
skipping: [marutamachi]
skipping: [takeyamachi]
skipping: [nijyo]
skipping: [ebisugawa]
skipping: [oike]
changed: [oshikoji]
TASK [Start apache2 service.] ************************************************************************************************************************
skipping: [marutamachi]
skipping: [takeyamachi]
skipping: [nijyo]
skipping: [ebisugawa]
skipping: [oike]
ok: [oshikoji]
PLAY RECAP *******************************************************************************************************************************************
ebisugawa : ok=1 changed=0 unreachable=0 failed=0 skipped=5 rescued=0 ignored=0
marutamachi : ok=3 changed=2 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
nijyo : ok=1 changed=0 unreachable=0 failed=0 skipped=5 rescued=0 ignored=0
oike : ok=1 changed=0 unreachable=0 failed=0 skipped=5 rescued=0 ignored=0
oshikoji : ok=3 changed=1 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
takeyamachi : ok=3 changed=2 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
y_mrok@ctrl:~/code/chap18$
冪等性を確認
y_mrok@ctrl:~/code/chap18$ ansible-playbook -i hosts.yml install_apache.yml
PLAY [Identify the OS and install Apache.] ***********************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************
ok: [oshikoji]
ok: [takeyamachi]
ok: [marutamachi]
ok: [nijyo]
ok: [ebisugawa]
ok: [oike]
TASK [RedHat family with major version 7.] ***********************************************************************************************************
skipping: [marutamachi]
skipping: [oshikoji]
skipping: [nijyo]
skipping: [ebisugawa]
skipping: [oike]
ok: [takeyamachi]
TASK [RedHat family with major version 8.] ***********************************************************************************************************
skipping: [takeyamachi]
skipping: [oshikoji]
skipping: [nijyo]
skipping: [ebisugawa]
skipping: [oike]
ok: [marutamachi]
TASK [Start httpd service.] **************************************************************************************************************************
skipping: [oshikoji]
skipping: [nijyo]
skipping: [ebisugawa]
skipping: [oike]
ok: [takeyamachi]
ok: [marutamachi]
TASK [Debian family.] ********************************************************************************************************************************
skipping: [marutamachi]
skipping: [takeyamachi]
skipping: [nijyo]
skipping: [ebisugawa]
skipping: [oike]
ok: [oshikoji]
TASK [Start apache2 service.] ************************************************************************************************************************
skipping: [marutamachi]
skipping: [takeyamachi]
skipping: [nijyo]
skipping: [ebisugawa]
skipping: [oike]
ok: [oshikoji]
PLAY RECAP *******************************************************************************************************************************************
ebisugawa : ok=1 changed=0 unreachable=0 failed=0 skipped=5 rescued=0 ignored=0
marutamachi : ok=3 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
nijyo : ok=1 changed=0 unreachable=0 failed=0 skipped=5 rescued=0 ignored=0
oike : ok=1 changed=0 unreachable=0 failed=0 skipped=5 rescued=0 ignored=0
oshikoji : ok=3 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
takeyamachi : ok=3 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
y_mrok@ctrl:~/code/chap18$
Q2. それぞれの管理対象ノード上で Apache が動作していることを確認するため、ブラウザーでテストページを表示してください。
解答
- 管理対象ノード : marutamachi
http://192.168.111.101/
- 管理対象ノード : takeyamachi
http://192.168.111.102/
- 管理対象ノード : oshikoji
http://192.168.111.105/