'Why, in Ansible, I can't have same key in many children groups?
I want to make some clean stuff with my ansible hosts file.
If i use ansible-playbook --limit calendar -i hosts update_common.yml
Ansible execute my playbook on all hosts, even on hostname in other groups.
Apparently, it seems to comme from my keys dev and prod :
all:
children:
gitlab:
children:
dev:
hosts:
gitlab-dev: # Stretch
prod:
hosts:
gitlab-A: # Stretch
gitlab-B: # Stretch
gitlab-C.mysociety.fr:
ansible_port: 22
intranet:
children:
dev:
hosts:
intra-dev: # Buster
wordpress-dev: # Buster
prod:
hosts:
intra-prod: # Buster
calendar:
children:
dev:
hosts:
calendar-dev:
prod:
hosts:
calendar:
Solution 1:[1]
Group names must be unique, it's just the way ansible works. You can either create separate inventory files with same group names, and invoke your playbooks with different inventories or have unique group names.
See this issue https://github.com/ansible/ansible/issues/32247 and comment from one of the devs:
This won't work as group names are unique, they are not based on their relationships with other groups.
I recommend creating parent1_subgroup1 named groups that have the hierarchy you want built in.
Solution 2:[2]
I believe Ansible interprets the inventory slightly different.
You specify the playbook to be executed on all hosts in the calendar group. That one contains everything in the children group. And since children also contains gitlab and intranet...
Very likely the problem is that you use the keyword at many different levels and locations. If that should work, you'd have to specify a path for the playbook run. Seems just not the way Ansible was designed.
Solution 3:[3]
Yes when I make ansible-inventory -i hosts --list
We can see how it is managed, dev hosts are grouped together, same for prod :(
"all": {
"children": [
"calendar",
"gitlab",
"intranet",
"ungrouped"
]
},
"dev": {
"hosts": [
"calendar-dev",
"gitlab-test",
"intra-dev",
"wordpress-dev"
]
},
"fil-calendar": {
"children": [
"dev",
"prod"
]
},
"gitlab": {
"children": [
"dev",
"prod"
]
},
"intranet": {
"children": [
"dev",
"prod"
]
},
"prod": {
"hosts": [
"calendar",
"gitlab-A",
"gitlab-B",
"gitlab-C.mysociety.fr",
"intra-prod"
]
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrew |
| Solution 2 | |
| Solution 3 | Mickael Bertainchant |
