include_tasks について
include_tasks を使用することで、タスク内容を記載した外部ファイルを Playbook へ読み込んでそのファイルに記載されいているタスクを実行することができます。
include_tasks を使用するメリットとして以下のような点があります。
- あらかじめタスクファイルを作成しておくことで Playbook 作成を効率化できる
- Playbook の内容を処理の流れだけを記載したコンパクトな内容にできる
主なパラメータ
パラメータ | 必須 | 選択肢/デフォルト | 説明 |
file | ● | 読み込み対象のタスクファイルのパス | |
apply | (調査中) |
file のみ指定する場合は「include_tasks: file path」の形式で記載することができます。
ファイルパスの指定は相対パスと絶対パスが使用できます。
相対パスの場合は Playbook 格納ディレクトリが基準となります。
使用例
作業環境
- CentOS 8.0
- Python 3.7.7
- Ansible 2.9.6
(1) 最も単純な例
- debug モジュールでメッセージを出力するタスクファイル message.yml を読み込み実行する
■ message.yml
- name: Output message
debug:
msg: message from message.yml
■ playbook.yml
- hosts: all
tasks:
- name: include_tasks test
include_tasks: message.yml
■ Ansible 実行結果
# ansible-playbook -i hosts playbook.yml
PLAY [all] **********************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************
ok: [localhost]
TASK [include_tasks test] *******************************************************************************************
included: /root/message.yml for localhost
TASK [Output message] ***********************************************************************************************
ok: [localhost] => {
"msg": "message from message.yml"
}
PLAY RECAP **********************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
(2) 一連の処理を記載したタスクファイル
意味を持った一連の処理を記載したタスクファイルを作成しておくと便利です。
- Reboot を実施するタスクファイル reboot.yml を読み込み実行する
■ reboot.yml
- name: Execute reboot
shell:
cmd: 'sleep 2 && reboot'
async: 1
poll: 0
- name: Wait for SSH port down
become: no
wait_for:
host: '{{ inventory_hostname }}'
port: '{{ ansible_ssh_port }}'
state: stopped
delegate_to: 127.0.0.1
- name: Wait for SSH port up
become: no
wait_for:
host: '{{ inventory_hostname }}'
port: '{{ ansible_ssh_port }}'
state: started
delegate_to: 127.0.0.1
■ playbook.yml
- hosts: all
become: yes
tasks:
- name: Execute reboot
include_tasks: reboot.yml
- name: message
debug:
msg: 'Finish!'
■ Ansible 実行結果
# ansible-playbook -i hosts playbook.yml
PLAY [all] **********************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************
ok: [10.69.34.191]
TASK [Execute reboot] ***********************************************************************************************
included: /root/reboot.yml for 10.69.34.191
TASK [Execute reboot] ***********************************************************************************************
changed: [10.69.34.191]
TASK [Wait for SSH port down] ***************************************************************************************
ok: [10.69.34.191 -> 127.0.0.1]
TASK [Wait for SSH port up] *****************************************************************************************
ok: [10.69.34.191 -> 127.0.0.1]
TASK [message] ******************************************************************************************************
ok: [10.69.34.191] => {
"msg": "Finish!"
}
PLAY RECAP **********************************************************************************************************
10.69.34.191 : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
(3) タスクファイルで変数を使用する
タスクファイルで変数を使用することができます。
- 使用例(1)の message.yml のメッセージ内容を変数にする
■ message.yml
- name: Output message
debug:
msg: '{{ item }}'
変数値を with_items で与える前提で変数名を item としています。
■ playbook.yml
- hosts: all
tasks:
- name: Vars in task_file
include_tasks: message.yml
with_items:
- 'message1'
- 'message2'
with_items でタスクファイルにパラメータを与えると、各パラメータ毎にタスクファイルが読み込まれて順次実行されます。
■ Ansible 実行結果
# ansible-playbook -i hosts playbook.yml
PLAY [all] **********************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************
ok: [10.69.34.191]
TASK [Vars in task_file] ********************************************************************************************
included: /root/message.yml for 10.69.34.191
included: /root/message.yml for 10.69.34.191
TASK [Output message] ***********************************************************************************************
ok: [10.69.34.191] => {
"msg": "message1"
}
TASK [Output message] ***********************************************************************************************
ok: [10.69.34.191] => {
"msg": "message2"
}
PLAY RECAP **********************************************************************************************************
10.69.34.191 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
―――――――――――――