diff options
author | Brian Coca <bcoca@users.noreply.github.com> | 2024-09-05 16:16:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-05 16:16:23 +0200 |
commit | 4fa512406bf993a570759a0c5deed20d639e51c8 (patch) | |
tree | a114c5e54de48206d7415e4bfe4f86d18ee61868 /test | |
parent | release.py - Include pyproject.toml in git add (#83892) (diff) | |
download | ansible-4fa512406bf993a570759a0c5deed20d639e51c8.tar.xz ansible-4fa512406bf993a570759a0c5deed20d639e51c8.zip |
loop_control "early exit" feature (#62151)
* add a loop_control break_when directive to break out of a loop after any item
* remove loop var as normal exit would
* example usage:
- name: generate a random password up to 10 times, until it matches the policy
set_fact:
password: "{{ lookup('password', '/dev/null', chars=character_set, length=length) }}"
loop: "{{ range(0, 10) }}"
loop_control:
break_when:
- password is match(password_policy)
Co-authored-by: s-hertel <19572925+s-hertel@users.noreply.github.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/integration/targets/loop_control/break_when.yml | 17 | ||||
-rwxr-xr-x | test/integration/targets/loop_control/runme.sh | 2 | ||||
-rw-r--r-- | test/units/executor/test_task_executor.py | 2 |
3 files changed, 21 insertions, 0 deletions
diff --git a/test/integration/targets/loop_control/break_when.yml b/test/integration/targets/loop_control/break_when.yml new file mode 100644 index 0000000000..da3de28937 --- /dev/null +++ b/test/integration/targets/loop_control/break_when.yml @@ -0,0 +1,17 @@ +- hosts: localhost + gather_facts: false + tasks: + - debug: var=item + changed_when: false + loop: + - 1 + - 2 + - 3 + - 4 + loop_control: + break_when: item >= 2 + register: untiltest + + - assert: + that: + - untiltest['results']|length == 2 diff --git a/test/integration/targets/loop_control/runme.sh b/test/integration/targets/loop_control/runme.sh index af065ea0e2..6c71aedd78 100755 --- a/test/integration/targets/loop_control/runme.sh +++ b/test/integration/targets/loop_control/runme.sh @@ -10,3 +10,5 @@ bar_label' [ "$(ansible-playbook label.yml "$@" |grep 'item='|sed -e 's/^.*(item=looped_var \(.*\)).*$/\1/')" == "${MATCH}" ] ansible-playbook extended.yml "$@" + +ansible-playbook break_when.yml "$@" diff --git a/test/units/executor/test_task_executor.py b/test/units/executor/test_task_executor.py index f562bfa525..8f95d801db 100644 --- a/test/units/executor/test_task_executor.py +++ b/test/units/executor/test_task_executor.py @@ -164,9 +164,11 @@ class TestTaskExecutor(unittest.TestCase): def _copy(exclude_parent=False, exclude_tasks=False): new_item = MagicMock() + new_item.loop_control = MagicMock(break_when=[]) return new_item mock_task = MagicMock() + mock_task.loop_control = MagicMock(break_when=[]) mock_task.copy.side_effect = _copy mock_play_context = MagicMock() |