Dictionary lookups being clobbered when assigning values to children elsewhere

Hey People,

Having some trouble using YAML dictionaries.
I'm refactoring my variable structure in a role a wrote a while ago.

The variables used to all look like: sensu_api_host, but I wanted to move to using dictionaries, so when referencing them it wouldn't be:
{{ sensu_api_host }}
it'd be:
{{ sensu['api']['host'] }}

In doing so, I've restructured my roles' defaults/main.yml like so:
sensu:
  api:
    host: "{{ groups['sensu_masters'][0] }}"
    port: 4567
    ssl: "false"
    user_name: admin
    password: secret
    uchiwa_path: ''
    timeout: 5000
  client_config: client.json.j2
  config_path: /opt/local/etc/sensu
  gem_state: present
  plugin_gem_state: present
  user_name: sensu
  group_name: sensu
  include_plugins: true
  include_dashboard: false
  master: false

Though, I have a problem. Lookups for the above variables work fine... until I try to assign them values elsewhere, where it should take precedence.

Some of the key values above are clobbered when they are defined elsewhere, such as host_vars/hostname.yml or group_vars/groupname.yml.
It seems that if I want to define one of the nested keys' value elsewhere, it ruins the dictionary lookup for all other child keys who share that parent.

For example, above you can see that sensu['include_dashboard'] is set to false.
If I want to set this to `true` for a particular host, in host_vars/hostname.yml like so:

I’m still new to Ansible, but my understanding is that it overwrites variables by default, including dictionary variables. This is because the default value for hash_behavior setting is “replace”. If you change that config value to “merge”, you’ll see the behavior you’re expecting, I believe.

I don’t fully understand why replace is preferred, but I’ve found it easier to just avoid dictionary variables just to keep things simple. Seems like some changes have been made in v2.0.

The default behavior of Ansible is to “replace” the entire data structure instead of merging it. This is defined/configured by the hash_behavior parameter in an ansible.cfg file.

Because switching to merge is a process that requires a user to modify their Ansible configuration, I feel that is most appropriate to not use nested data structures where you expect users to provide configurations for your role, but instead use what you were doing previously with sensu_api_host.

You’re absolutely right - thanks for taking the time to point this out!

Thanks, Matt.
In that case, I’ll keep it the way I’ve written it :slight_smile: