# Are groups in host\_vars the same groups as those in inventory

**URL:** <https://forum.ansible.com/t/are-groups-in-host-vars-the-same-groups-as-those-in-inventory/29409>\
**Category:** Ansible Project\
**Created:** [January 9, 2019, 5:54pm UTC](https://forum.ansible.com/t/are-groups-in-host-vars-the-same-groups-as-those-in-inventory/29409 "2019-01-09T17:54:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyril.stoll1](https://avatars.discourse-cdn.com/v4/letter/c/f07891/32.png) [@cyril.stoll1](https://forum.ansible.com/u/cyril.stoll1)\
**Post date:** [January 9, 2019, 5:54pm UTC](https://forum.ansible.com/t/are-groups-in-host-vars-the-same-groups-as-those-in-inventory/29409/1 "2019-01-09T17:54:58Z")

</div>

Hi there

The short version:  
Are the group-names defined in the inventory file the same group-names as in a file below the host\_vars folder or can I define separate groups that have nothing to do with each other? This is not clear from reading the documentation [1] but the group names they used in the documentation suggest that.

The long version:

Intro)  
I have 3 types of servers (web-servers, application-servers, db-servers) and each of them 3 times for different environments (production, testing, development). As all the servers per type have basically the same config I created 3 groups in my inventory file and 3 different roles, one per group.  
Now the servers in the same environment (e.g. web-testing, application-testing and db-testing) need to access the same NFS share. So each web-server accesses a different NFS share based on the environment it is in. To mount those NFS shares I need a variable based on the environment and not based on the group the servers are in. The only thing that marks a servers environment is its hostname. That is way I want to define variables based on hosts or their hostnames.

Idea1)  
So far I understand that I could just define that in my inventory inside the groups I already defined. That would look like this

[web]  
web-testing.domain.tld server\_env\_var: test-nfs  
web-production.domain.tld server\_env\_var: prod-nfs  
web-development.domain.tld server\_env\_var: dev-nfs

[db]  
db-testing.domain.tld server\_env\_var: test-nfs  
db-production.domain.tld server\_env\_var: prod-nfs  
etc.

Idea2)  
Or I could use the host\_vars folder inside my inventory and create a file for each server including just one variable definition.

Problem)  
Actually I have way more than 3 types of servers and more than 3 different environments. So both solutions turn out to become quite ugly in my opinion. I would prefer having one single file inside the inventory/host\_vars folder or inside the role/vars folder defining those server\_env\_vars.

Thus my Idea3)  
Can I create groups based on environments in my inventory/host\_vars/ files? For example in a file called lotsofservers.yml have the following groups that do not match the groups in the inventory file:  
[testing]  
web-testing.domain.tld server\_env\_vars: test-nfs  
app-testing.domain.tld server\_env\_vars: test-nfs  
db-testing.domain.tld server\_env\_vars: test-nfs

[production]  
web-production.domain.tld server\_env\_vars: prod-nfs  
etc.

Or Idea4)  
When using the role/vars folder and putting a main.yml in there, how do I define the variables based on my hosts? Couldn’t find that in the documentation…

Thanks for any help and best regards,  
Cyril

[1] [https://docs.ansible.com/ansible/latest/user\_guide/intro\_inventory.html#host-variables](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#host-variables)
