2014-02-21 19:26:34 +00:00
|
|
|
# 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
|
2014-07-21 16:20:49 +00:00
|
|
|
set_fact: "{{ item }}=moo"
|
2014-02-21 19:26:34 +00:00
|
|
|
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
|
2014-07-21 16:20:49 +00:00
|
|
|
set_fact: "{{ item.0 + item.1 }}=x"
|
2014-02-21 19:26:34 +00:00
|
|
|
with_nested:
|
|
|
|
- [ 'a', 'b' ]
|
2015-05-12 17:51:35 +00:00
|
|
|
- [ 'c', 'd' ]
|
2014-02-21 19:26:34 +00:00
|
|
|
|
|
|
|
- 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
|
2014-07-21 16:20:49 +00:00
|
|
|
set_fact: "{{ 'x' + item }}={{ item }}"
|
2014-02-21 19:26:34 +00:00
|
|
|
with_sequence: start=0 end=3
|
|
|
|
|
2015-03-31 00:34:17 +00:00
|
|
|
- name: test with_sequence backwards
|
|
|
|
set_fact: "{{ 'y' + item }}={{ item }}"
|
|
|
|
with_sequence: start=3 end=0 stride=-1
|
|
|
|
|
2014-02-21 19:26:34 +00:00
|
|
|
- name: verify with_sequence
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "x0 == '0'"
|
|
|
|
- "x1 == '1'"
|
|
|
|
- "x2 == '2'"
|
|
|
|
- "x3 == '3'"
|
2015-03-31 00:34:17 +00:00
|
|
|
- "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
|
|
|
|
|
2015-08-18 06:33:23 +00:00
|
|
|
- name: test with_sequence does 1 when start == end
|
|
|
|
debug: msg='should run once'
|
|
|
|
with_sequence: start=1 end=1
|
|
|
|
register: start_equal_end
|
|
|
|
|
2015-07-03 17:54:31 +00:00
|
|
|
- name: test with_sequence count 1
|
|
|
|
set_fact: "{{ 'x' + item }}={{ item }}"
|
|
|
|
with_sequence: count=1
|
|
|
|
register: count_of_one
|
|
|
|
|
2015-03-31 00:34:17 +00:00
|
|
|
- assert:
|
|
|
|
that:
|
2015-08-18 06:33:23 +00:00
|
|
|
- not start_equal_end| skipped
|
2015-03-31 00:34:17 +00:00
|
|
|
- count_of_zero | skipped
|
2015-07-03 17:54:31 +00:00
|
|
|
- not count_of_one | skipped
|
2014-02-21 19:26:34 +00:00
|
|
|
|
2017-03-29 23:11:19 +00:00
|
|
|
- name: test with_sequence shortcut syntax (end)
|
|
|
|
set_fact: "{{ 'ws_z_' + item }}={{ item }}"
|
|
|
|
with_sequence: '4'
|
|
|
|
|
|
|
|
- name: test with_sequence shortcut syntax (start-end/stride)
|
|
|
|
set_fact: "{{ 'ws_z_' + item }}=stride_{{ item }}"
|
|
|
|
with_sequence: '2-6/2'
|
|
|
|
|
|
|
|
- name: test with_sequence shortcut syntax (start-end:format)
|
|
|
|
set_fact: "{{ 'ws_z_' + item }}={{ item }}"
|
|
|
|
with_sequence: '7-8:host%02d'
|
|
|
|
|
|
|
|
- name: verify with_sequence shortcut syntax
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "ws_z_1 == '1'"
|
|
|
|
- "ws_z_2 == 'stride_2'"
|
|
|
|
- "ws_z_3 == '3'"
|
|
|
|
- "ws_z_4 == 'stride_4'"
|
|
|
|
- "ws_z_6 == 'stride_6'"
|
|
|
|
- "ws_z_host07 == 'host07'"
|
|
|
|
- "ws_z_host08 == 'host08'"
|
|
|
|
|
2014-02-21 19:26:34 +00:00
|
|
|
# WITH_RANDOM_CHOICE
|
|
|
|
|
|
|
|
- name: test with_random_choice
|
2014-07-21 16:20:49 +00:00
|
|
|
set_fact: "random={{ item }}"
|
2014-02-21 19:26:34 +00:00
|
|
|
with_random_choice:
|
|
|
|
- "foo"
|
2015-08-18 06:33:23 +00:00
|
|
|
- "bar"
|
2014-02-21 19:26:34 +00:00
|
|
|
|
|
|
|
- name: verify with_random_choice
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "random in ['foo', 'bar']"
|
|
|
|
|
|
|
|
# WITH_SUBELEMENTS
|
|
|
|
|
|
|
|
- name: test with_subelements
|
2014-07-21 16:20:49 +00:00
|
|
|
set_fact: "{{ '_'+ item.0.id + item.1 }}={{ item.1 }}"
|
2014-02-21 19:26:34 +00:00
|
|
|
with_subelements:
|
2016-09-15 21:41:54 +00:00
|
|
|
- "{{element_data}}"
|
2014-02-21 19:26:34 +00:00
|
|
|
- the_list
|
|
|
|
|
|
|
|
- name: verify with_subelements results
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "_xf == 'f'"
|
|
|
|
- "_xd == 'd'"
|
|
|
|
- "_ye == 'e'"
|
|
|
|
- "_yf == 'f'"
|
|
|
|
|
2015-05-12 17:51:35 +00:00
|
|
|
- name: test with_subelements in subkeys
|
|
|
|
set_fact: "{{ '_'+ item.0.id + item.1 }}={{ item.1 }}"
|
|
|
|
with_subelements:
|
2016-09-15 21:41:54 +00:00
|
|
|
- "{{element_data}}"
|
2015-05-12 17:51:35 +00:00
|
|
|
- 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:
|
2016-09-15 21:41:54 +00:00
|
|
|
- "{{element_data_missing}}"
|
2015-05-12 17:51:35 +00:00
|
|
|
- the.sub.key.list
|
|
|
|
- skip_missing: yes
|
|
|
|
register: _subelements_missing_subkeys
|
|
|
|
|
2015-05-12 20:09:03 +00:00
|
|
|
- debug: var=_subelements_missing_subkeys
|
2015-05-12 17:51:35 +00:00
|
|
|
- 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'"
|
|
|
|
|
|
|
|
|
2017-03-29 23:11:19 +00:00
|
|
|
# WITH_TOGETHER
|
2014-02-21 19:26:34 +00:00
|
|
|
|
|
|
|
- name: test with_together
|
|
|
|
#shell: echo {{ item }}
|
2014-07-21 16:20:49 +00:00
|
|
|
set_fact: "{{ item.0 }}={{ item.1 }}"
|
2014-02-21 19:26:34 +00:00
|
|
|
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 }}
|
2014-07-21 16:20:49 +00:00
|
|
|
set_fact: "first_found={{ item }}"
|
2014-02-21 19:26:34 +00:00
|
|
|
with_first_found:
|
|
|
|
- "{{ output_dir + '/does_not_exist' }}"
|
|
|
|
- "{{ output_dir + '/foo1' }}"
|
|
|
|
- "{{ output_dir + '/bar1' }}"
|
|
|
|
|
|
|
|
- name: set expected
|
2017-07-03 19:27:53 +00:00
|
|
|
set_fact: first_expected="{{ output_dir | expanduser + '/foo1' }}"
|
2014-02-21 19:26:34 +00:00
|
|
|
|
|
|
|
- name: set unexpected
|
2017-07-03 19:27:53 +00:00
|
|
|
set_fact: first_unexpected="{{ output_dir | expanduser + '/bar1' }}"
|
2014-02-21 19:26:34 +00:00
|
|
|
|
|
|
|
- name: verify with_first_found results
|
|
|
|
assert:
|
|
|
|
that:
|
2017-03-29 23:11:19 +00:00
|
|
|
- "first_found == first_expected"
|
2014-02-21 19:26:34 +00:00
|
|
|
- "first_found != first_unexpected"
|
|
|
|
|
|
|
|
# WITH_LINES
|
|
|
|
|
|
|
|
- name: test with_lines
|
|
|
|
#shell: echo "{{ item }}"
|
2017-03-29 23:11:19 +00:00
|
|
|
set_fact: "{{ item }}=set"
|
2014-02-21 19:26:34 +00:00
|
|
|
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
|
2014-07-21 16:20:49 +00:00
|
|
|
set_fact: "{{ item[1] + item[0]|string }}=set"
|
2016-09-15 21:41:54 +00:00
|
|
|
with_indexed_items: "{{list_data.stdout_lines}}"
|
2014-02-21 19:26:34 +00:00
|
|
|
|
|
|
|
- name: verify with_indexed_items result
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "x0 == 'set'"
|
|
|
|
- "x1 == 'set'"
|
|
|
|
- "x2 == 'set'"
|
|
|
|
- "x3 == 'set'"
|
|
|
|
- "x4 == 'set'"
|
|
|
|
|
|
|
|
# WITH_FLATTENED
|
|
|
|
|
|
|
|
- name: test with_flattened
|
2014-07-21 16:20:49 +00:00
|
|
|
set_fact: "{{ item }}=flattened"
|
2014-02-21 19:26:34 +00:00
|
|
|
with_flattened:
|
|
|
|
- [ 'a__' ]
|
2017-03-29 23:11:19 +00:00
|
|
|
- [ 'b__', ['c__', 'd__'] ]
|
2014-02-21 19:26:34 +00:00
|
|
|
|
|
|
|
- name: verify with_flattened results
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "a__ == 'flattened'"
|
|
|
|
- "b__ == 'flattened'"
|
|
|
|
- "c__ == 'flattened'"
|
|
|
|
- "d__ == 'flattened'"
|