summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/loop-until/tasks/main.yml
blob: bb3799a30f5733a705a667b72c5c68c0440e298d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Test code for integration of until and loop options
# Copyright: (c) 2018, Ansible Project

# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <https://www.gnu.org/licenses/>.
- shell: '{{ ansible_python.executable }} -c "import tempfile; print(tempfile.mkstemp()[1])"'
  register: tempfilepaths
  # 0 to 3:
  loop: "{{ range(0, 3 + 1) | list }}"

- set_fact:
    "until_tempfile_path_{{ idx }}": "{{ tmp_file.stdout }}"
    until_tempfile_path_var_names: >
        {{ [ 'until_tempfile_path_' + idx | string ] + until_tempfile_path_var_names | default([]) }}
  loop: "{{ tempfilepaths.results }}"
  loop_control:
    index_var: idx
    loop_var: tmp_file

# `select` filter is only available since Jinja 2.7,
# thus tests are failing under CentOS in CI
#- set_fact:
#    until_tempfile_path_var_names: >
#      {{ vars | select('match', '^until_tempfile_path_') | list }}

- name: loop and until with 6 retries
  shell: echo "run" >> {{ lookup('vars', tmp_file_var) }} && wc -w < {{ lookup('vars', tmp_file_var) }} | tr -d ' '
  register: runcount
  until: runcount.stdout | int == idx + 3
  retries: "{{ idx + 2 }}"
  delay: 0.01
  loop: "{{ until_tempfile_path_var_names }}"
  loop_control:
    index_var: idx
    loop_var: tmp_file_var

- debug: var=runcount

- assert:
    that: item.stdout | int == idx + 3
  loop: "{{ runcount.results }}"
  loop_control:
    index_var: idx

- &cleanup-tmp-files
  name: Empty tmp files
  copy:
    content: ""
    dest: "{{ lookup('vars', tmp_file_var) }}"
  loop: "{{ until_tempfile_path_var_names }}"
  loop_control:
    index_var: idx
    loop_var: tmp_file_var

- name: loop with specified max retries
  shell: echo "run" >> {{ lookup('vars', tmp_file_var) }}
  until: 1==0
  retries: 5
  delay: 0.01
  ignore_errors: true
  loop: "{{ until_tempfile_path_var_names }}"
  loop_control:
    index_var: idx
    loop_var: tmp_file_var

- name: validate output
  shell: wc -l < {{ lookup('vars', tmp_file_var) }}
  register: runcount
  loop: "{{ until_tempfile_path_var_names }}"
  loop_control:
    index_var: idx
    loop_var: tmp_file_var

- assert:
    that: item.stdout | int == 6  # initial + 5 retries
  loop: "{{ runcount.results }}"

- *cleanup-tmp-files

- name: Test failed_when impacting until
  shell: echo "run" >> {{ lookup('vars', tmp_file_var) }}
  register: failed_when_until
  failed_when: True
  until: failed_when_until is successful
  retries: 3
  delay: 0.5
  ignore_errors: True
  loop: "{{ until_tempfile_path_var_names }}"
  loop_control:
    index_var: idx
    loop_var: tmp_file_var

- name: Get attempts number
  shell: wc -l < {{ lookup('vars', tmp_file_var) }}
  register: runcount
  loop: "{{ until_tempfile_path_var_names }}"
  loop_control:
    index_var: idx
    loop_var: tmp_file_var

- assert:
    that: item.stdout | int == 3 + 1
  loop: "{{ runcount.results }}"

- *cleanup-tmp-files

- name: Test changed_when impacting until
  shell: echo "run" >> {{ lookup('vars', tmp_file_var) }}
  register: changed_when_until
  changed_when: False
  until: changed_when_until is changed
  retries: 3
  delay: 0.5
  ignore_errors: True
  loop: "{{ until_tempfile_path_var_names }}"
  loop_control:
    index_var: idx
    loop_var: tmp_file_var

- name: Get attempts number
  shell: wc -l < {{ lookup('vars', tmp_file_var) }}
  register: runcount
  loop: "{{ until_tempfile_path_var_names }}"
  loop_control:
    index_var: idx
    loop_var: tmp_file_var

- assert:
    that: item.stdout | int == 3 + 1
  loop: "{{ runcount.results }}"

- *cleanup-tmp-files

- name: Test access to attempts in changed_when/failed_when
  shell: 'true'
  register: changed_when_attempts
  until: 1 == 0
  retries: 5
  delay: 0.5
  failed_when: changed_when_attempts.attempts > 6
  loop: "{{ runcount.results }}"

- &wipe-out-tmp-files
  file: path="{{ lookup('vars', tmp_file_var) }}" state=absent
  loop: "{{ until_tempfile_path_var_names }}"
  loop_control:
    index_var: idx
    loop_var: tmp_file_var