
Ansible Documentation
import_role – Import a role into a play機能
- play の実行中に role を取り込む
- このモジュールに指定したディレクティブは role 内のタスクに適用される
- import_role で取り込まれるロールの vars/ および defaults/ で定義した変数は import_role が実行される前からアクセス可能になる
- role 内の handlers は import_role が実行される前から play 内からアクセス可能になる
パラメータ
パラメータ | 選択肢/ Default | 説明 |
---|---|---|
defaults_from | "main" | role の defaults/ ディレクトリから読み込むファイル名 |
handlers_from | "main" | role の handlers/ ディレクトリから読み込むファイル名 |
name | - | role 名 |
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: role 内の変数を参照 debug: var: foo - name: shell モジュールで changed を発生させる shell: ls notify: "role_handler" - name: sample role を import1 import_role: name: sample when: ansible_facts['distribution'] == "CentOS"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実行結果です。node-c0706 が CentOS 7.6 、node-u1804 が Ubuntu 18.04 です。
- roll タスク「role 内の変数を参照」で import_roll の実行前に role 内の変数を参照しています。
- 同様に import_roll の実行前にタスク「shell モジュールで changed を発生させる」で notify が発生しており、role 内の handlers が処理しています。
- roll 内の各タスクに import_roll の when ディレクティブが継承されています。そのため、node-u1804 のすべての実行結果が skipping になっています。
- tasks: 内のすべてのタスクの実行が終了してから roll 内の handlers が実行されています。このときも import_roll に指定した when ディレクティブが継承されているので node-u1804 の handlers の処理が skipping になっています。
ansibleman@ubuntu-pc:~/ansible/import-role$ ansible-playbook -i hosts.yml site.yml
PLAY [all] ****************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************
ok: [node-c0706]
ok: [node-u1804]
TASK [role 内の変数を参照] ************************************************************************************************************
*******
ok: [node-c0706] => {
"foo": "bar"
}ok: [node-u1804] => {
"foo": "bar"
}
TASK [shell モジュールで changed を発生させる] ****************************************************************************************
************
changed: [node-c0706]
changed: [node-u1804]
TASK [sample : sample role の文字列1] *************************************************************************************************
****
ok: [node-c0706] => {
"msg": "Sample Role 1"
}
skipping: [node-u1804]
TASK [sample : sample role の文字列2] *************************************************************************************************
****
ok: [node-c0706] => {
"msg": "Sample Role 2"
}
skipping: [node-u1804]
RUNNING HANDLER [sample : handlers の処理] ********************************************************************************************
***
ok: [node-c0706] => {
"msg": "role 内の handlers が実行されました"
}
skipping: [node-u1804]
PLAY RECAP ****************************************************************************************************************************
node-c0706 : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node-u1804 : ok=3 changed=1 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
ansibleman@ubuntu-pc:~/ansible/import-role$
■ 例 2
対象ホストの OS ごとに import する role を切り替える例です。これはエラーになりません。
--- - hosts: all vars: fn1: CentOS fn2: Ubuntu tasks: - name: CentOS のとき import_role: name: "{{ fn1 }}" when: ansible_facts['distribution'] == "CentOS" - name: Ubuntu のとき import_role: name: "{{ fn2 }}" when: ansible_facts['distribution'] == "Ubuntu"これは ERROR! 'ansible_facts' is undefined エラーになります。
--- - hosts: all tasks: - name: ディストリビューションごとに処理を切り替え import_role: name: "{{ ansible_facts['distribution'] }}"