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:
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: {}
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:
- 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
I’ve harped on about this before. 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.:
---
# /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
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.