variables defined in the Inventory

Dear all,

I have an inventory defined in the way below:

[Group1]
host1

[Group1:vars]
package_name: aaa

foo: bar

[Group2]
host1

[Group2:vars]
package_name: bbb
foo1: bar

then I defined a playbook for the hosts with: Group2. But when I execute the playbook, I found the package_name was in aaa instead of bbb.

What’s the problem here?

Thanks for the advice
Regards, Wenxing

There is no problem, this is how Ansible work.

You always get all the variables for a host regardless from where the variable is defined, inventory, group_vars and host_vars.

So in you example host1 will always have foo and foo1 defined.
And since package_name is set two times for host1 only the last one will stick since the variable is overwritten.

Get it. Many thanks for the feedback.