2018-01-26 21:29:04 +00:00
|
|
|
#
|
|
|
|
# loop_control/pause
|
|
|
|
#
|
|
|
|
|
2016-09-13 18:12:58 +00:00
|
|
|
- name: Measure time before
|
|
|
|
shell: date +%s
|
|
|
|
register: before
|
|
|
|
|
|
|
|
- debug:
|
|
|
|
var: i
|
|
|
|
with_sequence: count=3
|
|
|
|
loop_control:
|
|
|
|
loop_var: i
|
|
|
|
pause: 2
|
|
|
|
|
|
|
|
- name: Measure time after
|
|
|
|
shell: date +%s
|
|
|
|
register: after
|
|
|
|
|
|
|
|
# since there is 3 rounds, and 2 seconds between, it should last 4 seconds
|
|
|
|
# we do not test the upper bound, since CI can lag significantly
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- '(after.stdout |int) - (before.stdout|int) >= 4'
|
2018-01-30 22:46:14 +00:00
|
|
|
|
2019-01-19 03:41:29 +00:00
|
|
|
- name: test subsecond pause
|
|
|
|
block:
|
|
|
|
- name: Measure time before loop with .5s pause
|
|
|
|
set_fact:
|
|
|
|
times: "{{times|default([]) + [ lookup('pipe','date +%s.%3N') ]}}"
|
|
|
|
with_sequence: count=3
|
|
|
|
loop_control:
|
|
|
|
pause: 0.6
|
|
|
|
|
|
|
|
- name: ensure lag, since there is 3 rounds, and 0.5 seconds between, it should last 1.2 seconds, but allowing leeway due to CI lag
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- tdiff|float >= 1.2
|
|
|
|
- tdiff|int < 3
|
|
|
|
vars:
|
|
|
|
tdiff: '{{ times[2]|float - times[0]|float }}'
|
|
|
|
when:
|
|
|
|
- ansible_facts['distribution'] not in ("MacOSX", "FreeBSD")
|
|
|
|
|
2018-01-30 22:46:14 +00:00
|
|
|
#
|
|
|
|
# Tests of loop syntax with args
|
|
|
|
#
|
|
|
|
|
|
|
|
- name: Test that with_list works with a list
|
|
|
|
ping:
|
|
|
|
data: '{{ item }}'
|
|
|
|
with_list:
|
|
|
|
- 'Hello World'
|
|
|
|
- 'Olá Mundo'
|
|
|
|
register: results
|
|
|
|
|
|
|
|
- name: Assert that we ran the module twice with the correct strings
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- 'results["results"][0]["ping"] == "Hello World"'
|
|
|
|
- 'results["results"][1]["ping"] == "Olá Mundo"'
|
|
|
|
|
|
|
|
- name: Test that with_list works with a list inside a variable
|
|
|
|
ping:
|
|
|
|
data: '{{ item }}'
|
|
|
|
with_list: '{{ phrases }}'
|
|
|
|
register: results2
|
|
|
|
|
|
|
|
- name: Assert that we ran the module twice with the correct strings
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- 'results2["results"][0]["ping"] == "Hello World"'
|
|
|
|
- 'results2["results"][1]["ping"] == "Olá Mundo"'
|
|
|
|
|
|
|
|
- name: Test that loop works with a manual list
|
|
|
|
ping:
|
|
|
|
data: '{{ item }}'
|
|
|
|
loop:
|
|
|
|
- 'Hello World'
|
|
|
|
- 'Olá Mundo'
|
|
|
|
register: results3
|
|
|
|
|
|
|
|
- name: Assert that we ran the module twice with the correct strings
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- 'results3["results"][0]["ping"] == "Hello World"'
|
|
|
|
- 'results3["results"][1]["ping"] == "Olá Mundo"'
|
|
|
|
|
|
|
|
- name: Test that loop works with a list in a variable
|
|
|
|
ping:
|
|
|
|
data: '{{ item }}'
|
|
|
|
loop: '{{ phrases }}'
|
|
|
|
register: results4
|
|
|
|
|
|
|
|
- name: Assert that we ran the module twice with the correct strings
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- 'results4["results"][0]["ping"] == "Hello World"'
|
|
|
|
- 'results4["results"][1]["ping"] == "Olá Mundo"'
|
|
|
|
|
|
|
|
- name: Test that loop works with a list via the list lookup
|
|
|
|
ping:
|
|
|
|
data: '{{ item }}'
|
|
|
|
loop: '{{ lookup("list", "Hello World", "Olá Mundo", wantlist=True) }}'
|
|
|
|
register: results5
|
|
|
|
|
|
|
|
- name: Assert that we ran the module twice with the correct strings
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- 'results5["results"][0]["ping"] == "Hello World"'
|
|
|
|
- 'results5["results"][1]["ping"] == "Olá Mundo"'
|
|
|
|
|
|
|
|
- name: Test that loop works with a list in a variable via the list lookup
|
|
|
|
ping:
|
|
|
|
data: '{{ item }}'
|
|
|
|
loop: '{{ lookup("list", wantlist=True, *phrases) }}'
|
|
|
|
register: results6
|
|
|
|
|
|
|
|
- name: Assert that we ran the module twice with the correct strings
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- 'results6["results"][0]["ping"] == "Hello World"'
|
|
|
|
- 'results6["results"][1]["ping"] == "Olá Mundo"'
|
|
|
|
|
2018-02-21 16:25:56 +00:00
|
|
|
- name: Test that loop works with a list via the query lookup
|
|
|
|
ping:
|
|
|
|
data: '{{ item }}'
|
|
|
|
loop: '{{ query("list", "Hello World", "Olá Mundo") }}'
|
|
|
|
register: results7
|
|
|
|
|
|
|
|
- name: Assert that we ran the module twice with the correct strings
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- 'results7["results"][0]["ping"] == "Hello World"'
|
|
|
|
- 'results7["results"][1]["ping"] == "Olá Mundo"'
|
|
|
|
|
|
|
|
- name: Test that loop works with a list in a variable via the query lookup
|
|
|
|
ping:
|
|
|
|
data: '{{ item }}'
|
|
|
|
loop: '{{ q("list", *phrases) }}'
|
|
|
|
register: results8
|
|
|
|
|
|
|
|
- name: Assert that we ran the module twice with the correct strings
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- 'results8["results"][0]["ping"] == "Hello World"'
|
|
|
|
- 'results8["results"][1]["ping"] == "Olá Mundo"'
|
|
|
|
|
|
|
|
- name: Test that loop works with a list and keyword args
|
|
|
|
ping:
|
|
|
|
data: '{{ item }}'
|
|
|
|
loop: '{{ q("file", "data1.txt", "data2.txt", lstrip=True) }}'
|
|
|
|
register: results9
|
|
|
|
|
|
|
|
- name: Assert that we ran the module twice with the correct strings
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- 'results9["results"][0]["ping"] == "Hello World"'
|
|
|
|
- 'results9["results"][1]["ping"] == "Olá Mundo"'
|
|
|
|
|
|
|
|
- name: Test that loop works with a list in variable and keyword args
|
|
|
|
ping:
|
|
|
|
data: '{{ item }}'
|
|
|
|
loop: '{{ q("file", lstrip=True, *filenames) }}'
|
|
|
|
register: results10
|
|
|
|
|
|
|
|
- name: Assert that we ran the module twice with the correct strings
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- 'results10["results"][0]["ping"] == "Hello World"'
|
|
|
|
- 'results10["results"][1]["ping"] == "Olá Mundo"'
|
|
|
|
|
2018-01-26 21:29:04 +00:00
|
|
|
#
|
|
|
|
# loop_control/index_var
|
|
|
|
#
|
|
|
|
|
|
|
|
- name: check that the index var is created and increments as expected
|
|
|
|
assert:
|
|
|
|
that: my_idx == item|int
|
|
|
|
with_sequence: start=0 count=3
|
|
|
|
loop_control:
|
|
|
|
index_var: my_idx
|
|
|
|
|
|
|
|
- name: check that value of index var matches position of current item in source list
|
|
|
|
assert:
|
|
|
|
that: 'test_var.index(item) == my_idx'
|
|
|
|
vars:
|
|
|
|
test_var: ['a', 'b', 'c']
|
|
|
|
with_items: "{{ test_var }}"
|
|
|
|
loop_control:
|
|
|
|
index_var: my_idx
|
|
|
|
|
|
|
|
- name: check index var with included tasks file
|
|
|
|
include_tasks: index_var_tasks.yml
|
|
|
|
with_sequence: start=0 count=3
|
|
|
|
loop_control:
|
|
|
|
index_var: my_idx
|
2018-02-21 15:16:02 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# loop_control/label
|
|
|
|
# https://github.com/ansible/ansible/pull/36430
|
|
|
|
#
|
|
|
|
|
|
|
|
- set_fact:
|
|
|
|
loopthis:
|
|
|
|
- name: foo
|
|
|
|
label: foo_label
|
|
|
|
- name: bar
|
|
|
|
label: bar_label
|
|
|
|
|
|
|
|
- name: check that item label is updated each iteration
|
|
|
|
debug:
|
|
|
|
msg: "{{ looped_var.name }}"
|
|
|
|
with_items: "{{ loopthis }}"
|
|
|
|
loop_control:
|
|
|
|
loop_var: looped_var
|
|
|
|
label: "looped_var {{ looped_var.label }}"
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- "output.results[0]['_ansible_item_label'] == 'looped_var foo_label'"
|
|
|
|
- "output.results[1]['_ansible_item_label'] == 'looped_var bar_label'"
|
2018-05-17 20:29:36 +00:00
|
|
|
|
|
|
|
# The following test cases are to ensure that we don't have a regression on
|
|
|
|
# GitHub Issue https://github.com/ansible/ansible/issues/35481
|
|
|
|
#
|
|
|
|
# This should execute and not cause a RuntimeError
|
|
|
|
- debug:
|
|
|
|
msg: "with_dict passed a list: {{item}}"
|
|
|
|
with_dict: "{{ a_list }}"
|
|
|
|
register: with_dict_passed_a_list
|
|
|
|
ignore_errors: True
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- with_dict_passed_a_list is failed
|
|
|
|
- debug:
|
|
|
|
msg: "with_list passed a dict: {{item}}"
|
|
|
|
with_list: "{{ a_dict }}"
|
|
|
|
register: with_list_passed_a_dict
|
|
|
|
ignore_errors: True
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- with_list_passed_a_dict is failed
|
2018-07-31 15:51:32 +00:00
|
|
|
|
|
|
|
- debug:
|
|
|
|
var: "item"
|
|
|
|
loop:
|
|
|
|
- "{{ ansible_search_path }}"
|
|
|
|
register: loop_search_path
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- ansible_search_path == loop_search_path.results.0.item
|
2018-09-07 15:16:03 +00:00
|
|
|
|
|
|
|
# https://github.com/ansible/ansible/issues/45189
|
|
|
|
- name: with_X conditional delegate_to shortcircuit on templating error
|
|
|
|
debug:
|
|
|
|
msg: "loop"
|
|
|
|
when: false
|
|
|
|
delegate_to: localhost
|
|
|
|
with_list: "{{ fake_var }}"
|
|
|
|
register: result
|
|
|
|
failed_when: result is not skipped
|
|
|
|
|
|
|
|
- name: loop conditional delegate_to shortcircuit on templating error
|
|
|
|
debug:
|
|
|
|
msg: "loop"
|
|
|
|
when: false
|
|
|
|
delegate_to: localhost
|
|
|
|
loop: "{{ fake_var }}"
|
|
|
|
register: result
|
|
|
|
failed_when: result is not skipped
|
2018-10-16 18:35:10 +00:00
|
|
|
|
|
|
|
- name: Loop on literal empty list
|
|
|
|
debug:
|
|
|
|
loop: []
|
|
|
|
register: literal_empty_list
|
|
|
|
failed_when: literal_empty_list is not skipped
|
2018-10-23 16:18:21 +00:00
|
|
|
|
|
|
|
# https://github.com/ansible/ansible/issues/47372
|
|
|
|
- name: Loop unsafe list
|
|
|
|
debug:
|
|
|
|
var: item
|
|
|
|
with_items: "{{ things|list|unique }}"
|
|
|
|
vars:
|
|
|
|
things:
|
|
|
|
- !unsafe foo
|
|
|
|
- !unsafe bar
|
2018-12-07 19:49:50 +00:00
|
|
|
|
|
|
|
- name: extended loop info
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- ansible_loop.nextitem == 'orange'
|
|
|
|
- ansible_loop.index == 1
|
|
|
|
- ansible_loop.index0 == 0
|
|
|
|
- ansible_loop.first
|
|
|
|
- not ansible_loop.last
|
|
|
|
- ansible_loop.previtem is undefined
|
|
|
|
- ansible_loop.allitems == ['apple', 'orange', 'banana']
|
|
|
|
- ansible_loop.revindex == 3
|
|
|
|
- ansible_loop.revindex0 == 2
|
|
|
|
- ansible_loop.length == 3
|
|
|
|
loop:
|
|
|
|
- apple
|
|
|
|
- orange
|
|
|
|
- banana
|
|
|
|
loop_control:
|
|
|
|
extended: true
|
|
|
|
when: item == 'apple'
|
|
|
|
|
|
|
|
- name: extended loop info 2
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- ansible_loop.nextitem == 'banana'
|
|
|
|
- ansible_loop.index == 2
|
|
|
|
- ansible_loop.index0 == 1
|
|
|
|
- not ansible_loop.first
|
|
|
|
- not ansible_loop.last
|
|
|
|
- ansible_loop.previtem == 'apple'
|
|
|
|
- ansible_loop.allitems == ['apple', 'orange', 'banana']
|
|
|
|
- ansible_loop.revindex == 2
|
|
|
|
- ansible_loop.revindex0 == 1
|
|
|
|
- ansible_loop.length == 3
|
|
|
|
loop:
|
|
|
|
- apple
|
|
|
|
- orange
|
|
|
|
- banana
|
|
|
|
loop_control:
|
|
|
|
extended: true
|
|
|
|
when: item == 'orange'
|
|
|
|
|
|
|
|
- name: extended loop info 3
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- ansible_loop.nextitem is undefined
|
|
|
|
- ansible_loop.index == 3
|
|
|
|
- ansible_loop.index0 == 2
|
|
|
|
- not ansible_loop.first
|
|
|
|
- ansible_loop.last
|
|
|
|
- ansible_loop.previtem == 'orange'
|
|
|
|
- ansible_loop.allitems == ['apple', 'orange', 'banana']
|
|
|
|
- ansible_loop.revindex == 1
|
|
|
|
- ansible_loop.revindex0 == 0
|
|
|
|
- ansible_loop.length == 3
|
|
|
|
loop:
|
|
|
|
- apple
|
|
|
|
- orange
|
|
|
|
- banana
|
|
|
|
loop_control:
|
|
|
|
extended: true
|
|
|
|
when: item == 'banana'
|