'Ansible - variable for a specific inventory
I have a multi environment & multi inventories setup within ansible (2.7.9).
With one of the inventories, I am wanting to set a global variable to be inherited by all the hosts within the inventory. For this purpose I added the variable into that specific inventory (inventory/production/prodinv):
[all:vars]
myvar="True"
And it works fine if I ran ansible against that specific inventory (inventory/production/prodinv). However, if I run ansible against the inventory directory (eg inventory/production) , I noticed that the variable is inherited on all the hosts across all the inventories - which isn't ideal because I only want the hosts within firstenv inventory to have the var defined.
Currently group_vars and host_vars are a symlink (for all the inventories) against a "shared" root group_vars and host_vars.
To add more clarity to my question, below is the structure of my ansible:
.
├── ansible.cfg
├── playbooks/
├── roles/
├── inventory/
│ │
│ ├── group_vars/
| |
| ├── host_vars/
| |
│ ├── tnd/
│ │ ├── group_vars/ -> ../group_vars
│ | ├── host vars/ -> ../host_vars
│ │ └── devinv
│ │
│ ├── production/
│ │ ├── group_vars/ -> ../group_vars
│ | ├── host vars/ -> ../host_vars
│ │ └── prodinv
│
└── . .
I'm not sure how / where to define this var that should apply to all hosts/groups within a particular inventory, without running into the same issue. Ideas?
Thanks, J
Solution 1:[1]
I think your problem is two-fold.
- Ansible applies the
group_varsof a directory to all files and subdirectories within the specified inventory directory. So,inventory/production/group_varswill get applied to everything withininventory/production. This just gets masked when you explicitly limit your inventory further while running, like you did (-i inventory/production/prodinv).- This means that you need to put the
group_varsonly being applied toprodinvin their own directory and not in theinventory/productiondirectory. For example,inventory/production/prodinv/group_vars.
- This means that you need to put the
- Your symlinks are set up in a way that if you run against
inventory, you're going to have the samegroup_varsapplied to all your inventories. You're not hitting this in your example, but you'll likely hit it in the future.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | wbh1 |
