Ansible group_vars not loading based on environmen

2019-04-16 06:02发布

I have a playbook like so:

- hosts: "{{env}}"
  name: "REDIS Playbook"
  sudo: no
  vars:
    product: redis
  roles:
    - redis

And I call it with: 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

And I have an inventory like:

[qa:children]
qa_redis

[qa_redis]
mybox.1.space
mybox.2.space
mybox.3.space

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
1条回答
Viruses.
2楼-- · 2019-04-16 06:31

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.

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.

查看更多
登录 后发表回答