group vars ?

Wondering the variables could have been assigned per group, right.
If i reuse the variable of same name they are overwritten within the
hosts file or with global_vars now ?

hosts:

[group1]
10.100.1.100

[group2]
10.100.1.100

[group1:vars]
username=value1

[group2:vars]
username=value2

The playbook will give me result 'value2' now, also tried with --extra-
vars="groups=group1"

Wondering the variables could have been assigned per group, right.
If i reuse the variable of same name they are overwritten within the
hosts file or with global_vars now ?

The “group_vars” files definitely resolve in tree order, where a group name can override all. This may not be currently true for [group:vars] inline.

hosts should override groups.

hosts:

[group1]
10.100.1.100

[group2]
10.100.1.100

[group1:vars]
username=value1

[group2:vars]
username=value2

The playbook will give me result ‘value2’ now, also tried with --extra-
vars=“groups=group1”

Not sure how that part about extra vars is relevant if you don’t had a variable $groups in your “hosts:” directive, it’s pretty meaningless.
In fact, you probably shouldn’t set this at all, as the variable “groups” is typically useful to Ansible. “hosts” maybe


  • name: test
    hosts: group1

hosts: $hosts