host in multiple groups, no control over which group_var get's loaded / overwritten.

Dear List,

We have a program running under different acounts on one server. (Sometimes two server share the load of 1 environment)
Each program/account combination has it’s own settings, IP ports, etc.

This part is all fine and we want to take care of those settings using ansible.
So we came up with this:

Each environment needs it’s own settings, so:

“/etc/ansible/hosts”


[env1]
taixkaren

[env2]
taixkaren
taixkitty
[env3]
taixkaren
[env4]
taixkaren
[env5]
taixkaren
peibapril

And then some group_vars files to set the individual environment settings:

“/etc/ansible/group_vars/env1”


program:
setting1: epplus
setting2: 8080

“/etc/ansible/group_vars/env2”


program:
setting1: userepenv
setting2: 86735

eib:
sets: “4,6,02”

etc for the other groups.

The main playbook looks like this:


  • hosts: $omgeving
    sudo: true
    vars_files:
  • /etc/ansible/group_vars/$omgeving

tasks:

  • include: $basedir/europort/ep_stop_start/tasks/finttpci_stop.yml
    when: program is defined
  • include: $basedir/europort/ep_stop_start/tasks/fintexch_stop.yml
    when: eib is defined
  • include: $basedir/europort/ep_stop_start/tasks/fintbpo_stop.yml

Do you notice that “- /etc/ansible/group_vars/$omgeving” line? remember it…

To start this playbook, I do this:
ansible-playbook europort/europort.yml --extra-vars “omgeving=env2

So far so good.

Now let’s try two environments:
ansible-playbook europort/europort.yml --extra-vars “omgeving=env2:env4

This goes wrong because ansible tries to import “/etc/ansible/group_vars/env2:env4” which ofcourse does not exist.
But when I do not put in that line “/etc/ansible/group_vars/$omgeving” it may actually load the settings for env3 in memory instead of env2 even though I only wanted to target env2!

Are we going about this the wrong way entirely?

Thoughts much appreciated!

The problem is this:

"
vars_files:

  • /etc/ansible/group_vars/$omgeving

"

Ansible already has a system for keeping group variables with inventory, this is the “group_vars” directory that lives inside your inventory.

It is imported automatically by the inventory system, so you don’t have to do any explicit imports.

In the above example, you are explicitly importing it again, it so seems, but also passing a non-existent filename to it, because it’s not letting the inventory just do it’s thing.

The solution here it to just remove the ‘vars_files’ import of this variable file and inventory will do it for you.

–Michael

Aha! Yes it got you too!

You see, when I delete that line, one does not know which group_var file actually get’s loaded. I think they all get loaded because the server is in all of them and overwrite each other’s variables with the same name!

So in this case, I tried to run the playbooks against the env2 environment, but I might end up getting the env4 variables instead!

Mark

Yes, all inventory groups get loaded (by design).

In which case you should just put your variables in a file and load them explicitly, but you won’t be able to just pass in the host pattern like you are doing because you don’t have a file named after that host pattern.

Ok, so creating a combo of the group and hostname using group_by would solve it for this use case…