JaneD
JaneD

Reputation: 199

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

Upvotes: 0

Views: 286

Answers (1)

wbh1
wbh1

Reputation: 407

I think your problem is two-fold.

  1. Ansible applies the group_vars of a directory to all files and subdirectories within the specified inventory directory. So, inventory/production/group_vars will get applied to everything within inventory/production. This just gets masked when you explicitly limit your inventory further while running, like you did (-i inventory/production/prodinv).
    1. This means that you need to put the group_vars only being applied to prodinv in their own directory and not in the inventory/production directory. For example, inventory/production/prodinv/group_vars.
  2. Your symlinks are set up in a way that if you run against inventory, you're going to have the same group_vars applied to all your inventories. You're not hitting this in your example, but you'll likely hit it in the future.

Upvotes: 0

Related Questions