--- - name: "Set playbook path for {{ hook.name }}" vars: _hook_name: >- {{ hook.name | regex_replace('([^a-zA-Z0-9\-\._])+', '_') | lower | truncate(30, true, '') }} _play: >- {%- if hook.source is not ansible.builtin.abs -%} {{ role_path }}/../../hooks/playbooks/{{ hook.source }} {%- else -%} {{ hook.source }} {%- endif -%} _bdir: "{{ cifmw_basedir }}" _operator_namespace: >- {{ cifmw_install_yamls_defaults['OPERATOR_NAMESPACE'] | default('openstack-operators') }} _namespace: >- {{ cifmw_install_yamls_defaults['NAMESPACE'] | default('openstack') }} ansible.builtin.set_fact: cifmw_basedir: "{{ _bdir }}" hook_name: "{{ _hook_name }}" playbook_path: "{{ _play | realpath }}" log_path: >- {{ _bdir }}/logs/{{ step }}_{{ _hook_name }}.log extra_vars: >- -e namespace={{ cifmw_openstack_namespace }} {%- if hook.extra_vars is defined and hook.extra_vars|length > 0 -%} {% for key,value in hook.extra_vars.items() -%} {%- if key == 'file' %} -e "@{{ value }}" {%- else %} -e "{{ key }}={{ value }}" {%- endif %} {%- endfor %} {%- endif %} - name: Ensure file exists block: - name: Get file stat register: playbook_stat ansible.builtin.stat: path: "{{ playbook_path }}" - name: Fail if playbook doesn't exist when: - not playbook_stat.stat.exists ansible.builtin.fail: msg: "Playbook {{ playbook_path }} doesn't seem to exist." - name: Get parameters files ansible.builtin.find: paths: >- {{ (cifmw_basedir, 'artifacts/parameters') | path_join }} file_type: file patterns: "*.yml" register: cifmw_run_hook_parameters_files - name: "Add parameters artifacts as extra variables" ansible.builtin.set_fact: extra_vars: >- {{ extra_vars }} {% for file in cifmw_run_hook_parameters_files.files %} -e "@{{ file.path }}" {%- endfor %} - name: Ensure log directory exists ansible.builtin.file: path: "{{ log_path | dirname }}" state: directory mode: "0755" - name: Ensure artifacts directory exists ansible.builtin.file: path: "{{ cifmw_basedir }}/artifacts" state: directory mode: "0755" # We cannot call ansible.builtin.import_playbook from within a play, # even less from a task. So the way to run a playbook from within a playbook # is to call a command. Though we may lose some of the data passed to the # "main" play. - name: "Run hook without retry - {{ hook.name }}" when: not hook.hook_retry | default(false) no_log: "{{ cifmw_nolog | default(true) | bool }}" cifmw.general.ci_script: output_dir: "{{ cifmw_basedir }}/artifacts" extra_args: >- {{ { 'ANSIBLE_CONFIG': hook.config_file | default(ansible_config_file), 'ANSIBLE_LOG_PATH': log_path } | combine( {'ANSIBLE_GATHERING': hook.gathering} if hook.gathering is defined else {} ) }} creates: "{{ hook.creates | default(omit) }}" script: >- ansible-playbook -i {{ hook.inventory | default(inventory_file) }} {% if hook.connection is defined -%} -c {{ hook.connection }} {% endif -%} {{ extra_vars }} -e "cifmw_basedir={{ cifmw_basedir }}" -e "step={{ step }}" -e "hook_name={{ hook_name }}" -e "playbook_dir={{ playbook_path | dirname }}" {{ playbook_path }} - name: "Run hook with retry - {{ hook.name }}" when: hook.hook_retry | default(false) no_log: "{{ cifmw_nolog | default(true) | bool }}" cifmw.general.ci_script: output_dir: "{{ cifmw_basedir }}/artifacts" extra_args: >- {{ { 'ANSIBLE_CONFIG': hook.config_file | default(ansible_config_file), 'ANSIBLE_LOG_PATH': log_path } | combine( {'ANSIBLE_GATHERING': hook.gathering} if hook.gathering is defined else {} ) }} creates: "{{ hook.creates | default(omit) }}" script: >- ansible-playbook -i {{ hook.inventory | default(inventory_file) }} {% if hook.connection is defined -%} -c {{ hook.connection }} {% endif -%} {{ extra_vars }} -e "cifmw_basedir={{ cifmw_basedir }}" -e "step={{ step }}" -e "hook_name={{ hook_name }}" -e "playbook_dir={{ playbook_path | dirname }}" {{ playbook_path }} register: hook_result retries: 3 delay: 10 until: hook_result is not failed - name: Load generated content if any block: - name: Check if we have a file register: hook_callback ansible.builtin.stat: path: "{{ cifmw_basedir }}/artifacts/{{ step }}_{{ hook_name }}.yml" - name: Load generated content in main playbook when: hook_callback.stat.exists ansible.builtin.include_vars: file: "{{ cifmw_basedir }}/artifacts/{{ step }}_{{ hook_name }}.yml"