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