# Loading all group\_vars content even if only one is needed

**URL:** <https://forum.ansible.com/t/loading-all-group-vars-content-even-if-only-one-is-needed/26641>\
**Category:** Ansible Project\
**Created:** [February 2, 2018, 12:31pm UTC](https://forum.ansible.com/t/loading-all-group-vars-content-even-if-only-one-is-needed/26641 "2018-02-02T12:31:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![0lai.mariano](https://avatars.discourse-cdn.com/v4/letter/0/51bf81/32.png) [@0lai.mariano](https://forum.ansible.com/u/0lai.mariano)\
**Post date:** [February 2, 2018, 12:31pm UTC](https://forum.ansible.com/t/loading-all-group-vars-content-even-if-only-one-is-needed/26641/1 "2018-02-02T12:31:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 2, 2018, 1:45pm UTC](https://forum.ansible.com/t/loading-all-group-vars-content-even-if-only-one-is-needed/26641/2 "2018-02-02T13:45:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![0lai.mariano](https://avatars.discourse-cdn.com/v4/letter/0/51bf81/32.png) [@0lai.mariano](https://forum.ansible.com/u/0lai.mariano)\
**Post date:** [February 5, 2018, 1:19pm UTC](https://forum.ansible.com/t/loading-all-group-vars-content-even-if-only-one-is-needed/26641/3 "2018-02-05T13:19:44Z")

</div>

I see. That explains it then. Thanks!

---

<div class="post-metadata">

**Author:** ![0lai.mariano](https://avatars.discourse-cdn.com/v4/letter/0/51bf81/32.png) [@0lai.mariano](https://forum.ansible.com/u/0lai.mariano)\
**Post date:** [May 18, 2018, 1:02pm UTC](https://forum.ansible.com/t/loading-all-group-vars-content-even-if-only-one-is-needed/26641/4 "2018-05-18T13:02:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Zicheng\_Wang](https://avatars.discourse-cdn.com/v4/letter/z/9fc29f/32.png) [@Zicheng\_Wang](https://forum.ansible.com/u/Zicheng_Wang)\
**Post date:** [May 18, 2018, 4:15pm UTC](https://forum.ansible.com/t/loading-all-group-vars-content-even-if-only-one-is-needed/26641/5 "2018-05-18T16:15:07Z")

</div>

Could you try

```auto

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

```
