V Mahaja
V Mahaja

Reputation: 365

How to break loop in Ansible?

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

Upvotes: 4

Views: 14670

Answers (2)

techraf
techraf

Reputation: 68609

Ansible runs the loop and

  • for items 4, 5, and 6 executes the echo command - shows changed 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"

Upvotes: 6

U880D
U880D

Reputation: 12123

The solution without break, until, when, skipping or any IF, THEN, ELSE construct on runtime and list elements.

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - name: "For {{ loop_range }} loop up to < {{ smaller_then }}"
    debug:
      msg: "{{ item }}"
    loop: "{{ loop_range[:idx | int] }}"
    loop_control:
      extended: true
      label: "{{ ansible_loop.index }}"
    vars:
      loop_range: "{{ range(4, 17, 1) }}"
      smaller_then: 7
      idx: "{{ loop_range | ansible.utils.index_of('eq', smaller_then) }}"

will result into

TASK [For [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] loop up to < 7] ******
ok: [localhost] => (item=1) =>
  msg: 4
ok: [localhost] => (item=2) =>
  msg: 5
ok: [localhost] => (item=3) =>
  msg: 6

Further Reading

Upvotes: 0

Related Questions