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

41 views
Skip to first unread message

Konstantinos Tzevelekidis

unread,
Jan 9, 2020, 1:29:41 PM1/9/20
to Ansible Project
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

Brian Coca

unread,
Jan 9, 2020, 1:49:31 PM1/9/20
to Ansible Project
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.


--
----------
Brian Coca

Brian Coca

unread,
Jan 9, 2020, 1:53:20 PM1/9/20
to Ansible Project
sorry, second entry should be:

host1B ansible_host=hosta



--
----------
Brian Coca

Konstantinos Tzevelekidis

unread,
Jan 10, 2020, 5:23:00 AM1/10/20
to Ansible Project
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

Brian Coca

unread,
Jan 10, 2020, 10:42:33 AM1/10/20
to Ansible Project
ansible_host is just a variable, you can define it anywhere you want.


--
----------
Brian Coca

Konstantinos Tzevelekidis

unread,
Jan 10, 2020, 11:09:48 AM1/10/20
to Ansible Project
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

Brian Coca

unread,
Jan 13, 2020, 9:22:45 AM1/13/20
to Ansible Project
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.


--
----------
Brian Coca

Konstantinos Tzevelekidis

unread,
Jan 13, 2020, 10:11:11 AM1/13/20
to Ansible Project
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

Austin Wolfson

unread,
Jan 13, 2020, 10:21:41 AM1/13/20
to ansible...@googlegroups.com
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

-------------
* make group_vars for each group name with specific variables and then calli in your playbook:

- hosts: 'GroupA'
  vars_files:
    - ../group_vars/GroupA
 roles:
  - blah blah blah

- hosts: 'GroupA'
  vars_files:
    - ../group_vars/GroupB
 roles:
  - blah blah blah

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d3e09cc9-e792-4c63-8077-35be809ee8f2%40googlegroups.com.


--
Austin Wolfson
DevOps Engineer, CEH
Interactions, LLC

*******************************************************************************

This e-mail and any of its attachments may contain Interactions LLC proprietary information, which is privileged, confidential, or subject to copyright belonging to the Interactions LLC. This e-mail is intended solely for the use of the individual or entity to which it is addressed. If you are not the intended recipient of this e-mail, you are hereby notified that any dissemination, distribution, copying, or action taken in relation to the contents of and attachments to this e-mail is strictly prohibited and may be unlawful. If you have received this e-mail in error, please notify the sender immediately and permanently delete the original and any copy of this e-mail and any printout. Thank You. 

******************************************************************************* 

Austin Wolfson

unread,
Jan 13, 2020, 10:22:44 AM1/13/20
to ansible...@googlegroups.com
sorry, second host should be 'GroupB' in the playbook

Brian Coca

unread,
Jan 13, 2020, 10:27:19 AM1/13/20
to Ansible Project
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




--
----------
Brian Coca

Konstantinos Tzevelekidis

unread,
Jan 14, 2020, 8:52:13 AM1/14/20
to Ansible Project
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:
---
- hosts: all
tasks:
- include: PlaybookTasks.yml mygroup="{{ item }}"
with_items: "{{ mygroups}}"

PlaybookTasks.yml:
- name: Include vars
include: "group_vars/{{ group}}/vars.yml"
- name: Download application
.....

The structure of the host_vars and group_vars remained as I initially explained.

Thank you all for the support and your prompt answers.

Best Regards,
Konstantinos

Reply all
Reply to author
Forward
0 new messages