My feeling is that you are making a generality from a specific example. There is no such thing as the group name for the set of hosts that a play is executing on
. A play executes against a set of hosts you define in the hosts
key of a given play. What is expected in this parameter is a pattern. Even though in your example the pattern is a single group, it can be much more complicated than that.
The pattern is read at the beginning of the play. It is available as the raw given string inside the variable ansible_play_name
. So if you are 100% sure you will only give your group name, this var could do the trick. But this is far from being reliable on all occasions.
Once the pattern is read at beginning of play, it translates to a set of individual target to run the play against. This list is available in the ansible_play_hosts_all
variable (which contains all hosts, failed or not, as compared to ansible_play_hosts
you cited in your question).
Now if you want to reliably find the name of group(s) which are common to all hosts targeted in the play besides the all
and ungrouped
special groups, here is one solution.
To better understand the problem, I created the following example inventory in inventories/demo/hosts.yml
:
---
all:
children:
cluster_members:
children:
controllers:
hosts:
host1:
host2:
workers:
hosts:
host3:
host4:
random1:
hosts:
toto:
random2:
hosts:
pipo:
hosts:
bingo:
The following playbook:
---
- hosts: "{{ target }}"
gather_facts: false
tasks:
- name: Calculate common groups for hosts in play
vars:
current_groups: "{{ hostvars[item].group_names | difference(['all', 'ungrouped']) }}"
ansible.builtin.set_fact:
common_groups: "{{ common_groups | d(hostvars[ansible_play_hosts_all.0].group_names) | intersect(current_groups) }}"
loop: "{{ ansible_play_hosts_all }}"
run_once: true
- ansible.builtin.debug:
msg:
- "This play targets the following hosts: {{ ansible_play_hosts_all | join(', ') }}"
- "These targets where expanded from the following hosts play pattern: {{ ansible_play_name }}"
- "These hosts are part of {{ common_groups | count }} common group(s): {{ common_groups | join(', ') }}"
run_once: true
gives (several executions against different target patterns):
$ ansible-playbook -i inventories/demo/ playbook.yml -e target='cluster_members'
PLAY [cluster_members] ********************************************************************************************************************************************************************************************************
TASK [Calculate common groups for hosts in play] ******************************************************************************************************************************************************************************
ok: [host1] => (item=host1)
ok: [host1] => (item=host2)
ok: [host1] => (item=host3)
ok: [host1] => (item=host4)
TASK [ansible.builtin.debug] **************************************************************************************************************************************************************************************************
ok: [host1] => {
"msg": [
"This play targets the following hosts: host1, host2, host3, host4",
"These targets where expanded from the following hosts play pattern: cluster_members",
"These hosts are part of 1 common group(s): cluster_members"
]
}
PLAY RECAP ********************************************************************************************************************************************************************************************************************
host1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$ ansible-playbook -i inventories/demo/ playbook.yml -e target='workers'
PLAY [workers] ****************************************************************************************************************************************************************************************************************
TASK [Calculate common groups for hosts in play] ******************************************************************************************************************************************************************************
ok: [host3] => (item=host3)
ok: [host3] => (item=host4)
TASK [ansible.builtin.debug] **************************************************************************************************************************************************************************************************
ok: [host3] => {
"msg": [
"This play targets the following hosts: host3, host4",
"These targets where expanded from the following hosts play pattern: workers",
"These hosts are part of 2 common group(s): cluster_members, workers"
]
}
PLAY RECAP ********************************************************************************************************************************************************************************************************************
host3 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$ ansible-playbook -i inventories/demo/ playbook.yml -e target='all:!controllers'
PLAY [all:!controllers] *******************************************************************************************************************************************************************************************************
TASK [Calculate common groups for hosts in play] ******************************************************************************************************************************************************************************
ok: [bingo] => (item=bingo)
ok: [bingo] => (item=toto)
ok: [bingo] => (item=pipo)
ok: [bingo] => (item=host3)
ok: [bingo] => (item=host4)
TASK [ansible.builtin.debug] **************************************************************************************************************************************************************************************************
ok: [bingo] => {
"msg": [
"This play targets the following hosts: bingo, toto, pipo, host3, host4",
"These targets where expanded from the following hosts play pattern: all:!controllers",
"These hosts are part of 0 common group(s): "
]
}
PLAY RECAP ********************************************************************************************************************************************************************************************************************
bingo : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0