summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/iterators/tasks/main.yml
blob: 584144af77128ee2a5c59a1934cc0bed07083eb8 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# test code for iterating with lookup plugins
# (c) 2014, James Tanner <tanner.jc@gmail.com>

# 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 <http://www.gnu.org/licenses/>.

# WITH_ITEMS

- name: test with_items
  set_fact: "{{ item }}=moo"
  with_items:
    - 'foo'
    - 'bar'

- debug: var=foo
- debug: var=bar

- name: verify with_items results
  assert:
    that:
        - "foo == 'moo'"
        - "bar == 'moo'"

# WITH_NESTED

- name: test with_nested
  set_fact: "{{ item.0 + item.1 }}=x"
  with_nested:
    - [ 'a', 'b' ]
    - [ 'c', 'd' ]

- debug: var=ac
- debug: var=ad
- debug: var=bc
- debug: var=bd

- name: verify with_nested results
  assert:
    that:
        - "ac == 'x'"
        - "ad == 'x'"
        - "bc == 'x'"
        - "bd == 'x'"

# WITH_SEQUENCE

- name: test with_sequence
  set_fact: "{{ 'x' + item }}={{ item }}"
  with_sequence: start=0 end=3

- name: test with_sequence backwards
  set_fact: "{{ 'y' + item }}={{ item }}"
  with_sequence: start=3 end=0 stride=-1

- name: verify with_sequence
  assert:
    that:
        - "x0 == '0'"
        - "x1 == '1'"
        - "x2 == '2'"
        - "x3 == '3'"
        - "y3 == '3'"
        - "y2 == '2'"
        - "y1 == '1'"
        - "y0 == '0'"

- name: test with_sequence not failing on count == 0
  debug: msg='previously failed with backward counting error'
  with_sequence: count=0
  register: count_of_zero

- name: test with_sequence does 1 when start == end
  debug: msg='should run once'
  with_sequence: start=1 end=1
  register: start_equal_end

- name: test with_sequence count 1
  set_fact: "{{ 'x' + item }}={{ item }}"
  with_sequence: count=1
  register: count_of_one

- assert:
    that:
        - not start_equal_end| skipped
        - count_of_zero | skipped
        - not count_of_one | skipped

# WITH_RANDOM_CHOICE

- name: test with_random_choice
  set_fact: "random={{ item }}"
  with_random_choice:
    - "foo"
    - "bar"

- name: verify with_random_choice
  assert:
    that:
        - "random in ['foo', 'bar']"

# WITH_SUBELEMENTS

- name: test with_subelements
  set_fact: "{{ '_'+ item.0.id + item.1 }}={{ item.1 }}"
  with_subelements:
    - "{{element_data}}"
    - the_list

- name: verify with_subelements results
  assert:
    that:
        - "_xf == 'f'"
        - "_xd == 'd'"
        - "_ye == 'e'"
        - "_yf == 'f'"

- name: test with_subelements in subkeys
  set_fact: "{{ '_'+ item.0.id + item.1 }}={{ item.1 }}"
  with_subelements:
    - "{{element_data}}"
    - the.sub.key.list

- name: verify with_subelements in subkeys results
  assert:
    that:
        - "_xq == 'q'"
        - "_xr == 'r'"
        - "_yi == 'i'"
        - "_yo == 'o'"

- name: test with_subelements with missing key or subkey
  set_fact: "{{ '_'+ item.0.id + item.1 }}={{ item.1 }}"
  with_subelements:
    - "{{element_data_missing}}"
    - the.sub.key.list
    - skip_missing: yes
  register: _subelements_missing_subkeys

- debug: var=_subelements_missing_subkeys
- debug: var=_subelements_missing_subkeys.results|length
- name: verify with_subelements in subkeys results
  assert:
    that:
        - _subelements_missing_subkeys.skipped is not defined
        - _subelements_missing_subkeys.results|length == 2
        - "_xk == 'k'"
        - "_xl == 'l'"


# WITH_TOGETHER        

- name: test with_together
  #shell: echo {{ item }}
  set_fact: "{{ item.0 }}={{ item.1 }}"
  with_together:
    - [ 'a', 'b', 'c', 'd' ]
    - [ '1', '2', '3', '4' ]

- name: verify with_together results
  assert:
    that:
        - "a == '1'"
        - "b == '2'"
        - "c == '3'"
        - "d == '4'"

# WITH_FIRST_FOUND

- name: create file for test
  shell: echo "foo" > {{ output_dir }}/foo1

- name: create file for test
  shell: echo "bar" > {{ output_dir }}/bar1

- name: test with_first_found
  #shell: echo {{ item }}
  set_fact: "first_found={{ item }}"
  with_first_found:
    - "{{ output_dir + '/does_not_exist' }}"
    - "{{ output_dir + '/foo1' }}"
    - "{{ output_dir + '/bar1' }}"

- name: set expected
  set_fact: first_expected="{{ output_dir | expanduser + '/foo1' }}"

- name: set unexpected
  set_fact: first_unexpected="{{ output_dir | expanduser + '/bar1' }}"

- name: verify with_first_found results
  assert:
    that:
        - "first_found == first_expected"  
        - "first_found != first_unexpected"

# WITH_LINES

- name: test with_lines
  #shell: echo "{{ item }}"
  set_fact: "{{ item }}=set" 
  with_lines: for i in $(seq 1 5); do echo "l$i" ; done;

- name: verify with_lines results
  assert:
    that:
        - "l1 == 'set'"
        - "l2 == 'set'"
        - "l3 == 'set'"
        - "l4 == 'set'"
        - "l5 == 'set'"

# WITH_INDEX
- name: create unindexed list
  shell: for i in $(seq 1 5); do echo "x" ; done;
  register: list_data

- name: create indexed list
  set_fact: "{{ item[1] + item[0]|string }}=set"
  with_indexed_items: "{{list_data.stdout_lines}}"

- name: verify with_indexed_items result
  assert:
    that:
        - "x0 == 'set'"
        - "x1 == 'set'"
        - "x2 == 'set'"
        - "x3 == 'set'"
        - "x4 == 'set'"

# WITH_FLATTENED

- name: test with_flattened
  set_fact: "{{ item }}=flattened"
  with_flattened:
    - [ 'a__' ]
    - [ 'b__', ['c__', 'd__'] ]        

- name: verify with_flattened results
  assert:
    that:
        - "a__ == 'flattened'"
        - "b__ == 'flattened'"
        - "c__ == 'flattened'"
        - "d__ == 'flattened'"