【问题标题】:Variable from another host in ansibleansible中来自另一个主机的变量
【发布时间】:2018-12-12 14:56:31
【问题描述】:

我不明白为什么找不到来自 localhost 的域变量,即使将完整的 hostvars 变量转储到另一个主机中也是如此。

这是完整的剧本:

---
- hosts: localhost
  connection: local
  vars:
    - domain: foo.com
  tasks:
    - debug:
        var: domain
- hosts: "{{ host_ip }}"
  vars:
    - domain: "{{ hostvars.localhost.domain }}"
  tasks:
  - copy:
      content: "{{ hostvars }}"
      dest: facts.json
  - synchronize:
      mode: pull
      src: "./facts.json"
      dest: "../facts.json"
  - debug:
      var: hostvars.localhost.domain
  - debug:
      var: domain

我可以理解它们是否隐藏在另一个主机或类似的东西下,但域“foo.com”根本不在主机变量中。我也尝试过使用 var:hostvars['localhost']['domain'] 但这没有区别

运行时会产生以下输出:

$ ansible-playbook -v -u docker --private-key '/home/mikael/.ssh/aws' -i '1.2.3.4,' -e ansible_python_interpreter=/usr/bin/python3 /home/mikael/workspace/playbooks/vartest.yml --extra-vars 'host_ip=1.2.3.4'
Using /etc/ansible/ansible.cfg as config file

PLAY [localhost] ****************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************
ok: [localhost]

TASK [debug] ********************************************************************************************************************************************************************
ok: [localhost] => {
    "domain": "foo.com"
}

PLAY [18.202.25.29] *************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************
ok: [18.202.25.29]

TASK [copy] *******************************************************************************************************************************************************************************************************************
changed: [18.202.25.29]

TASK [synchronize] ************************************************************************************************************************************************************************************************************
changed: [18.202.25.29]

TASK [debug] ********************************************************************************************************************************************************************
ok: [18.202.25.29] => {
    "hostvars.localhost.domain": "VARIABLE IS NOT DEFINED!: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'domain'"
}

TASK [debug] ********************************************************************************************************************************************************************
ok: [18.202.25.29] => {
    "domain": "VARIABLE IS NOT DEFINED!: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'domain'"
}

PLAY RECAP **********************************************************************************************************************************************************************
18.202.25.29               : ok=5    changed=2    unreachable=0    failed=0   
localhost                  : ok=2    changed=0    unreachable=0    failed=0

【问题讨论】:

    标签: ansible


    【解决方案1】:

    “域”变量仅在其指定的剧本范围内持续存在。这只是使该变量可用于该剧本中的任务,但不会将其添加到该剧本中每个主机的事实集合中.

    而是向 localhost play 添加一个任务,该任务将一个事实添加到 localhost 的事实集合中,这将在 play 之外持续存在:

    - name: Set domain variable
      set_fact:
        domain: foo.com
    

    【讨论】:

    • 我认为注册变量和播放变量的处理方式相同,但如果我使用注册它就被设置为事实?
    • 我可能误解了您的评论,但我认为我们在谈论不同的目的。注册变量是为存储另一个模块的输出而创建的变量。事实是关于每个系统发现的变量(或使用 set_fact 命令显式设置)并在播放之间持续存在。至于一般的变量范围,文档详细说明了这一点:docs.ansible.com/ansible/latest/user_guide/…
    • 啊,是的,这就是我的意思。做事,注册为 foo。感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    • 2018-08-03
    • 2016-12-30
    相关资源
    最近更新 更多