Ansible Documentation
include_role – Load and execute a role機能
- pre_tasks, tasks, post_tasks 内から role を取り込む
- include_role に指定したディレクティブは include_role 自体に適用され、role 内の各タスクに適用されない
- role 内の handlers は include_role が実行された後で play 内からアクセス可能になる
パラメータ
| パラメータ | 選択肢/ Default | 説明 |
|---|---|---|
| apply | - | tags や become などのディレクティブを role 内の各タスクに継承する |
| defaults_from | "main" | role の defaults/ ディレクトリから読み込むファイル名 |
| handlers_from | "main" | role の handlers/ ディレクトリから読み込むファイル名 |
| name | - | role 名 |
| public | ・ no・ yes | yes を指定すると include_tasks モジュールの実行後も role 内の vars/ ディレクトリおよび defaults/ ディレクトリで定義した変数にアクセス可能にする |
| tasks_from | "main" | role の tasks/ ディレクトリから読み込むファイル名 |
| vars_from | "main" | role の vars/ ディレクトリから読み込むファイル名 |
例
■ 例 1tree 構造です。
.
├── hosts.yml
├── roles
│ └── sample
│ ├── handlers
│ │ └── main.yml
│ ├── tasks
│ │ └── main.yml
│ └── vars
│ └── main.yml
└── site.yml
site.yml の内容です。
---
- hosts: all
tasks:
- name: sample role を include
include_role:
name: sample
public: yes
when: ansible_facts['distribution'] == "CentOS"
- name: shell モジュールで changed を発生させる
shell: ls
notify: "role_handler"
- name: role 内の変数を参照
debug:
var: foo
roles/sample/tasks/main.yml の内容です。
---
- name: sample role の文字列1
debug:
msg: "Sample Role 1"
- name: sample role の文字列2
debug:
msg: "Sample Role 2"
roles/sample/handlers/main.yml の内容です。
---
- name: handlers の処理
debug:
msg: "role 内の handlers が実行されました"
listen: "role_handler"
roles/sample/vars/main.yml の内容です。
--- foo: bar実行結果です。
- when ディレクティブの条件式は include_role モジュールに適用されています。タスク「sample role を include」で node-u1804 は skipping され、タスク内の各タスクは node-c0706 だけに実行されています。
- タスク「shell モジュールで changed を発生させる」の notify は tasks: 内のすべてのタスクの実行が終了した後に role 内のの handlers で実行しています。
- public: yes を指定しているので include_role の実行後のタスク「role 内の変数を参照」で vars/ ディレクトリで定義した変数が参照できています。
ansibleman@ubuntu-pc:~/ansible/include_role$ ansible-playbook -i hosts.yml site.yml
PLAY [all] ****************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************
ok: [node-c0706]
ok: [node-u1804]
TASK [sample role を include] *********************************************************************************************************
*
skipping: [node-u1804]
TASK [sample : sample role の文字列1] *************************************************************************************************
****
ok: [node-c0706] => {
"msg": "Sample Role 1"
}
TASK [sample : sample role の文字列2] *************************************************************************************************
****
ok: [node-c0706] => {
"msg": "Sample Role 2"
}
TASK [shell モジュールで changed を発生させる] ****************************************************************************************
************
changed: [node-c0706]
changed: [node-u1804]
TASK [role 内の変数を参照] ************************************************************************************************************
*******
ok: [node-c0706] => {
"foo": "bar"
}
ok: [node-u1804] => {
"foo": "bar"
}
RUNNING HANDLER [sample : handlers の処理] ********************************************************************************************
***
ok: [node-c0706] => {
"msg": "role 内の handlers が実行されました"
}
ok: [node-u1804] => {
"msg": "role 内の handlers が実行されました"
}
PLAY RECAP ****************************************************************************************************************************
node-c0706 : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node-u1804 : ok=4 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
ansibleman@ubuntu-pc:~/ansible/include_role$
■ 例 2
例 1 の include_role の when ディレクティブの条件が不成立の場合の例です。
---
- hosts: all
tasks:
- name: sample role を include
include_role:
name: sample
public: yes
when: ansible_facts['distribution'] == "Cent"
- name: shell モジュールで changed を発生させる
shell: ls
notify: "role_handler"
- name: role 内の変数を参照
debug:
var: foo
実行結果です。include_role に付けた when 条件が不成立になった結果、include_role が実行されず、指定した role が読み込まれませんでした。その結果、タスク「shell モジュールで changed を発生させる」の notify を受ける handler が存在しなくなり、エラーが発生しました。
ansibleman@ubuntu-pc:~/ansible/include_role$ ansible-playbook -i hosts.yml site.yml
PLAY [all] ****************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************
ok: [node-c0706]
ok: [node-u1804]
TASK [sample role を include] *********************************************************************************************************
*
skipping: [node-c0706]
skipping: [node-u1804]
TASK [shell モジュールで changed を発生させる] ****************************************************************************************
************
ERROR! The requested handler 'role_handler' was not found in either the main handlers list nor in the listening handlers list
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/home/ansibleman/.local/lib/python3.6/site-packages/ansible/plugins/strategy/__init__.py", line 86, in results_thread_main
result = strategy._final_q.get()
File "/usr/lib/python3.6/multiprocessing/queues.py", line 94, in get
res = self._recv_bytes()
File "/usr/lib/python3.6/multiprocessing/connection.py", line 216, in recv_bytes
buf = self._recv_bytes(maxlength)
File "/usr/lib/python3.6/multiprocessing/connection.py", line 411, in _recv_bytes
return self._recv(size)
File "/usr/lib/python3.6/multiprocessing/connection.py", line 379, in _recv
chunk = read(handle, remaining)
TypeError: an integer is required (got type NoneType)
ansibleman@ubuntu-pc:~/ansible/include_role$
■ 例 3
tree 構造です。
.
├── hosts.yml
├── roles
│ ├── CentOS
│ │ └── tasks
│ │ └── main.yml
│ └── Ubuntu
│ └── tasks
│ └── main.yml
└── site.yml
site.yml の内容です。
---
- hosts: all
tasks:
- name: ディストリビューションごとに処理を切り替え
include_role:
name: "{{ ansible_facts['distribution'] }}"
roles/CentOS/tasks/main.yml の内容です。
---
- name: CentOS のときの処理
debug:
msg: "CentOS"
roles/Ubuntu/tasks/main.yml の内容です。
---
- name: Ubuntu のときの処理
debug:
msg: "Ubuntu"
実行結果です。node-c0706 が CentOS 7.6 、node-u1804 が Ubuntu 18.04 です。
ansibleman@ubuntu-pc:~/ansible/include_role$ ansible-playbook -i hosts.yml site.yml
PLAY [all] ****************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************
ok: [node-c0706]
ok: [node-u1804]
TASK [ディストリビューションごとに処理を切り替え] **********************************************************************************************************
TASK [CentOS : CentOS のときの処理] *********************************************************************************************************
ok: [node-c0706] => {
"msg": "CentOS"
}
TASK [Ubuntu : Ubuntu のときの処理] *********************************************************************************************************
ok: [node-u1804] => {
"msg": "Ubuntu"
}
PLAY RECAP ****************************************************************************************************************************
node-c0706 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node-u1804 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansibleman@ubuntu-pc:~/ansible/include_role$