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

**URL:** <https://forum.ansible.com/t/question-how-to-use-the-same-host-for-multiple-groups/31944>\
**Category:** Ansible Project\
**Created:** [January 9, 2020, 6:29pm UTC](https://forum.ansible.com/t/question-how-to-use-the-same-host-for-multiple-groups/31944 "2020-01-09T18:29:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Konstantinos\_Tzevele](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@Konstantinos\_Tzevele](https://forum.ansible.com/u/Konstantinos_Tzevele)\
**Post date:** [January 9, 2020, 6:29pm UTC](https://forum.ansible.com/t/question-how-to-use-the-same-host-for-multiple-groups/31944/1 "2020-01-09T18:29:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 9, 2020, 6:48pm UTC](https://forum.ansible.com/t/question-how-to-use-the-same-host-for-multiple-groups/31944/2 "2020-01-09T18:48:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 9, 2020, 6:52pm UTC](https://forum.ansible.com/t/question-how-to-use-the-same-host-for-multiple-groups/31944/3 "2020-01-09T18:52:23Z")

</div>

sorry, second entry should be:

host1B ansible\_host=hosta

---

<div class="post-metadata">

**Author:** ![Konstantinos\_Tzevele](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@Konstantinos\_Tzevele](https://forum.ansible.com/u/Konstantinos_Tzevele)\
**Post date:** [January 10, 2020, 10:22am UTC](https://forum.ansible.com/t/question-how-to-use-the-same-host-for-multiple-groups/31944/4 "2020-01-10T10:22:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 10, 2020, 3:41pm UTC](https://forum.ansible.com/t/question-how-to-use-the-same-host-for-multiple-groups/31944/5 "2020-01-10T15:41:32Z")

</div>

ansible\_host is just a variable, you can define it anywhere you want.

---

<div class="post-metadata">

**Author:** ![Konstantinos\_Tzevele](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@Konstantinos\_Tzevele](https://forum.ansible.com/u/Konstantinos_Tzevele)\
**Post date:** [January 10, 2020, 4:09pm UTC](https://forum.ansible.com/t/question-how-to-use-the-same-host-for-multiple-groups/31944/6 "2020-01-10T16:09:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 13, 2020, 2:21pm UTC](https://forum.ansible.com/t/question-how-to-use-the-same-host-for-multiple-groups/31944/7 "2020-01-13T14:21:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Konstantinos\_Tzevele](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@Konstantinos\_Tzevele](https://forum.ansible.com/u/Konstantinos_Tzevele)\
**Post date:** [January 13, 2020, 3:11pm UTC](https://forum.ansible.com/t/question-how-to-use-the-same-host-for-multiple-groups/31944/8 "2020-01-13T15:11:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Austin\_Wolfson](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@Austin\_Wolfson](https://forum.ansible.com/u/Austin_Wolfson)\
**Post date:** [January 13, 2020, 3:21pm UTC](https://forum.ansible.com/t/question-how-to-use-the-same-host-for-multiple-groups/31944/9 "2020-01-13T15:21:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Austin\_Wolfson](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@Austin\_Wolfson](https://forum.ansible.com/u/Austin_Wolfson)\
**Post date:** [January 13, 2020, 3:22pm UTC](https://forum.ansible.com/t/question-how-to-use-the-same-host-for-multiple-groups/31944/10 "2020-01-13T15:22:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 13, 2020, 3:26pm UTC](https://forum.ansible.com/t/question-how-to-use-the-same-host-for-multiple-groups/31944/11 "2020-01-13T15:26:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Konstantinos\_Tzevele](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@Konstantinos\_Tzevele](https://forum.ansible.com/u/Konstantinos_Tzevele)\
**Post date:** [January 14, 2020, 1:52pm UTC](https://forum.ansible.com/t/question-how-to-use-the-same-host-for-multiple-groups/31944/12 "2020-01-14T13:52:13Z")

</div>

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:
