Ansible 剧本,临时变量无法在两个主机之间使用
Ansible playbook, adhoc var not able to use between two host
无法设置和使用 hostvars,在 host1 中声明了一个我想在 localhost 中使用的 var。我采纳了很多建议,none 帮我解决了错误
The task includes an option with an undefined variable. The error was: "hostvars['host1']" is undefined
步骤是
- host1通过
fetch
复制一个文件到本地,使用register
- 然后使用
set_fact
将目标路径存储为包含主机(“dest”:“/var/log/ansible_runs/10.xxx.xxx.251/tmp/xxx_env_pin.tmp”)。这在 host1 下工作
- 下一个 localhost 使用从 host1 创建的 var "file_n_path"(工作时将使用路径附加到另一个文件)。
# Run script on remote machine
---
- hosts: host1
remote_user: one
tasks:
- name: Store file into local
fetch:
src: /tmp/xxx_env_one.tmp
dest: /var/log/ansible_runs
register: fetch_output1
- set_fact: file_n_path="{{fetch_output1.dest}}"
- debug:
var: fetch_output1
- debug:
msg: " 1a. {{ file_n_path }}"
# --/ Run on local machine to append copied file
- hosts: localhost
connection: local
vars:
m_var_frm_pin: "{{ hostvars['host1']['file_n_path'] }}"
tasks:
- debug:
msg: " 2a. {{ m_var_frm_pin }}"
输出
PLAY [host1] *******************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts gather_subset=['all'], gather_timeout=10] **************************************************************************************************************************************************************************
ok: [10.xxx.xxx.251]
TASK [Store file intolocal dest=/var/log/ansible_runs, src=/tmp/xxx_env_pin.tmp] *******************************************************************************************************************
ok: [10.xxx.xxx.251]
TASK [set_fact file_n_path={{fetch_output1.dest}}] ***************************************************************************************************************************************************************************************
ok: [10.xxx.xxx.251]
TASK [debug var=fetch_output1] ***********************************************************************************************************************************************************************************************************
ok: [10.xxx.xxx.251] => {
"fetch_output1": {
"changed": false,
"checksum": "dc828a5f0c48456c72e5849891736135f89b265c",
"dest": "/var/log/ansible_runs/10.xxx.xxx.251/tmp/xxx_env_pin.tmp",
"failed": false,
"file": "/tmp/xxx_env_pin.tmp",
"md5sum": "30a6d5ba55ed78832a978c53298a054c"
}
}
TASK [debug msg= 1a. {{ file_n_path }}] **************************************************************************************************************************************************************************************************
ok: [10.xxx.xxx.251] => {}
MSG:
1a. /var/log/ansible_runs/10.xxx.xxx.251/tmp/xxx_env_pin.tmp
PLAY [localhost] *************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts gather_subset=['all'], gather_timeout=10] **************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug msg= 2a. {{ m_var_frm_pin }}] ************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {}
MSG:
The task includes an option with an undefined variable. The error was: "hostvars['host1']" is undefined
The error appears to be in '/etc/ansible/Playbook_033a.yml': line 26, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- debug:
^ here
PLAY RECAP *******************************************************************************************************************************************************************************************************************************
10.xxx.xxx.251 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
鉴于您的回顾,尤其是那些行 ok: [10.xxx.xxx.251]
,您的问题似乎是 host1
不是一个主机而是一组主机。
如果想通过组名访问组中单个主机的hostvars
,可以使用hostvars[groups['group_name'][0]]['var_name']
.
所以你本地的戏份应该是:
- hosts: localhost
connection: local
vars:
m_var_frm_pin: "{{ hostvars[groups['host1'][0]]['file_n_path'] }}"
tasks:
- debug:
msg: " 2a. {{ m_var_frm_pin }}"
无法设置和使用 hostvars,在 host1 中声明了一个我想在 localhost 中使用的 var。我采纳了很多建议,none 帮我解决了错误
The task includes an option with an undefined variable. The error was: "hostvars['host1']" is undefined
步骤是
- host1通过
fetch
复制一个文件到本地,使用register
- 然后使用
set_fact
将目标路径存储为包含主机(“dest”:“/var/log/ansible_runs/10.xxx.xxx.251/tmp/xxx_env_pin.tmp”)。这在 host1 下工作
- 下一个 localhost 使用从 host1 创建的 var "file_n_path"(工作时将使用路径附加到另一个文件)。
# Run script on remote machine
---
- hosts: host1
remote_user: one
tasks:
- name: Store file into local
fetch:
src: /tmp/xxx_env_one.tmp
dest: /var/log/ansible_runs
register: fetch_output1
- set_fact: file_n_path="{{fetch_output1.dest}}"
- debug:
var: fetch_output1
- debug:
msg: " 1a. {{ file_n_path }}"
# --/ Run on local machine to append copied file
- hosts: localhost
connection: local
vars:
m_var_frm_pin: "{{ hostvars['host1']['file_n_path'] }}"
tasks:
- debug:
msg: " 2a. {{ m_var_frm_pin }}"
输出
PLAY [host1] *******************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts gather_subset=['all'], gather_timeout=10] **************************************************************************************************************************************************************************
ok: [10.xxx.xxx.251]
TASK [Store file intolocal dest=/var/log/ansible_runs, src=/tmp/xxx_env_pin.tmp] *******************************************************************************************************************
ok: [10.xxx.xxx.251]
TASK [set_fact file_n_path={{fetch_output1.dest}}] ***************************************************************************************************************************************************************************************
ok: [10.xxx.xxx.251]
TASK [debug var=fetch_output1] ***********************************************************************************************************************************************************************************************************
ok: [10.xxx.xxx.251] => {
"fetch_output1": {
"changed": false,
"checksum": "dc828a5f0c48456c72e5849891736135f89b265c",
"dest": "/var/log/ansible_runs/10.xxx.xxx.251/tmp/xxx_env_pin.tmp",
"failed": false,
"file": "/tmp/xxx_env_pin.tmp",
"md5sum": "30a6d5ba55ed78832a978c53298a054c"
}
}
TASK [debug msg= 1a. {{ file_n_path }}] **************************************************************************************************************************************************************************************************
ok: [10.xxx.xxx.251] => {}
MSG:
1a. /var/log/ansible_runs/10.xxx.xxx.251/tmp/xxx_env_pin.tmp
PLAY [localhost] *************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts gather_subset=['all'], gather_timeout=10] **************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug msg= 2a. {{ m_var_frm_pin }}] ************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {}
MSG:
The task includes an option with an undefined variable. The error was: "hostvars['host1']" is undefined
The error appears to be in '/etc/ansible/Playbook_033a.yml': line 26, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- debug:
^ here
PLAY RECAP *******************************************************************************************************************************************************************************************************************************
10.xxx.xxx.251 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
鉴于您的回顾,尤其是那些行 ok: [10.xxx.xxx.251]
,您的问题似乎是 host1
不是一个主机而是一组主机。
如果想通过组名访问组中单个主机的hostvars
,可以使用hostvars[groups['group_name'][0]]['var_name']
.
所以你本地的戏份应该是:
- hosts: localhost
connection: local
vars:
m_var_frm_pin: "{{ hostvars[groups['host1'][0]]['file_n_path'] }}"
tasks:
- debug:
msg: " 2a. {{ m_var_frm_pin }}"