Search code examples
ansibleansible-2.xansible-inventory

Ansible not overriding default vars


I have the following variables defined in role/defaults/main.yml:

jvm_heap_size_max: 1024
jvm_heap_size_min: "{{ (jvm_heap_size_max * 0.5)|int|abs }}"

My group_vars folder looks like this:

├── group_vars
│   ├── all
│   ├── group
│   │   ├── vars
│   │   └── vault

And I have the following variable in inventories/test/group_vars/group/vars:

jvm_heap_size_max: 512
jvm_heap_size_min: 1024

My expected output is:

jvm_heap_size_max: 512
jvm_heap_size_min: 1024

But what I'm getting is this:

jvm_heap_size_max: 1024
jvm_heap_size_min: 1024

How come? I'm using Ansible 2.7.7


Solution

  • I forgot to mention that I'm targeting the same host in my example above. Apparently, this behavior is documented:

    https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#how-variables-are-merged

    By default variables are merged/flattened to the specific host before a play is run. This keeps Ansible focused on the Host and Task, so groups don’t really survive outside of inventory and host matching. By default, Ansible overwrites variables including the ones defined for a group and/or host (see the hash_merge setting to change this) .