# Ansible do-until loop in check mode

**URL:** <https://forum.ansible.com/t/ansible-do-until-loop-in-check-mode/24831>\
**Category:** Ansible Project\
**Created:** [June 27, 2017, 5:19pm UTC](https://forum.ansible.com/t/ansible-do-until-loop-in-check-mode/24831 "2017-06-27T17:19:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Groten](https://avatars.discourse-cdn.com/v4/letter/r/f9ae1b/32.png) [@Ryan\_Groten](https://forum.ansible.com/u/Ryan_Groten)\
**Post date:** [June 27, 2017, 5:19pm UTC](https://forum.ansible.com/t/ansible-do-until-loop-in-check-mode/24831/1 "2017-06-27T17:19:54Z")

</div>

Using a standard do-until loop like this:

> - shell: /bin/true  
> register: result  
> until: result.rc == 0  
> retries: 5  
> delay: 10

Anytime I run the playbook in check mode this task will fail (because the result variable is not set). It seems like the until part of the loop is evaluated even though the task is being skipped, what’s the best way to fix this?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 27, 2017, 5:25pm UTC](https://forum.ansible.com/t/ansible-do-until-loop-in-check-mode/24831/2 "2017-06-27T17:25:42Z")

</div>

To make shell and command module runs in check mode you need to add

&nbsp;&nbsp;&nbsp;check\_mode: no

then the shell task will run inn check mode and the output is registered in the variable.

---

<div class="post-metadata">

**Author:** ![Ryan\_Groten](https://avatars.discourse-cdn.com/v4/letter/r/f9ae1b/32.png) [@Ryan\_Groten](https://forum.ansible.com/u/Ryan_Groten)\
**Post date:** [June 27, 2017, 5:40pm UTC](https://forum.ansible.com/t/ansible-do-until-loop-in-check-mode/24831/3 "2017-06-27T17:40:05Z")

</div>

Thanks for the response Kai, in this case I want the task not to run at all in check mode, only execute the loop when the play is run in execute mode.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 27, 2017, 6:12pm UTC](https://forum.ansible.com/t/ansible-do-until-loop-in-check-mode/24831/4 "2017-06-27T18:12:44Z")

</div>

Than you can do this

until: (result | default(0)).rc | default(0) == 0

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 27, 2017, 6:16pm UTC](https://forum.ansible.com/t/ansible-do-until-loop-in-check-mode/24831/5 "2017-06-27T18:16:46Z")

</div>

When I think about it, you don't need the first default because the variable result do exist.

So the what you only need

&nbsp;&nbsp;&nbsp;until: result.rc | default(0) == 0

---

<div class="post-metadata">

**Author:** ![Ryan\_Groten](https://avatars.discourse-cdn.com/v4/letter/r/f9ae1b/32.png) [@Ryan\_Groten](https://forum.ansible.com/u/Ryan_Groten)\
**Post date:** [June 27, 2017, 7:11pm UTC](https://forum.ansible.com/t/ansible-do-until-loop-in-check-mode/24831/6 "2017-06-27T19:11:34Z")

</div>

That works just fine, thanks Kai!

---

<div class="post-metadata">

**Author:** ![Ryan\_Groten](https://avatars.discourse-cdn.com/v4/letter/r/f9ae1b/32.png) [@Ryan\_Groten](https://forum.ansible.com/u/Ryan_Groten)\
**Post date:** [June 27, 2017, 7:22pm UTC](https://forum.ansible.com/t/ansible-do-until-loop-in-check-mode/24831/7 "2017-06-27T19:22:56Z")

</div>

One slight wrinkle, my example wasn’t exactly what I’m trying to do. Here’s my real until loop:

> until: cluster\_health.json.status | default(“green”) == “green”

That fails with the error “‘dict object’ has no attribute ‘json’”

I think the difference is that my value is two attributes deep (ie: cluster\_health. **json.status** versus response.rc)

---

<div class="post-metadata">

**Author:** ![Ryan\_Groten](https://avatars.discourse-cdn.com/v4/letter/r/f9ae1b/32.png) [@Ryan\_Groten](https://forum.ansible.com/u/Ryan_Groten)\
**Post date:** [June 27, 2017, 7:37pm UTC](https://forum.ansible.com/t/ansible-do-until-loop-in-check-mode/24831/8 "2017-06-27T19:37:47Z")

</div>

Figured out a different solution. I added this conditional to my task:

`  
when: not ansible\_check\_mode

`

Thanks for the help!
