Skip to content

Commit 7527d0c

Browse files
cjeanneropenshift-merge-bot[bot]
authored andcommitted
Improve run_hook role UX
This change should ensure running playbook hook will display things properly: - use `realpath` filter for the playbook path - move some constraint tests at the top to ensure early failure - display log path before running the hook, in order to allow easy follow - remove outputs since we have the dedicated file - remove some useless debugging outputs - better hook name sanitizer to avoid unwanted chars
1 parent ae8045d commit 7527d0c

File tree

1 file changed

+73
-66
lines changed

1 file changed

+73
-66
lines changed

roles/run_hook/tasks/playbook.yml

Lines changed: 73 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
---
22
- 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: >-
77
{%- if hook.source is not ansible.builtin.abs -%}
88
{{ role_path }}/../../hooks/playbooks/{{ hook.source }}
99
{%- else -%}
1010
{{ hook.source }}
1111
{%- 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
1220
extra_vars: >-
1321
{%- if hook.extra_vars is defined and hook.extra_vars|length > 0 -%}
1422
{% for key,value in hook.extra_vars.items() -%}
@@ -20,85 +28,84 @@
2028
{%- endfor %}
2129
{%- endif %}
2230
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+
2343
- name: Get parameters files
2444
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+
}}
2649
file_type: file
2750
patterns: '*.yml'
2851
register: cifmw_run_hook_parameters_files
2952

30-
- name: "Add parameters artifacts as extra variables" # noqa jinja[spacing]
53+
- name: "Add parameters artifacts as extra variables"
3154
ansible.builtin.set_fact:
3255
extra_vars: >-
3356
{{ extra_vars }}
3457
{% for file in cifmw_run_hook_parameters_files.files %}
3558
-e "@{{ file.path }}"
3659
{%- endfor %}
3760
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"
4166

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 }}"
5376

5477
# We cannot call ansible.builtin.import_playbook from within a play,
5578
# even less from a task. So the way to run a playbook from within a playbook
5679
# is to call a command. Though we may lose some of the data passed to the
5780
# "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 }}
101100
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

Comments
 (0)