5

Is there any way that I can get the group name for the set of hosts that a play is executing on? I know that ansible has a variable called ansible_play_hosts which is a list of all the hosts that a particular play is executing on. I want the actual group name that encompasses all these hosts.

I am using ansible version 2.3.2.0

Example:

# file: hosts

[my-host-group]
hostname-1
hostname-2


# file: playbook.yml
---

- hosts: my-host-group
  tasks:
    - name: "Print group name for 'hosts'"
      debug:
        msg: "Hosts var is '{{ hosts }}'"  

I want the message to print Hosts var is 'my-host-group'

Sami Badra
  • 111
  • 2
  • 6
  • Thats like wanting to print out the name of a variable. Whats the point? – mewc Mar 22 '18 at 03:52
  • @mewc you never had to print the key? Always the value ? – ady8531 Mar 22 '18 at 05:09
  • @mewc, I don't want to print the name of the variable, I actually want to get the value of the hosts variable inside the play configuration. In my case, I need to use the group name for determining what to name a particular file when copying it. – Sami Badra Mar 22 '18 at 16:38

1 Answers1

1

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  
Zeitounator
  • 38,476
  • 7
  • 53
  • 66