ansible_qemu_ceph_xcat_test.../cluster/roles/merge_vars/README.md

3.0 KiB

Role Info

A role called by another role inline to deep merge variables, this would typically merge custom variables source from a file in 'group_vars' with the role defaults or role vars files. Dictionaries will be merged, nested lists will be appended to. The role makes the assumption that the group_vars file is named the same as the role: role_name = ntp AND variables-file: = group_vars/ntp.yml

If you only want to overide variables without merge (sometimes necessary where you do not want nested lists to be appended), the ansible 'include_vars' module will suffice.

- include_vars:
    file: "{{ ansible_inventory_sources[0] | dirname }}/group_vars/{{ role_name }}.yml"
    name: "merge_{{ role_name }}"

- name: merge custom vars to vars[]
  set_fact:
    { "{{ entry }}": "{{ 'merge_'role_name[entry] }}" }
  loop: "{{ 'merge_'role_name | list }}"
  loop_control:
    loop_var: entry

# - name: merge steel['firewalld'] over role defaults
#   set_fact:
#     firewalld: "{{ firewalld | default({}) | combine( steel['firewalld'], recursive=True) }}"
#   when: steel['firewalld'] is defined

Role Variables

Accepts variables from calling role/task, returns dictionary vars_return.

## set calling role variables
- name: set role variable sources
  set_fact:
    role_info:
      role_defaults_file: "{{ role_path }}/defaults/main.yml"
      role_override_file: "{{ ansible_inventory_sources[0] | dirname }}/group_vars/{{ role_name }}.yml"
      vars_return: "placeholder"

Dependencies

Dependency on the ansible-merge-vars plugin.

https://github.com/leapfrogonline/ansible-merge-vars

Example Playbook

The top of your calling role/task should include the following block as the first item. Change 'role_defaults_file' and 'role_override_file' file locations to merge variables within files. Only 2 files maybe specified. Omit/replace merge custom vars to vars[] task to copy role_info['vars_return'] to a location of your choosing, by default this task places variables at the root of vars[].

- name: merge custom vars
  block:

  - name: set role variable sources
    set_fact:
      role_info:
        role_defaults_file: "{{ role_path }}/defaults/main.yml"
        role_override_file: "{{ ansible_inventory_sources[0] | dirname }}/group_vars/{{ role_name }}.yml"
        vars_return: "placeholder"

  - set_fact:
      source_role: "{{ role_name }}"

  - name: run merge_vars role
    include_role:
      name: "merge_vars"
    vars:
      a_config_file: "{{ role_info['role_defaults_file'] }}"
      b_config_file: "{{ role_info['role_override_file'] }}"
      calling_role: "{{ source_role }}"

  - name: merge custom vars to vars[]
    set_fact:
      { "{{ entry }}": "{{ role_info['vars_return'][entry] }}" }
    loop: "{{ role_info['vars_return'] | list }}"
    loop_control:
      loop_var: entry
    when:
      - not role_info['vars_return'] == 'placeholder'

  delegate_to: localhost

License

BSD