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
。
为什么?
答案 0 :(得分: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