Ansible group_vars未根据环境/主机列表加载 [英] Ansible group_vars not loading based on environment / host list
问题描述
我有这样的剧本:
- hosts: "{{env}}"
name: "REDIS Playbook"
sudo: no
vars:
product: redis
roles:
- redis
我用ansible-playbook pb_redis.yml -i inventory/redis -e env=qa -v
我有一个类似的目录结构:
I have a directory structure like:
.
├── group_vars
│ ├── qa
│ │ ├── common
│ │ | └── redis.yml
│ │ ├── products
│ │ | └── abc-1.yml
│ │ | └── xyz-2.yml
│ ├── test
│ │ ├── common
│ │ | └── redis.yml
│ │ ├── products
│ │ | └── abc-1.yml
│ │ | └── xyz-2.yml
├── inventory
└── roles
└── redis
├── files
├── handlers
├── meta
├── tasks
├── templates
└── vars
我有一个类似的清单:
[qa:children]
qa_redis
[qa_redis]
mybox.1.space
mybox.2.space
mybox.3.space
我的问题:运行ansible-playbook pb_redis.yml -i inventory/redis -e env=qa -v
时,我仍然会使用../test/common/redis.yml
中定义的group_vars
而不是../qa/common/redis.yml
-我误解了它应该如何工作?正确的 hosts 是从清单文件中拾取的,而不是正确的 group_vars .我应该将redis.yml
放在../qa/products/
下吗?
My Issue: When I run ansible-playbook pb_redis.yml -i inventory/redis -e env=qa -v
, I'm still picking up group_vars
defined in the ../test/common/redis.yml
instead of ../qa/common/redis.yml
-- am I misunderstanding how this should work? The correct hosts get picked up from the inventory file, but not the correct group_vars. Should I place the redis.yml
under ../qa/products/
instead?
推荐答案
Ansible中的库存(主机和组)变量绑定到主机.存在组变量是为了方便.
Inventory (host and group) variables in Ansible are bound to host. Group variables exist for convenience.
如果主机同时位于多个组中,则所有组变量都将应用于该主机.
如果不同的组具有相同的变量,则在库存加载过程中它们会相互覆盖.
If a host is in multiple groups at the same time, all group variables are applied to that host.
If different groups have same variables, they overwrite each other during inventory load process.
因此,如果组qa
和test
中有mybox.1.space
,则组qa
和test
中的变量将应用于此主机.
So if you have mybox.1.space
in groups qa
and test
, variables from groups qa
and test
are applied to this host.
通常,您要使用单独的清单来与不同的部署环境一起使用.组用于分隔清单中的不同逻辑单元.
Usually you want to use separate inventories to work with different deploy environments. And groups are used to separate different logical units inside inventory.
这篇关于Ansible group_vars未根据环境/主机列表加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!