説明
演習問題
Q. invnetory_hostname ごとのロールを呼び出すプレイブック「 check_hostname.yml 」を作成してください。
- inventory_hostname がロール名です。
- インベントリーファイルに記載したすべての管理対象ノードが対象です。
- 「 tasks/main.yml 」ファイルに inventory_hostname を表示するタスクを記述します。
- インベントリーファイルの内容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/all.yml--- ansible_user: vagrant ansible_password: vagrant
host_vas/marutamachi.yml--- ansible_host: 192.168.111.101
host_vas/takeyamachi.yml--- ansible_host: 192.168.111.102
host_vas/ebisugawa.yml--- ansible_host: 192.168.111.103
host_vas/nijyo.yml--- ansible_host: 192.168.111.104
host_vas/oshikoji.yml--- ansible_host: 192.168.111.105 ansible_python_interpreter: /usr/bin/python3
host_vas/oike.yml--- ansible_host: 192.168.111.106 ansible_python_interpreter: /usr/libexec/platform-python
解答
- ディレクトリー「 host_vars/ 」と「 group_vars/ 」の内容 / 構成は変更しません(そのまま使用します)。
check_hostname.yml
---
- name: Switch the role to be invoked for each inventory_hostname.
hosts: all
gather_facts: no
tasks:
- name: Call the role.
ansible.builtin.include_role:
name: "{{ inventory_hostname }}"
roles/marutamachi/tasks/main.yml
---
- name: Displays the inventory_hostname of the managed node marutamachi.
ansible.builtin.debug:
var: inventory_hostname
roles/takeyamachi/tasks/main.yml
---
- name: Displays the inventory_hostname of the managed node takeyamachi.
ansible.builtin.debug:
var: inventory_hostname
roles/ebisugawa/tasks/main.yml
---
- name: Displays the inventory_hostname of the managed node ebisugawa.
ansible.builtin.debug:
var: inventory_hostname
roles/nijyo/tasks/main.yml
---
- name: Displays the inventory_hostname of the managed node nijyo.
ansible.builtin.debug:
var: inventory_hostname
roles/oshikoji/tasks/main.yml
---
- name: Displays the inventory_hostname of the managed node oshikoji.
ansible.builtin.debug:
var: inventory_hostname
roles/oike/tasks/main.yml
---
- name: Displays the inventory_hostname of the managed node oike.
ansible.builtin.debug:
var: inventory_hostname
y_mrok@ctrl:~/code/chap23$ ansible-playbook -i hosts.yml check_hostname.yml
PLAY [Switch the role to be invoked for each inventory_hostname.] ************************************************************************************
TASK [Call the role.] ********************************************************************************************************************************
TASK [marutamachi : Displays the inventory_hostname of the managed node marutamachi.] ****************************************************************
ok: [marutamachi] => {
"inventory_hostname": "marutamachi"
}
TASK [takeyamachi : Displays the inventory_hostname of the managed node takeyamachi.] ****************************************************************
ok: [takeyamachi] => {
"inventory_hostname": "takeyamachi"
}
TASK [oshikoji : Displays the inventory_hostname of the managed node oshikoji.] **********************************************************************
ok: [oshikoji] => {
"inventory_hostname": "oshikoji"
}
TASK [nijyo : Displays the inventory_hostname of the managed node nijyo.] ****************************************************************************
ok: [nijyo] => {
"inventory_hostname": "nijyo"
}
TASK [ebisugawa : Displays the inventory_hostname of the managed node ebisugawa.] ********************************************************************
ok: [ebisugawa] => {
"inventory_hostname": "ebisugawa"
}
TASK [oike : Displays the inventory_hostname of the managed node oike.] ******************************************************************************
ok: [oike] => {
"inventory_hostname": "oike"
}
PLAY RECAP *******************************************************************************************************************************************
ebisugawa : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
marutamachi : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
nijyo : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
oike : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
oshikoji : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
takeyamachi : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
y_mrok@ctrl:~/code/chap23$