无法设置和使用hostvars,在host1中声明了一个var,我想在localhost中使用它。我遵循了许多建议,但没有一个能解决错误The task includes an option with an undefined variable. The error was: "hostvars['host1']" is undefined
步骤为
- host1使用
register
通过fetch
将文件复制到本地 - 然后使用
set_fact
将目标路径存储为包含主机("dest":"/var/log/assible_runs/10.xxx.xxx.251/tmp/xxxx_env_pin.tmp"(。这在host1下工作 - 下一个localhost使用var";路径文件";从host1创建(工作时将使用路径附加到另一个文件(
# 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 }}"