# Playbook inventory group targeting

**URL:** <https://forum.ansible.com/t/playbook-inventory-group-targeting/10390>\
**Category:** Get Help\
**Created:** [October 18, 2024, 5:58am UTC](https://forum.ansible.com/t/playbook-inventory-group-targeting/10390 "2024-10-18T05:58:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeitler](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/zeitler/32/5198_2.png) [@zeitler](https://forum.ansible.com/u/zeitler)\
**Post date:** [October 18, 2024, 5:58am UTC](https://forum.ansible.com/t/playbook-inventory-group-targeting/10390/1 "2024-10-18T05:58:39Z")

</div>

Hi

Need some help on how inventory works.  
Having this structure:  
`inventory/groupA.yaml`

```auto
groupA:
  children:
    masters:
      hosts:
        master1ofA: {}
    slaves:
      hosts:
        slave1ofA: {}

```

`inventory/groupB.yaml`

```auto
groupB:
  children:
    masters:
      hosts:
        master1ofB: {}
    slaves:
      hosts:
        slave1ofB: {}

```

`playbooks/playbook.yaml`

```auto
---
- name: Print hostname on all hosts
  hosts: groupA
  become: false
  tasks:
    - name: Display hostname
      command: hostname

```

why:  
`ansible-playbook -i inventory playbooks/playbook.yaml --list-hosts`

```auto
playbook: playbooks/playbook.yaml

  play #1 (groupA): Print hostname TAGS: []
    pattern: ['groupA']
    hosts (4):
      slave1ofB
      master1ofB
      slave1ofA
      master1ofA

```

why does it appears the hosts of groupB?

If I change to `-i inventory/groupA.yaml` it target the correct hosts. So why it needs the `hosts: groupA` instruction in playbook?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![zeitler](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/zeitler/32/5198_2.png) [@zeitler](https://forum.ansible.com/u/zeitler)\
**Post date:** [October 18, 2024, 8:14am UTC](https://forum.ansible.com/t/playbook-inventory-group-targeting/10390/2 "2024-10-18T08:14:46Z")

</div>

Even in the playbook:  
`hosts: groupA:&masters`

```auto
ansible-playbook -i inventory playbook/playbook.yaml --list-hosts

playbook: playbook/playbook.yaml

  play #1 (groupA:&masters): Print hostname TAGS: []
    pattern: ['groupA:&masters']
    hosts (2):
      master1ofA
      master1ofB

```

It still target boths masters.  
If hosts are from groupA AND masters, why it target a master from groupB?

---

<div class="post-metadata">

**Author:** ![gwmngilfen](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/gwmngilfen/32/6_2.png) [@gwmngilfen](https://forum.ansible.com/u/gwmngilfen)\
**Post date:** [October 18, 2024, 8:23am UTC](https://forum.ansible.com/t/playbook-inventory-group-targeting/10390/3 "2024-10-18T08:23:28Z")

</div>



---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [October 18, 2024, 11:13am UTC](https://forum.ansible.com/t/playbook-inventory-group-targeting/10390/4 "2024-10-18T11:13:25Z")

</div>

Welcome to _The Forum_, @zeitler !

> [@zeitler](#):
>
> It still target boths masters.

There’s your misunderstanding. There is only one `masters` group. Or any other group. Groups don’t nest. You have two inventory files, both of which add hosts to the single `masters` group.

If you want that sort of hierarchy, you can fake it with group naming, like shown below:

```txt
a:
  children:
    a_masters:
      hosts:
        master1ofa: {}
    a_slaves:
      hosts:
        slave1ofa: {}
# No need for separate files, unless you find it convenient. Which is fine.
b:
  children:
    b_masters:
      hosts:
        master1ofb: {}
    b_slaves:
      hosts:
        slave1ofb: {}

```

---

<div class="post-metadata">

**Author:** ![zeitler](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/zeitler/32/5198_2.png) [@zeitler](https://forum.ansible.com/u/zeitler)\
**Post date:** [October 18, 2024, 1:40pm UTC](https://forum.ansible.com/t/playbook-inventory-group-targeting/10390/5 "2024-10-18T13:40:14Z")

</div>

thank you

So, in you example, what happens to “a”? It’s still a group? Ansible don’t make a difference about that? But it’s that its used in `hosts` on playbook. That’s why I’m not getting it.

Im my cases, I have some roles. In them, I have something like:

```auto
- name: Get cluster token
  ansible.builtin.include_tasks:
    apply:
      become: true
    file: get_cluster_token.yaml
  when: inventory_hostname in groups['masters'][0]

```

Doing how you suggest, how can I accomplish this role to other clusters?

The idea was having some roles, that I can reuse to all of our clusters. But everytime I start the playbook, I act on all hosts.  
If I do `ansible-playbook -i inventory/groupa.yaml ....` it works. But, If I’m defining the hosts on playbook… I feel that I’m repeating myself 🙂

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [October 18, 2024, 1:47pm UTC](https://forum.ansible.com/t/playbook-inventory-group-targeting/10390/6 "2024-10-18T13:47:34Z")

</div>

> [@zeitler](#):
>
> So, in you example, what happens to “a”? It’s still a group?

Yes, it’s still a group. Behold:

```txt
utoddl@tango:~/ansible/zeitler$ ansible -i inventory.yml --list-hosts all
  hosts (4):
    master1ofb
    slave1ofb
    master1ofa
    slave1ofa
utoddl@tango:~/ansible/zeitler$ ansible -i inventory.yml --list-hosts a
  hosts (2):
    master1ofa
    slave1ofa
utoddl@tango:~/ansible/zeitler$ ansible -i inventory.yml --list-hosts b
  hosts (2):
    master1ofb
    slave1ofb
utoddl@tango:~/ansible/zeitler$ ansible-inventory -i inventory.yml --list
{
    "_meta": {
        "hostvars": {}
    },
    "a": {
        "children": [
            "a_masters",
            "a_slaves"
        ]
    },
    "a_masters": {
        "hosts": [
            "master1ofa"
        ]
    },
    "a_slaves": {
        "hosts": [
            "slave1ofa"
        ]
    },
    "all": {
        "children": [
            "ungrouped",
            "a",
            "b"
        ]
    },
    "b": {
        "children": [
            "b_masters",
            "b_slaves"
        ]
    },
    "b_masters": {
        "hosts": [
            "master1ofb"
        ]
    },
    "b_slaves": {
        "hosts": [
            "slave1ofb"
        ]
    }
}

```

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [October 18, 2024, 3:09pm UTC](https://forum.ansible.com/t/playbook-inventory-group-targeting/10390/7 "2024-10-18T15:09:41Z")

</div>

> [@zeitler](#):
>
> Doing how you suggest, how can I accomplish this role to other clusters?

I’ve harped on about this [before](http://forum.ansible.com/t/multiple-clients-and-multiple-sub-groups/2496/5). It’s what I call “fully articulated” inventory groups. In our shop, we group things by service line, and within that by environment (dev, tst, prd, …). We also prepend our work group’s initials to all our host group names to keep them from colliding with those of other work groups. We’re called the “Middleware” group, hence the leading “`mw_`”. So all our host group names look like this:

```
mw_<service>[_<env>[_<hostclass>[_<subclass>]]]

```

Whenever we need it, we’ll also define a literal “`all`” group at any of those placeholder levels. Then we can say:

```
 when: inventory_hostname in groups['mw_xkcd_all_head']

```

Or the equivalent for your inventory if it were “fully articulated”:

```
when: inventory_hostname in groups['all_masters']

```

That lets us avoid contortions in our `when:` clauses like the following – which works, but who wants to read it, much less write it.:

```yaml
---
# /home/utoddl/ansible/zeitler/test.yml
- name: Play group name games
  hosts: all
  gather_facts: false
  tasks:
    - name: Are we in a *_master group
      ansible.builtin.debug:
        msg:
         - 'group_names: {{ group_names }}'
         - '{{ inventory_hostname }} is in a *_master group.'
      when: group_names | map("regex_search", "_master") | reject("==", None) | length

```

which produces

```txt
TASK [Are we in a *_master group] ***********************************************************
task path: /home/utoddl/ansible/zeitler/test.yml:7
ok: [master1ofb] => 
  msg:
  - 'group_names: [''b'', ''b_masters'']'
  - master1ofb is in a *_master group.
skipping: [slave1ofb] => 
  false_condition: group_names | map("regex_search", "_master") | reject("==", None) | length
skipping: [slave1ofa] => 
  false_condition: group_names | map("regex_search", "_master") | reject("==", None) | length
ok: [master1ofa] => 
  msg:
  - 'group_names: [''a'', ''a_masters'']'
  - master1ofa is in a *_master group.

```

I’m going to quote myself in case someone didn’t follow the link above, because I think it’s worth repeating.

> [@Multiple clients and multiple sub groups](http://forum.ansible.com/t/multiple-clients-and-multiple-sub-groups/2496/5):
>
> If you take nothing else from our experience, consider this suggestion: **Although Ansible allows you to structure your inventory in gajillions of ways besides a strict hierarchy, don’t do it.** It’s easy to understand, verify, and maintain a hierarchy, and therefor easy(ish) to see when you mess it up. But if you accidentally build an _N_-dimensional ad hoc polyfrackus inventory from Mordor, you could get in pretty deep before you realize you’ve created a monster.

---

<div class="post-metadata">

**Author:** ![zeitler](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/zeitler/32/5198_2.png) [@zeitler](https://forum.ansible.com/u/zeitler)\
**Post date:** [October 18, 2024, 5:50pm UTC](https://forum.ansible.com/t/playbook-inventory-group-targeting/10390/8 "2024-10-18T17:50:13Z")

</div>

Thank you very much for your explanation.  
Understood now quite well how to accomplish what I wanted. Even better that I first thought

Thank you

---

<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:** [November 17, 2024, 5:50pm UTC](https://forum.ansible.com/t/playbook-inventory-group-targeting/10390/9 "2024-11-17T17:50:55Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
