Question: How to use the same host for multiple groups?

Hello,

I have the following inventory:

Test.inventory
[GroupA]
Host1
[GroupB]
Host1

GroupA and GroupB contain the same variables but with different values (e.g. filename). It seems that every time the playbook runs for this inventory, it does the steps once for the host (i.e. only for one of the groups).
What I want to achieve, is to run the playbook for Host1 twice (once for each group).

Is there any way to achieve this (maybe via roles)? Am I misusing the groups module?

The Ansible version I am using is 2.3.

Best Regards,
Konstantinos

There are many ways,

simplest might be using host aliases, but this might be inefficient:

[GroupA]
host1A ansible_host=hosta

[GroupB]
host1A ansible_host=hosta

Setting up the variables at play/role level might make more sense,
Ansible's inventory is meant to give a 'full picture' of the host and
any duplicate variables will be merged, groups are just a tag and
convenience to assign variables and select hosts, not a real entity in
a playbook, at that point there is only Hosts and Tasks, it doesn't
matter that you use GroupA to target a host, the host is still a
member of all the groups it is a member of.

sorry, second entry should be:

host1B ansible_host=hosta

Hello,

Thanks a lot for your help! From what I understand is that the ansible_host should be the actual hostname (dns entry). Is there any way to pass the to the ansible_host the name of an entry in the host_vars?

For example, I have in the host_vars directory, the folder Host1 where it has a vars.yml where one of the variables is ansible_host: "1.2.3.4".
In order to use your approach I will need to add the ansible_host inline in the inventory like this:
[GroupA]
host1A ansible_host="1.2.3.4"

[GroupB]
host1B ansible_host="1.2.3.4"

What I would like to do is to have something like this

[GroupA]
host1A ansible_host=Host1

[GroupB]
host1B ansible_host=Host1

Is this feasible?
If it is not feasible, I will have to add all the variables in the inventory and this will not be so clean.

Best Regards,
Konstantinos

ansible_host is just a variable, you can define it anywhere you want.

Hello,

What I want to have is one host folder (i.e. Host1) which will contain a vars.yml that will define the actual ansible_host (i.e. "1.2.3.4"). So, in the inventory I will need to have:
[GroupA]
host1A ansible_host=Host1

[GroupB]
host1B ansible_host=Host1

where the ansible_host will take the name of the host folder (i.e. Host1) and will find the actual value for the ansible_host (i.e. "1.2.3.4") from the vars.yml.
What I understood is that by using the ansible_host variable in the inventory I have to give the actual value (i.e. "1.2.3.4") and it cannot take let's say a host alias (i.e. Host1).

Is there any other way to define which host folder to check in order to retrieve the ansible_host variable?

Best Regards,
Konstantinos

that is kind of like making 4 left turns to go straight, unsure why
you would have the distinction in the first place, ... just use hostA
throughout.

Hello,

This is what I did and it didn't work. Different groups mean different configuration for my application (that is the reason of such distinction), so if I use HostA under both groups, it doesn't do the deployment twice in the same host but only for the last one.

Best Regards,
Konstantinos

This is how i’ve been doing it (not sure if you can implement)

inventory:

[GroupA]
host1
[GroupB]
host1

[Groups:children] . #throw all your vars that are the same for each group here
GroupA
GroupB

sorry, second host should be ‘GroupB’ in the playbook

using group_vars in vars_files is confusing as those are already
loaded and might produce unintended consequences, what you really want
is per play vars instead of inventory vars that relate to your
application

Hello,

Finally we made it work using the following approach:

Inventory file:
Host1 mygroups='["groupA","groupB"]'

Created two playbook files one with the tasks (PlaybookTasks.yml) and one that does an iteration (Playbook.yml)
Playbook.yml: