Chapter 17

タスクをグルーピング : block

y_mrok
y_mrok
2021.10.24に更新
このチャプターの目次

説明

この章の説明はこのリンクをクリックしてください。

演習問題

Q1. block ディレクティブを使用してタスクを RedHat 系と Debian 系にまとめてください。

  • インベントリーファイルの内容
    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: RedHat OS family.
      block:
        - 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
      when:
        - inventory_hostname in groups['web']
        - ansible_facts['os_family'] == "RedHat"

    - name: Debian OS family.
      block:
        - name: Debian family.
          ansible.builtin.apt:
            name: apache2
            state: present
        - name: Start apache2 service.
          ansible.builtin.systemd:
            name: apache2.service
            state: started
      when:
        - inventory_hostname in groups['web']
        - ansible_facts['os_family'] == "Debian"
y_mrok@ctrl:~/code/chap17$ 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/chap17$ 

冪等性を確認

y_mrok@ctrl:~/code/chap17$ 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/chap17$ 

Q2. それぞれの管理対象ノード上で Apache が動作していることを確認するため、ブラウザーでテストページを表示してください。

解答
  • 管理対象ノード : marutamachi
http://192.168.111.101/
  • 管理対象ノード : takeyamachi
http://192.168.111.102/
  • 管理対象ノード : oshikoji
http://192.168.111.105/