以外と調べても無かった(見つけられなかった)hostを跨いで設定した変数を利用する方法。
他には一時ファイルに書き出すなど方法もあるようですが簡易版。
localhostで設定した変数を、delegate_to
でremotehostを指定すると参照できていますが、
そのままhosts: remotehostとすると参照できずに失敗する。
※最後のtaskがその失敗の様子。
---
- name: localhost-> get now time
hosts: localhost
tasks:
- name: get now time
set_fact:
now_time: "{{ lookup('pipe','date +%Y%m%d_%H%M%S') }}"
- debug:
msg: "{{ now_time }}"
- name: localhost-> debug msg
hosts: localhost
tasks:
- debug:
msg: "{{ now_time }}"
- name: localhost-> remotehost-> debug msg
hosts: localhost
tasks:
- debug:
msg: "{{ now_time }}"
delegate_to: test_remote_host
delegate_facts: true
- name: remotehost-> debug msg
hosts: all
tasks:
- debug:
msg: "{{ now_time }}"
- 結果
PLAY [localhost-> get now time] **********************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [get now time] **********************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] *****************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "20240625_031911"
}
PLAY [localhost-> debug msg] *************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] *****************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "20240625_031911"
}
PLAY [localhost-> remotehost-> debug msg] ************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] *****************************************************************************************************************************************************************************************************************************************************
ok: [localhost -> yokoy-workstation-vm2] => {
"msg": "20240625_031911"
}
PLAY [remotehost-> debug msg] ************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************
ok: [test_remote_host]
TASK [debug] *****************************************************************************************************************************************************************************************************************************************************
fatal: [test_remote_host]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'now_time' is undefined\n\nThe error appears to be in 'xxx.yml': line 29, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - debug:\n ^ here\n"}
PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************
localhost : ok=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test_remote_host : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0