【问题标题】:Ansible playbook, adhoc var not able to use between two hostAnsible playbook,临时变量无法在两台主机之间使用
【发布时间】:2020-07-14 19:17:05
【问题描述】:

无法设置和使用 hostvar,在 host1 中声明了一个我想在 localhost 中使用的 var。我遵循了许多建议,但没有一个可以解决错误 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

【问题讨论】:

  • 请不要编辑答案来添加信息,而是发表评论或编辑您的问题。回答您的错误编辑:它不起作用,因为我在那里输入错误。当我的意思是 hostvars 时,我确实写了 hostsvars (在主机上添加复数)。编辑了我的答案,现在应该对你有用。

标签: ansible ansible-facts


【解决方案1】:

鉴于您的回顾,特别是那些行 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 }}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 2020-09-07
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多