---
- name: Test role include variable override
  hosts: all
  gather_facts: true
  vars:
    # only use vgs matching this pattern
    snapshot_lvm_vg_include: "^test_"
    snapshot_lvm_action: check
    snapshot_lvm_percent_space_required: 2
    snapshot_lvm_snapset_name: snapset1
    snapshot_lvm_all_vgs: true
  tasks:
    - name: Run test in a block to clean up in always
      block:
        - name: >-
            Create var file in caller that can override the one in called role
          delegate_to: localhost
          copy:
            # usually the fake file will cause the called role to crash of
            # overriding happens, but if not, set a variable that will
            # allow to detect the bug
            content: "__caller_override: true"
            # XXX ugly, self-modifying code - changes the "caller" role on
            # the controller
            dest: "{{ playbook_dir }}/roles/caller/vars/{{ item }}.yml"
            mode: preserve
          loop: "{{ varfiles | unique }}"
          # In case the playbook is executed against multiple hosts, use
          # only the first one. Otherwise the hosts would stomp on each
          # other since they are changing files on the controller.
          when: inventory_hostname == ansible_play_hosts_all[0]
          vars:
            # change to hostvars['localhost']['ansible_facts'] to use the
            # information for localhost
            facts: "{{ ansible_facts }}"
            versions:
              - "{{ facts['distribution_version'] }}"
              - "{{ facts['distribution_major_version'] }}"
            separators: ["-", "_"]
            # create all variants like CentOS, CentOS_8.1, CentOS-8.1,
            # CentOS-8, CentOS-8.1
            # more formally:
            # {{ ansible_distribution }}-{{ ansible_distribution_version }}
            # {{ ansible_distribution }}-\
            # {{ ansible_distribution_major_version }}
            # {{ ansible_distribution }}
            # {{ ansible_os_family }}
            # and the same for _ as separator.
            varfiles: "{{ [facts['distribution']] | product(separators) |
              map('join') | product(versions) | map('join') | list +
              [facts['distribution'], facts['os_family']] }}"
          register: __varfiles_created

        - name: Import role
          import_role:
            name: caller
          vars:
            roletoinclude: linux-system-roles.snapshot
      always:
        - name: Cleanup
          file:
            path: "{{ item.dest }}"
            state: absent
          loop: "{{ __varfiles_created.results }}"
          delegate_to: localhost
          when: inventory_hostname == ansible_play_hosts_all[0]
