I'm new to ansible. I'm sorry if my question is basic.
Let's say I have a web server in a group called web
in my inventory file. The same server is also in the test
group. Something like this:
web:
hosts:
vm01:
ansible_host: foo.example.com
ansible_user: user1
test:
hosts:
vm01:
ansible_user: user2
If I run:
ansible-inventory -i inventory.yaml --list
I get the following output:
{
"_meta": {
"hostvars": {
"vm01": {
"ansible_host": "foo.example.com",
"ansible_user": "user2"
}
}
},
"all": {
"children": [
"test",
"ungrouped",
"web"
]
},
"test": {
"hosts": [
"vm01"
]
},
"web": {
"hosts": [
"vm01"
]
}
}
First of all, why vm01
is getting ansible_user
variable from the test
group?? Isn't it supposed to get it from the web
??? According to documentation, variables at the same level will have values according to the parent's ASCII order.
Second, is it possible to run ansible somehow, to use ansible_user
variable from test
group instead??
Why
vm01
is gettingansible_user
variable from the test group??
According Understanding variable precedence
In general, Ansible gives precedence to variables that were defined more recently, more actively, and with more explicit scope.
In other words, the last definition wins always.
Isn't it supposed to get it from the
web
???
Therefore, no.
Is it possible to run ansible somehow, to use
ansible_user
variable fromtest
group instead??
Yes, by limiting the execution to that group (test
) in example via targeting hosts and groups.