|
1 | 1 | --- |
2 | 2 | - name: "Set playbook path for {{ hook.name }}" |
3 | | - ansible.builtin.set_fact: |
4 | | - cifmw_basedir: "{{ cifmw_basedir | default(ansible_user_dir ~ '/ci-framework-data') }}" |
5 | | - hook_name: "{{ hook.name | regex_replace('([^\x00-\x7F]|\\s|`)+', '_') | lower }}" |
6 | | - playbook_path: >- |
| 3 | + vars: |
| 4 | + _hook_name: >- |
| 5 | + {{ hook.name | regex_replace('([^a-zA-Z0-9\-\._])+', '_') | lower }} |
| 6 | + _play: >- |
7 | 7 | {%- if hook.source is not ansible.builtin.abs -%} |
8 | 8 | {{ role_path }}/../../hooks/playbooks/{{ hook.source }} |
9 | 9 | {%- else -%} |
10 | 10 | {{ hook.source }} |
11 | 11 | {%- endif -%} |
| 12 | + _bdir: >- |
| 13 | + {{ cifmw_basedir | default(ansible_user_dir ~ '/ci-framework-data') }} |
| 14 | + ansible.builtin.set_fact: |
| 15 | + cifmw_basedir: "{{ _bdir }}" |
| 16 | + hook_name: "{{ _hook_name }}" |
| 17 | + playbook_path: "{{ _play | realpath }}" |
| 18 | + log_path: >- |
| 19 | + {{ _bdir }}/logs/{{ step }}_{{ _hook_name }}.log |
12 | 20 | extra_vars: >- |
13 | 21 | {%- if hook.extra_vars is defined and hook.extra_vars|length > 0 -%} |
14 | 22 | {% for key,value in hook.extra_vars.items() -%} |
|
20 | 28 | {%- endfor %} |
21 | 29 | {%- endif %} |
22 | 30 |
|
| 31 | +- name: Ensure file exists |
| 32 | + block: |
| 33 | + - name: Get file stat |
| 34 | + register: playbook_stat |
| 35 | + ansible.builtin.stat: |
| 36 | + path: "{{ playbook_path }}" |
| 37 | + - name: Fail if playbook doesn't exist |
| 38 | + when: |
| 39 | + - not playbook_stat.stat.exists |
| 40 | + ansible.builtin.fail: |
| 41 | + msg: "Playbook {{ playbook_path }} doesn't seem to exist." |
| 42 | + |
23 | 43 | - name: Get parameters files |
24 | 44 | ansible.builtin.find: |
25 | | - paths: "{{ cifmw_basedir|default(ansible_user_dir ~ '/ci-framework-data') }}/artifacts/parameters" |
| 45 | + paths: >- |
| 46 | + {{ |
| 47 | + (cifmw_basedir, 'artifacts/parameters') | path_join |
| 48 | + }} |
26 | 49 | file_type: file |
27 | 50 | patterns: '*.yml' |
28 | 51 | register: cifmw_run_hook_parameters_files |
29 | 52 |
|
30 | | -- name: "Add parameters artifacts as extra variables" # noqa jinja[spacing] |
| 53 | +- name: "Add parameters artifacts as extra variables" |
31 | 54 | ansible.builtin.set_fact: |
32 | 55 | extra_vars: >- |
33 | 56 | {{ extra_vars }} |
34 | 57 | {% for file in cifmw_run_hook_parameters_files.files %} |
35 | 58 | -e "@{{ file.path }}" |
36 | 59 | {%- endfor %} |
37 | 60 |
|
38 | | -- name: Debug extra_vars |
39 | | - ansible.builtin.debug: |
40 | | - var: extra_vars |
| 61 | +- name: Ensure log directory exists |
| 62 | + ansible.builtin.file: |
| 63 | + path: "{{ log_path | dirname }}" |
| 64 | + state: directory |
| 65 | + mode: "0755" |
41 | 66 |
|
42 | | -- name: Ensure file exists |
43 | | - block: |
44 | | - - name: Get file stat |
45 | | - register: playbook_stat |
46 | | - ansible.builtin.stat: |
47 | | - path: "{{ playbook_path }}" |
48 | | - - name: Fail if playbook doesn't exist |
49 | | - when: |
50 | | - - not playbook_stat.stat.exists |
51 | | - ansible.builtin.fail: |
52 | | - msg: "Playbook {{ playbook_path }} doesn't seem to exist." |
| 67 | +- name: Ensure artifacts directory exists |
| 68 | + ansible.builtin.file: |
| 69 | + path: "{{ cifmw_basedir }}/artifacts" |
| 70 | + state: directory |
| 71 | + mode: "0755" |
| 72 | + |
| 73 | +- name: Point to the generated log file |
| 74 | + ansible.builtin.debug: |
| 75 | + msg: "You can follow hook run here: {{ log_path }}" |
53 | 76 |
|
54 | 77 | # We cannot call ansible.builtin.import_playbook from within a play, |
55 | 78 | # even less from a task. So the way to run a playbook from within a playbook |
56 | 79 | # is to call a command. Though we may lose some of the data passed to the |
57 | 80 | # "main" play. |
58 | | -- name: Ensure we get the logs even in case of failure |
59 | | - block: |
60 | | - - name: "Run play {{ playbook_path }}" |
61 | | - register: play_output |
62 | | - environment: |
63 | | - ANSIBLE_CONFIG: "{{ hook.config_file | default(ansible_config_file) }}" |
64 | | - ANSIBLE_LOG_PATH: >- |
65 | | - {{ cifmw_basedir }}/logs/{{ step }}_{{ hook_name }}.log |
66 | | - no_log: true |
67 | | - ansible.builtin.shell: # noqa: command-instead-of-shell |
68 | | - cmd: >- |
69 | | - ansible-playbook -i {{ hook.inventory | default(inventory_file) }} |
70 | | - {% if hook.connection is defined -%} |
71 | | - -c {{ hook.connection }} |
72 | | - {% endif -%} |
73 | | - {{ extra_vars }} |
74 | | - -e "cifmw_basedir={{ cifmw_basedir }}" |
75 | | - -e "step={{ step }}" |
76 | | - -e "hook_name={{ hook_name }}" |
77 | | - -e "playbook_dir={{ playbook_path | dirname }}" |
78 | | - {{ playbook_path }} |
79 | | - creates: "{{ hook.creates | default(omit) }}" |
80 | | - |
81 | | - - name: Load generated content if any |
82 | | - block: |
83 | | - - name: Check if we have a file |
84 | | - register: hook_callback |
85 | | - ansible.builtin.stat: |
86 | | - path: "{{ cifmw_basedir }}/artifacts/{{ step }}_{{ hook_name }}.yml" |
87 | | - - name: Load generated content in main playbook |
88 | | - when: |
89 | | - hook_callback.stat.exists |
90 | | - ansible.builtin.include_vars: |
91 | | - file: "{{ cifmw_basedir }}/artifacts/{{ step }}_{{ hook_name }}.yml" |
92 | | - |
93 | | - always: |
94 | | - - name: Output hook command |
95 | | - ansible.builtin.debug: |
96 | | - var: play_output.cmd |
97 | | - |
98 | | - - name: Output play stderr |
99 | | - ansible.builtin.debug: |
100 | | - var: play_output.stderr_lines |
| 81 | +- name: "Run {{ hook.name }}" |
| 82 | + no_log: true |
| 83 | + cifmw.general.ci_script: |
| 84 | + output_dir: "{{ cifmw_basedir }}/artifacts" |
| 85 | + extra_args: |
| 86 | + ANSIBLE_CONFIG: "{{ hook.config_file | default(ansible_config_file) }}" |
| 87 | + ANSIBLE_LOG_PATH: "{{ log_path }}" |
| 88 | + creates: "{{ hook.creates | default(omit) }}" |
| 89 | + script: >- |
| 90 | + ansible-playbook -i {{ hook.inventory | default(inventory_file) }} |
| 91 | + {% if hook.connection is defined -%} |
| 92 | + -c {{ hook.connection }} |
| 93 | + {% endif -%} |
| 94 | + {{ extra_vars }} |
| 95 | + -e "cifmw_basedir={{ cifmw_basedir }}" |
| 96 | + -e "step={{ step }}" |
| 97 | + -e "hook_name={{ hook_name }}" |
| 98 | + -e "playbook_dir={{ playbook_path | dirname }}" |
| 99 | + {{ playbook_path }} |
101 | 100 |
|
102 | | - - name: Output play stdout |
103 | | - ansible.builtin.debug: |
104 | | - var: play_output.stdout_lines |
| 101 | +- name: Load generated content if any |
| 102 | + block: |
| 103 | + - name: Check if we have a file |
| 104 | + register: hook_callback |
| 105 | + ansible.builtin.stat: |
| 106 | + path: "{{ cifmw_basedir }}/artifacts/{{ step }}_{{ hook_name }}.yml" |
| 107 | + - name: Load generated content in main playbook |
| 108 | + when: |
| 109 | + hook_callback.stat.exists |
| 110 | + ansible.builtin.include_vars: |
| 111 | + file: "{{ cifmw_basedir }}/artifacts/{{ step }}_{{ hook_name }}.yml" |
0 commit comments