【问题标题】:Ansible doesn't return correct group_vars, why?Ansible 没有返回正确的 group_vars,为什么?
【发布时间】:2017-09-12 11:32:12
【问题描述】:

deploy.yaml

---
- name: test
  hosts: host_a
  tasks:
    - name: debug
      debug:
        var: demo

inventory

[host_a]
localhost

[host_a:vars]
demo=aaa

[host_b]
localhost

[host_b:vars]
demo=bbb

当我运行 playbook 时,我希望变量 demo 返回 aaa,但它返回 bbb

为什么?

【问题讨论】:

    标签: ansible ansible-inventory ansible-facts


    【解决方案1】:

    Ansible 没有返回正确的 group_vars,为什么?

    因为清单中定义的变量被视为事实,并且事实绑定到 Ansible 中的主机。由于您只定义了一个名为 localhost 的主机,因此第一个值将被覆盖。

    确认:

    [host_a]
    127.0.0.1
    
    [host_a:vars]
    demo=aaa
    
    [host_b]
    127.0.0.2
    
    [host_b:vars]
    demo=bbb
    

    [host_a]
    localhost1 ansible_ssh_host=localhost
    
    [host_a:vars]
    demo=aaa
    
    [host_b]
    localhost2 ansible_ssh_host=localhost
    
    [host_b:vars]
    demo=bbb
    

    【讨论】:

    • 除了更改主机,还有其他方法吗?在我的剧本中,主机是按组模式设置的,我认为 group_vars 是按组匹配使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 2016-11-11
    • 1970-01-01
    • 2015-05-09
    相关资源
    最近更新 更多