Hi. I’m encountering a problem in my deployments which may or may not be a bug.
In my inventories directory, I have the following files –
inventories/prod/group_vars/a_server
inventories/prod/group_vars/b_server
inventories/prod/group_vars/c_server
Where a_server, b_server and c_server groups are defined in inventories/prod/hosts
All three group_vars files have a variable called “databaseURL” but with different values. When I specify the a_server as the target for my playbook run, it uses the “databaseURL” value defined in c_server when processing j2 templates. So it looks like Ansible loads all 3 group_vars files even if I had specified target=a_server. It looks like c_server is the last processed file (assuming it’s done alphabetically) so the value of “databaseURL” there takes precedence over the value of the same variable in a_server.
Is this behavior as designed? That all the content of group_vars is loaded even when you have chosen (by specifying the target) which group_vars file should be used?
Thanks.
A node will get all the variables of all group it is a member of.
So if your have a node1 that is a member of the group a_server, b_server og c_server it will get the variables from all of them.
Equal variable name will be overwritten so the last one is the one that sticks.
This is by design.
I see. That explains it then. Thanks!
I may have misunderstood the above reply.
Example set-up:
inventories/prod/hosts
[a_server]
hostname1
[b_server]
hostname2
[c_server]
hostname3
inventories/prod/group_vars/a_server
databaseURL : URL_for_hostname1s_db
inventories/prod/group_vars/b_server
databaseURL : URL_for_hostname2s_db
inventories/prod/group_vars/c_server
databaseURL : URL_for_hostname3s_db
When I run the playbook → ansible-playbook doTemplates.yml -i /inventories/prod/hosts -e target=b_server
I would expect the file resulting from the j2 to have “URL_for_hostname2s_db” however, it has “URL_for_hostname3s_db”
It looks like Ansible is loading the variables inside a_server, b_server, c_server even though I only specified b_server as the target.
Shouldn’t Ansible load just b_server?
Could you try
ansible-playbook doTemplates.yml -i <full path>/inventories/prod/hosts -l b_server