Answer the question
In order to leave comments, you need to log in
Strange behavior of Ansible, a bug or I didn't get it right?
Good afternoon. I started using Ansible actively and ran into a problem. For some reason, vars from one group are distributed to others, ignoring inheritance.
---
all:
children:
location1:
children:
debian:
hosts:
server1:
ansible_host: 192.168.0.1
ansible_user: root
vars:
ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q [email protected]"'
location2:
children:
debian:
hosts:
server2:
ansible_host: 192.168.0.2
ansible_user: root
{
"_meta": {
"hostvars": {
"server1": {
"ansible_host": "192.168.0.1",
"ansible_ssh_common_args": "-o ProxyCommand=\"ssh -W %h:%p -q [email protected]\"",
"ansible_user": "root"
},
"server2": {
"ansible_host": "192.168.0.2",
"ansible_ssh_common_args": "-o ProxyCommand=\"ssh -W %h:%p -q [email protected]\"",
"ansible_user": "root"
}
}
},
"all": {
"children": [
"location1",
"location2",
"ungrouped"
]
},
"debian": {
"hosts": [
"server1",
"server2"
]
},
"location1": {
"children": [
"debian"
]
},
"location2": {
"children": [
"debian"
]
}
}
Answer the question
In order to leave comments, you need to log in
Why this happens - Inventory has a flat structure. Essentially you only have hosts and groups. When expanding the hierarchy you described, ansible finds a group twice with the same name - debian. And this group is a child of both location1 and location2, so the variables for the hosts of those groups are merged.
How to get around this situation - think for yourself how it will be easier for you. For example, I would make different inventory files for each location. Or, changed the hierarchy to all-> debian->location1->hosts
decided for myself
---
all:
children:
location1:
children:
debian1:
hosts:
server1:
ansible_host: 192.168.0.1
ansible_user: root
vars:
ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q [email protected]"'
location2:
children:
debian2:
hosts:
server2:
ansible_host: 192.168.0.2
ansible_user: root
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question