Ansible do-until loop in check mode

492 views
Skip to first unread message

Ryan Groten

unread,
Jun 27, 2017, 1:19:54 PM6/27/17
to Ansible Project
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?  

Kai Stian Olstad

unread,
Jun 27, 2017, 1:26:06 PM6/27/17
to ansible...@googlegroups.com
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.

--
Kai Stian Olstad

Ryan Groten

unread,
Jun 27, 2017, 1:40:05 PM6/27/17
to Ansible Project, ansible-pr...@olstad.com
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.

Kai Stian Olstad

unread,
Jun 27, 2017, 2:13:01 PM6/27/17
to ansible...@googlegroups.com
On 27. juni 2017 19:40, Ryan Groten wrote:
> 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

--
Kai Stian Olstad

Kai Stian Olstad

unread,
Jun 27, 2017, 2:17:03 PM6/27/17
to ansible...@googlegroups.com
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

--
Kai Stian Olstad

Ryan Groten

unread,
Jun 27, 2017, 3:11:35 PM6/27/17
to Ansible Project, ansible-pr...@olstad.com
That works just fine, thanks Kai!

Ryan Groten

unread,
Jun 27, 2017, 3:22:56 PM6/27/17
to Ansible Project, ansible-pr...@olstad.com
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)

Ryan Groten

unread,
Jun 27, 2017, 3:37:47 PM6/27/17
to Ansible Project, ansible-pr...@olstad.com
Figured out a different solution. I added this conditional to my task:

when: not ansible_check_mode

Thanks for the help!
Reply all
Reply to author
Forward
0 new messages