Skip to content

Commit 72d5b66

Browse files
danpawlikamartyasinha
authored andcommitted
Add hook_retry parameter for run_hook role
Sometimes, some hooks fails like on downloading tools. Let's add a new parameter: hook_retry that will make a retry on those hooks, where the parameter `hook_retry` is set to true. Signed-off-by: Daniel Pawlik <[email protected]>
1 parent 423b9b6 commit 72d5b66

File tree

6 files changed

+94
-1
lines changed

6 files changed

+94
-1
lines changed

roles/run_hook/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ name:
3737
* `source`: (String) Source of the playbook. If it's a filename, the playbook is expected in `hooks/playbooks`. It can be an absolute path.
3838
* `type`: (String) Type of the hook. In this case, set it to `playbook`.
3939
* `extra_vars`: (Dict) Structure listing the extra variables you would like to pass down ([extra_vars explained](#extra_vars-explained))
40+
* `hook_retry` (Boolean) Set true, if the hook execution should be retried on failure
4041

4142
##### About OpenShift namespaces and install_yamls
4243

@@ -55,6 +56,7 @@ Since `install_yamls` might not be initialized, the `run_hook` is exposing two n
5556
* `source`: (String) Source of the playbook. If it's a filename, the playbook is expected in `hooks/playbooks`. It can be an absolute path.
5657
* `type`: (String) Type of the hook. In this case, set it to `playbook`.
5758
* `extra_vars`: (Dict) Structure listing the extra variables you would like to pass down ([extra_vars explained](#extra_vars-explained))
59+
* `hook_retry` (Boolean) Set true, if the hook execution should be retried on failure
5860

5961
#### Hook callback
6062

roles/run_hook/molecule/default/converge.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,26 @@
103103
that:
104104
- test_list is defined
105105
- test_list | length == 2
106+
107+
- name: Hooks with retry
108+
block:
109+
- name: Run hook with retry
110+
vars:
111+
step: retry_hook
112+
ansible.builtin.include_role:
113+
name: run_hook
114+
115+
- name: Check if fake file exists for retry playbook
116+
ansible.builtin.stat:
117+
path: /tmp/molecule-retry-fake-file
118+
register: _molecule_fake_file
119+
120+
- name: Ensure file exists and was created on retry
121+
ansible.builtin.assert:
122+
that:
123+
- _molecule_fake_file.stat.exists
124+
always:
125+
- name: Remove generated file
126+
ansible.builtin.file:
127+
path: /tmp/molecule-retry-fake-file
128+
state: absent

roles/run_hook/molecule/default/molecule.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,9 @@ provisioner:
5050
extra_vars:
5151
foo: bar
5252
file: "/tmp/dummy-env.yml"
53+
54+
retry_hook:
55+
- name: Run hook with retry
56+
source: "/tmp/dummy-retry.yml"
57+
type: playbook
58+
retry_hook: true

roles/run_hook/molecule/default/prepare.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,14 @@
4242
- dummy-4.yml
4343
- dummy-5.yml
4444
- dummy-6.yml
45+
46+
- name: Remove dummy file for retry playbook test
47+
ansible.builtin.file:
48+
path: /tmp/molecule-retry-fake-file
49+
state: absent
50+
51+
- name: Create dummy retry playbook
52+
ansible.builtin.template:
53+
dest: "/tmp/dummy-retry.yml"
54+
src: "dummy-retry.yml.j2"
55+
mode: "0644"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
- hosts: localhost
3+
gather_facts: true
4+
tasks:
5+
{% raw %}
6+
- name: Check if fake file exists
7+
ansible.builtin.stat:
8+
path: /tmp/molecule-retry-fake-file
9+
register: _molecule_fake_file
10+
11+
- name: Create a file, if it does not exists
12+
when: not _molecule_fake_file.stat.exists
13+
ansible.builtin.file:
14+
path: /tmp/molecule-retry-fake-file
15+
state: touch
16+
17+
- name: Finish if file does not exists
18+
when: not _molecule_fake_file.stat.exists
19+
ansible.builtin.meta: end_play
20+
21+
- name: Print Hello world if file exists
22+
when: _molecule_fake_file.stat.exists
23+
ansible.builtin.debug:
24+
msg: 'Hello retry world'
25+
{% endraw %}

roles/run_hook/tasks/playbook.yml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@
8989
# even less from a task. So the way to run a playbook from within a playbook
9090
# is to call a command. Though we may lose some of the data passed to the
9191
# "main" play.
92-
- name: "Run {{ hook.name }}"
92+
- name: "Run hook without retry - {{ hook.name }}"
93+
when: not hook.hook_retry | default(false)
9394
no_log: "{{ cifmw_nolog | default(true) | bool }}"
9495
cifmw.general.ci_script:
9596
output_dir: "{{ cifmw_basedir }}/artifacts"
@@ -109,6 +110,31 @@
109110
-e "playbook_dir={{ playbook_path | dirname }}"
110111
{{ playbook_path }}
111112
113+
- name: "Run hook with retry - {{ hook.name }}"
114+
when: hook.hook_retry | default(false)
115+
no_log: "{{ cifmw_nolog | default(true) | bool }}"
116+
cifmw.general.ci_script:
117+
output_dir: "{{ cifmw_basedir }}/artifacts"
118+
extra_args:
119+
ANSIBLE_CONFIG: "{{ hook.config_file | default(ansible_config_file) }}"
120+
ANSIBLE_LOG_PATH: "{{ log_path }}"
121+
creates: "{{ hook.creates | default(omit) }}"
122+
script: >-
123+
ansible-playbook -i {{ hook.inventory | default(inventory_file) }}
124+
{% if hook.connection is defined -%}
125+
-c {{ hook.connection }}
126+
{% endif -%}
127+
{{ extra_vars }}
128+
-e "cifmw_basedir={{ cifmw_basedir }}"
129+
-e "step={{ step }}"
130+
-e "hook_name={{ hook_name }}"
131+
-e "playbook_dir={{ playbook_path | dirname }}"
132+
{{ playbook_path }}
133+
register: hook_result
134+
retries: 3
135+
delay: 10
136+
until: hook_result is not failed
137+
112138
- name: Load generated content if any
113139
block:
114140
- name: Check if we have a file

0 commit comments

Comments
 (0)