| 1 |
# Test code for integration of until and loop options |
| 2 |
# Copyright: (c) 2018, Ansible Project |
| 3 |
|
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
- shell: '{{ ansible_python.executable }} -c "import tempfile; print(tempfile.mkstemp()[1])"' |
| 19 |
register: tempfilepaths |
| 20 |
# 0 to 3: |
| 21 |
loop: "{{ range(0, 3 + 1) | list }}" |
| 22 |
|
| 23 |
- set_fact: |
| 24 |
"until_tempfile_path_{{ idx }}": "{{ tmp_file.stdout }}" |
| 25 |
until_tempfile_path_var_names: > |
| 26 |
{{ [ 'until_tempfile_path_' + idx | string ] + until_tempfile_path_var_names | default([]) }} |
| 27 |
loop: "{{ tempfilepaths.results }}" |
| 28 |
loop_control: |
| 29 |
index_var: idx |
| 30 |
loop_var: tmp_file |
| 31 |
|
| 32 |
- name: loop and until with 6 retries |
| 33 |
shell: echo "run" >> {{ lookup('vars', tmp_file_var) }} && wc -w < {{ lookup('vars', tmp_file_var) }} | tr -d ' ' |
| 34 |
register: runcount |
| 35 |
until: runcount.stdout | int == idx + 3 |
| 36 |
retries: "{{ idx + 2 }}" |
| 37 |
delay: 0.01 |
| 38 |
loop: "{{ until_tempfile_path_var_names }}" |
| 39 |
loop_control: |
| 40 |
index_var: idx |
| 41 |
loop_var: tmp_file_var |
| 42 |
|
| 43 |
- debug: var=runcount |
| 44 |
|
| 45 |
- assert: |
| 46 |
that: item.stdout | int == idx + 3 |
| 47 |
loop: "{{ runcount.results }}" |
| 48 |
loop_control: |
| 49 |
index_var: idx |
| 50 |
|
| 51 |
- &cleanup-tmp-files |
| 52 |
name: Empty tmp files |
| 53 |
copy: |
| 54 |
content: "" |
| 55 |
dest: "{{ lookup('vars', tmp_file_var) }}" |
| 56 |
loop: "{{ until_tempfile_path_var_names }}" |
| 57 |
loop_control: |
| 58 |
index_var: idx |
| 59 |
loop_var: tmp_file_var |
| 60 |
|
| 61 |
- name: loop with specified max retries |
| 62 |
shell: echo "run" >> {{ lookup('vars', tmp_file_var) }} |
| 63 |
until: 1==0 |
| 64 |
retries: 5 |
| 65 |
delay: 0.01 |
| 66 |
ignore_errors: true |
| 67 |
loop: "{{ until_tempfile_path_var_names }}" |
| 68 |
loop_control: |
| 69 |
index_var: idx |
| 70 |
loop_var: tmp_file_var |
| 71 |
|
| 72 |
- name: validate output |
| 73 |
shell: wc -l < {{ lookup('vars', tmp_file_var) }} |
| 74 |
register: runcount |
| 75 |
loop: "{{ until_tempfile_path_var_names }}" |
| 76 |
loop_control: |
| 77 |
index_var: idx |
| 78 |
loop_var: tmp_file_var |
| 79 |
|
| 80 |
- assert: |
| 81 |
that: item.stdout | int == 6 # initial + 5 retries |
| 82 |
loop: "{{ runcount.results }}" |
| 83 |
|
| 84 |
- *cleanup-tmp-files |
| 85 |
|
| 86 |
- name: Test failed_when impacting until |
| 87 |
shell: echo "run" >> {{ lookup('vars', tmp_file_var) }} |
| 88 |
register: failed_when_until |
| 89 |
failed_when: True |
| 90 |
until: failed_when_until is successful |
| 91 |
retries: 3 |
| 92 |
delay: 0.5 |
| 93 |
ignore_errors: True |
| 94 |
loop: "{{ until_tempfile_path_var_names }}" |
| 95 |
loop_control: |
| 96 |
index_var: idx |
| 97 |
loop_var: tmp_file_var |
| 98 |
|
| 99 |
- name: Get attempts number |
| 100 |
shell: wc -l < {{ lookup('vars', tmp_file_var) }} |
| 101 |
register: runcount |
| 102 |
loop: "{{ until_tempfile_path_var_names }}" |
| 103 |
loop_control: |
| 104 |
index_var: idx |
| 105 |
loop_var: tmp_file_var |
| 106 |
|
| 107 |
- assert: |
| 108 |
that: item.stdout | int == 3 + 1 |
| 109 |
loop: "{{ runcount.results }}" |
| 110 |
|
| 111 |
- *cleanup-tmp-files |
| 112 |
|
| 113 |
- name: Test changed_when impacting until |
| 114 |
shell: echo "run" >> {{ lookup('vars', tmp_file_var) }} |
| 115 |
register: changed_when_until |
| 116 |
changed_when: False |
| 117 |
until: changed_when_until is changed |
| 118 |
retries: 3 |
| 119 |
delay: 0.5 |
| 120 |
ignore_errors: True |
| 121 |
loop: "{{ until_tempfile_path_var_names }}" |
| 122 |
loop_control: |
| 123 |
index_var: idx |
| 124 |
loop_var: tmp_file_var |
| 125 |
|
| 126 |
- name: Get attempts number |
| 127 |
shell: wc -l < {{ lookup('vars', tmp_file_var) }} |
| 128 |
register: runcount |
| 129 |
loop: "{{ until_tempfile_path_var_names }}" |
| 130 |
loop_control: |
| 131 |
index_var: idx |
| 132 |
loop_var: tmp_file_var |
| 133 |
|
| 134 |
- assert: |
| 135 |
that: item.stdout | int == 3 + 1 |
| 136 |
loop: "{{ runcount.results }}" |
| 137 |
|
| 138 |
- *cleanup-tmp-files |
| 139 |
|
| 140 |
- name: Test access to attempts in changed_when/failed_when |
| 141 |
shell: 'true' |
| 142 |
register: changed_when_attempts |
| 143 |
until: 1 == 0 |
| 144 |
retries: 5 |
| 145 |
delay: 0.5 |
| 146 |
failed_when: changed_when_attempts.attempts > 6 |
| 147 |
loop: "{{ runcount.results }}" |
| 148 |
|
| 149 |
- &wipe-out-tmp-files |
| 150 |
file: path="{{ lookup('vars', tmp_file_var) }}" state=absent |
| 151 |
loop: "{{ until_tempfile_path_var_names }}" |
| 152 |
loop_control: |
| 153 |
index_var: idx |
| 154 |
loop_var: tmp_file_var |