Ansible do-until loop in check mode

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?

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

   check_mode: no

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

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.

Than you can do this

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

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

So the what you only need

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

That works just fine, thanks Kai!

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)

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

`
when: not ansible_check_mode

`

Thanks for the help!