How to break loop in Ansible?
10,123
Ansible runs the loop and:
for items
4
,5
, and6
executes theecho
command - showschanged
status,for remaining items it does not execute it - shows
skipped
status,
You have achieved what you wanted.
The only thing you can improve is getting rid of the warning by fixing the conditional:
- hosts: localhost
tasks:
- shell: echo {{ item }}
register: result
with_sequence: start=4 end=16
when: "item|int < 7"
Author by
va1bhav
Updated on June 24, 2022Comments
-
va1bhav almost 2 years
Want to break the task after the value for item became 7, here is the sample task
- hosts: localhost tasks: - shell: echo {{ item }} register: result with_sequence: start=4 end=16 when: "{{ item }} < 7"
On the above code it's iterating the task from 4 to 16 as below
PLAY [localhost] *********************************************************************************************************************************************** TASK [Gathering Facts] ***************************************************************************************************************************************** ok: [localhost] TASK [command] ************************************************************************************************************************************************* [WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ item }} < 7 changed: [localhost] => (item=4) changed: [localhost] => (item=5) changed: [localhost] => (item=6) skipping: [localhost] => (item=7) skipping: [localhost] => (item=8) skipping: [localhost] => (item=9) skipping: [localhost] => (item=10) skipping: [localhost] => (item=11) skipping: [localhost] => (item=12) skipping: [localhost] => (item=13) skipping: [localhost] => (item=14) skipping: [localhost] => (item=15) skipping: [localhost] => (item=16) PLAY RECAP ***************************************************************************************************************************************************** localhost : ok=2 changed=1 unreachable=0 failed=0