# exit a with\_items loop after first successful action

**URL:** <https://forum.ansible.com/t/exit-a-with-items-loop-after-first-successful-action/43929>\
**Category:** Get Help\
**Created:** [July 17, 2025, 7:53pm UTC](https://forum.ansible.com/t/exit-a-with-items-loop-after-first-successful-action/43929 "2025-07-17T19:53:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bskeefer](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@bskeefer](https://forum.ansible.com/u/bskeefer)\
**Post date:** [July 17, 2025, 7:53pm UTC](https://forum.ansible.com/t/exit-a-with-items-loop-after-first-successful-action/43929/1 "2025-07-17T19:53:11Z")

</div>

I am working on a project to help our environment get more standardized with regards to LUKS passwords, and I have this as a use case:

given a list of known used passwords for LUKS, I need to loop through the password list until I find the password that opens the luks device using the --test-passphrase option.

once I find the correct password, I can then use it to rebind luks to a new randomly generated password. But I don’t need the loop to continue trying other passwords that won’t work.

I almost need to be able to specify a when condition to break the loop based on the return code of the cryptsetup comand

something like:

- name: determine correct luks password  
ansible.builtin.shell:  
cmd: echo {{ item }} |cryptsetup --test-passphrase luksOpen {{ luks\_dev }}  
register: pw\_result  
when: rc == 0

but that won’t work.

my other option would be o use a bash script for this one step, and then go from there.

Just making sure I’m not missing something.

Searching in this august forum I found a thread but it was back in 2020.

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [July 17, 2025, 8:20pm UTC](https://forum.ansible.com/t/exit-a-with-items-loop-after-first-successful-action/43929/2 "2025-07-17T20:20:32Z")

</div>

> [@bskeefer](#):
>
> that won’t work.

That’s not a very can-do attitude. It will work if you reference the variable you registered (instead of a nonexistent variable named `rc`) and think through the logic of what conditions should result in a skip.

```yaml
- hosts: localhost
  tasks:
    - shell: exit {{ item }}
      register: result
      failed_when: false
      when:
        - result.rc | default(0) != 2
        - result is not defined or result is not skipped
      loop:
        - 0
        - 1
        - 2
        - 3
        - 0

```

```plaintext
TASK [shell] *******************************************************************
changed: [localhost] => (item=0)
changed: [localhost] => (item=1)
changed: [localhost] => (item=2)
skipping: [localhost] => (item=3) 
skipping: [localhost] => (item=0) 

```

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [July 17, 2025, 8:26pm UTC](https://forum.ansible.com/t/exit-a-with-items-loop-after-first-successful-action/43929/3 "2025-07-17T20:26:29Z")

</div>

2.18 also introduced `loop_control.break_when`:

> **[Loops — Ansible Community Documentation](https://docs.ansible.com/ansible-core/2.18/playbook_guide/playbooks_loops.html#breaking-out-of-a-loop)**

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [July 17, 2025, 10:33pm UTC](https://forum.ansible.com/t/exit-a-with-items-loop-after-first-successful-action/43929/4 "2025-07-17T22:33:38Z")

</div>

for older versions, you can check previous results of the loop

```auto
when: pw_result is not defined or pw_result.get('rc') == 0

```

This will run the first time, before pw\_result is defined and check if it is 0 or not from the last result from 2nd iteration and above. This will skip the rest of the loop, but still run through the whole loop.
