defaults/main.yml
in a Role will be overridden in the precedence chain. In essence, as I understand, it is a dictionary that will be overlayed/merged/combined with sub-sequent variable settings (e.g. by inventory variables) in the manner as described here. But I suspect, this isn't done recursively.myapp_component1_feature1: defaultA
myapp_component1_feature2: defaultB
myapp_component2_feature1: defaultC
myapp_component2_feature2: defaultD
myapp:
component1:
feature1: defaultA
feature2: defaultB
component2:
feature1: defaultC
feature2: defaultD
www.example.com
vars:
myapp:
component1:
feature2: myvalueB
component2:
feature1: myvalueC
Ansible has an option hash_behavior(default is replace) that you can set
to merge, it will then merge hashes/dictonaries instead of replacing
them.
myapp_defaults:
component1:
feature1: defaultA
feature2: defaultB
component2:
feature1: defaultC
feature2: defaultD
myapp_vars: |
{%- if myapp is defined and myapp is iterable -%}
{{ myapp_defaults | combine (myapp, recursive=True) }}
{%- else -%}
{{ myapp_defaults }}
{%- endif -%}
- name: Some task
XXX:
YYY: "{{ myapp_vars.component2.feature1 }}"
...
...
# My Playbook
- name: setup MyApp
vars:
myapp:
component2:
feature1: "foo"
feature2: "bar"
tasks:
- include_role:
name: myapp
# My Playbook
- name: setup MyApp
tasks:
- include_role:
name: myapp
vars:
myapp:
component2: ...
...