1

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

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

为什么?

4

1 回答 1

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
于 2017-09-12T11:39:15.513 回答