Loading all group_vars content even if only one is needed

25 views
Skip to first unread message

0lai.m...@gmail.com

unread,
Feb 2, 2018, 7:31:19 AM2/2/18
to Ansible Project

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.


Kai Stian Olstad

unread,
Feb 2, 2018, 8:46:05 AM2/2/18
to ansible...@googlegroups.com
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.


--
Kai Stian Olstad

0lai.m...@gmail.com

unread,
Feb 5, 2018, 8:19:44 AM2/5/18
to Ansible Project
I see. That explains it then. Thanks!

0lai.m...@gmail.com

unread,
May 18, 2018, 9:02:41 AM5/18/18
to Ansible Project
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 <full path>/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?

Zicheng Wang

unread,
May 18, 2018, 12:15:19 PM5/18/18
to ansible...@googlegroups.com
Could you try 

```
 ansible-playbook doTemplates.yml -i <full path>/inventories/prod/hosts -l b_server

```


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c3043b4d-ff29-4bd6-88ef-0c1bb5c9ff23%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages