I have the following ansible setup:
ansible_test/playbooks/roles/main/main.yml
- name: hello world
hosts: all
tasks:
- name: hello!
shell: echo "test"
- name: display config
debug:
msg: "testvar is {{ hostvars['localhost']['testvar'] }}"
ansible_test/playbooks/inventory/hosts
[main]
localhost
[main:vars]
testvar=99
ansible-test/playbooks/inventory/group_vars/all/main.yml
testvar: 77
Now, when I modify the hosts file to the following:
[main]
localhost testvar=88
[main:vars]
testvar=99
I get an output of "testvar is 88". However, when I remove this, I get "testvar is 77" instead of the expected "testvar is 99".
When I add "testvar: 101" to a file: ansible_test/playbooks/inventory/host_vars/localhost.yml, I get an output of "testvar is 101".
I am running the command:
/usr/bin/ansible-playbook -i playbooks/inventory/hosts playbooks/roles/main/main.yml --connection=local -vvvv
Why is this? Shouldn't this host-group definition still take precedence over group_vars?

test_groupis calledtest_var, which is also the name of the var your assign in your role and inventory forhost_namebelonging todifferent_group. This is all really confusing. But since I guess your are running your test againsthost_name, the result you describe looks rather coherent: whendifferent_groupexists, it takes value defined in its group vars, when not, the default value in your role. Can you please double check all this, retest after your fix the eventual typo and see if it fixes your issue ? Thanks. - Zeitounatorhost_namein a single group, not two. - Zeitounator[test_group]\ntest_var<= This is declaring a host calledtest_var(nothost_name) in grouptest_group- Zeitounator