Chapter 15

条件 : when

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

説明

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

演習問題

Q1. web グループに含まれる管理対象ノードの OS やバージョンを考慮して Apache をインストールするプレイブックを作成してください。

  • OS ファミリー、バージョン、使用するモジュール、インストールするパッケージの組み合わせ

    OS ファミリー バージョン モジュール パッケージ
    RedHat 7 ansible.builtin.yum httpd
    RedHat 8 ansible.builtin.dnf httpd
    Debian - ansible.builtin.apt apache2
  • すべてのタスクは管理権限が必要です。

  • OS ファミリーは「変数の参照方法」の「演習問題」を参照してください。

  • OS のメジャーバージョンは「ファクト変数」の「演習問題 Q4 」 を参照してください。

  • Apache をインストール後、サービスの起動が必要です。サービスを起動するタスクです ( OS ファミリーなどの判断は含まれていません ) 。

    • RedHat 系
      - name: Start httpd service.
        ansible.builtin.systemd:
          name: httpd.service
          state: started
      
    • Debian 系
      - name: Start apache2 service.
        ansible.builtin.systemd:
          name: apache2.service
          state: started
      
  • インベントリーファイルの内容

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

冪等性を確認

y_mrok@ctrl:~/code/chap15$ ansible-playbook -i hosts.yml install_apache.yml 

PLAY [Identify the OS and install Apache.] ***********************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************
ok: [takeyamachi]
ok: [oshikoji]
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: [oshikoji]
skipping: [takeyamachi]
skipping: [nijyo]
skipping: [ebisugawa]
skipping: [oike]
ok: [marutamachi]

TASK [Start httpd service.] **************************************************************************************************************************
skipping: [oshikoji]
skipping: [ebisugawa]
skipping: [oike]
skipping: [nijyo]
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/chap15$ 

Q2. Apache のテストページを表示してください。

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