# Ignoring errors in check mode

**URL:** <https://forum.ansible.com/t/ignoring-errors-in-check-mode/18973>\
**Category:** Ansible Project\
**Created:** [August 7, 2015, 11:15pm UTC](https://forum.ansible.com/t/ignoring-errors-in-check-mode/18973 "2015-08-07T23:15:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Albertdev](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@Albertdev](https://forum.ansible.com/u/Albertdev)\
**Post date:** [August 7, 2015, 11:15pm UTC](https://forum.ansible.com/t/ignoring-errors-in-check-mode/18973/1 "2015-08-07T23:15:58Z")

</div>

Hello,

I’ve been tearing my hair out over a problem which seems simple to fix but is somehow full of pitfalls.

Basically I have a few playbooks where one task depends on the previous one. For example, I create a file based on a template and then as a next step symlink it, or I create a user and then register his SSH key in the authorized\_keys file.

Problem is that passing --check will stop the template and user creation tasks from modifying the system, yet the next tasks still run their full validation and call on a failure. I could put “ignore\_errors: True” on them, but then I wouldn’t get a real failure result in case they fail during a normal execution. I managed to find a reasonable workaround for the first case (the file module has a force=yes option to suppress the error), but the second one seems to have no such option and quite frankly I’m sure there will be other modules out there where the failure handling isn’t infinitely tweakable.

At this point I started looking at ignore\_errors to see if I could pass it a variable. Here follows a sample playbook which I run with “-e ansible\_check\_mode=True” and --check :

> * * *
> 
> - hosts: all  
> name: testing  
> tasks:
> - name: print variable  
> action: debug msg=“Does variable exist [{{ ansible\_check\_mode }}]”
> - name: Make symlink  
> action: file src=“/ohwhat/hai” dest=“/there/youare” state=link  
> ignore\_errors: ansible\_check\_mode

The first task prints that the variable exists and is set to true, but the second task yields this unclear error:

> ERROR: error while evaluating conditional: ansible\_check\_mode

Great. I thought that using a Jinja2 template might fix the issue:

> …  
> ignore\_errors: “{{ ansible\_check\_mode == True }}”

No luck, except that this yields a real traceback this time:

> Traceback (most recent call last):  
> File “/usr/bin/ansible-playbook”, line 323, in   
> sys.exit(main(sys.argv[1:]))  
> File “/usr/bin/ansible-playbook”, line 263, in main  
> pb.run()  
> File “/usr/lib/python2.6/site-packages/ansible/playbook/ **init**.py”, line 348, in run  
> if not self.\_run\_play(play):  
> File “/usr/lib/python2.6/site-packages/ansible/playbook/ **init**.py”, line 788, in \_run\_play  
> if not self.\_run\_task(play, task, False):  
> File “/usr/lib/python2.6/site-packages/ansible/playbook/ **init**.py”, line 493, in \_run\_task  
> task.ignore\_errors = utils.check\_conditional(cond, play.basedir, task.module\_vars, fail\_on\_undefined=C.DEFAULT\_UNDEFINED\_VAR\_BEHAVIOR)  
> File “/usr/lib/python2.6/site-packages/ansible/utils/ **init**.py”, line 265, in check\_conditional  
> conditional = template.template(basedir, conditional, inject, fail\_on\_undefined=fail\_on\_undefined)  
> File “/usr/lib/python2.6/site-packages/ansible/utils/template.py”, line 121, in template  
> varname = template\_from\_string(basedir, varname, templatevars, fail\_on\_undefined)  
> File “/usr/lib/python2.6/site-packages/ansible/utils/template.py”, line 368, in template\_from\_string  
> res = jinja2.utils.concat(rf)  
> File “”, line 8, in root  
> File “/usr/lib/python2.6/site-packages/jinja2/runtime.py”, line 485, in \_fail\_with\_undefined\_error  
> raise self.\_undefined\_exception(hint)  
> jinja2.exceptions.UndefinedError: ‘ansible\_check\_mode’ is undefined

I’ve tested this on Ansible 1.8.2 and 1.9.0 (both stable) and both went belly-up.

It seems this weirdness has been mentioned [before in this mailing group](https://groups.google.com/d/topic/ansible-project/E4aUBWgrr4s/discussion), but their workaround of duplicating the task and then playing with **when** is triggering my DRY-sense (Don’t Repeat Yourself). At this point I think it needs some fix in Ansible to actually get this working while keeping playbooks simple.

While I’m creating this topic anyway, did anything ever come of a [magic variable to detect check mode](https://groups.google.com/d/msg/ansible-project/BtC55jifUbw/XImn764Msh8J)? So far I’ve been testing [a horrible, horrible kludge](http://stackoverflow.com/a/28888474/983949) which detects check mode by the behaviour of the command module, but a simple magic variable could do wonders because I wouldn’t need to duplicate that kludge in all my roles.

If you agree that something can be done about all of the above, please let me know. I wanted to run this by the mailing group first before throwing it on the bugtracker to get feedback on how to file it. If you have been reading so far, thank you for your time.

With best regards,

Albert

---

<div class="post-metadata">

**Author:** ![Albertdev](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@Albertdev](https://forum.ansible.com/u/Albertdev)\
**Post date:** [August 18, 2015, 11:27am UTC](https://forum.ansible.com/t/ignoring-errors-in-check-mode/18973/2 "2015-08-18T11:27:09Z")

</div>

Nobody got any comments if and where to best report these errors? While my original mail was more of a rant than a constructive topic, I still have these errors when I try to put a variable in ignore\_errors:

When I directly reference a variable:

> ERROR: error while evaluating conditional: ansible\_check\_mode

When I use a jinja placeholder:

> Traceback (most recent call last):  
> File “/usr/bin/ansible-playbook”, line 323, in   
> sys.exit(main(sys.argv[1:]))  
> File “/usr/bin/ansible-playbook”, line 263, in main  
> pb.run()  
> File “/usr/lib/python2.6/site-packages/ansible/playbook/ **init**.py”, line 348, in run  
> if not self.\_run\_play(play):  
> File “/usr/lib/python2.6/site-packages/ansible/playbook/ **init**.py”, line 788, in \_run\_play  
> if not self.\_run\_task(play, task, False):  
> File “/usr/lib/python2.6/site-packages/ansible/playbook/ **init**.py”, line 493, in \_run\_task  
> task.ignore\_errors = utils.check\_conditional(cond, play.basedir, task.module\_vars, fail\_on\_undefined=C.DEFAULT\_UNDEFINED\_VAR\_BEHAVIOR)  
> File “/usr/lib/python2.6/site-packages/ansible/utils/ **init**.py”, line 265, in check\_conditional  
> conditional = template.template(basedir, conditional, inject, fail\_on\_undefined=fail\_on\_undefined)  
> File “/usr/lib/python2.6/site-packages/ansible/utils/template.py”, line 121, in template  
> varname = template\_from\_string(basedir, varname, templatevars, fail\_on\_undefined)  
> File “/usr/lib/python2.6/site-packages/ansible/utils/template.py”, line 368, in template\_from\_string  
> res = jinja2.utils.concat(rf)  
> File “”, line 8, in root  
> File “/usr/lib/python2.6/site-packages/jinja2/runtime.py”, line 485, in \_fail\_with\_undefined\_error  
> raise self.\_undefined\_exception(hint)  
> jinja2.exceptions.UndefinedError: ‘ansible\_check\_mode’ is undefined

I have since worked around the problem like this but I hate this kind of boilerplate:

> * * *
> 
> - hosts: all  
> name: testing  
> tasks:
> - name: print variable  
> action: debug msg=“Does variable exist [{{ ansible\_check\_mode }}]”
> - name: Make symlink  
> action: file src=“/ohwhat/hai” dest=“/there/youare” state=link  
> ignore\_errors: True  
> register: symlinkresult
> - name: Fail if symlink could not be created  
> action: fail msg=“Symlink could not be created”  
> when: “{{ not ( ansible\_check\_mode) and (symlinkresult | failed) }}”

With best regards,

Albert

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [August 19, 2015, 10:37am UTC](https://forum.ansible.com/t/ignoring-errors-in-check-mode/18973/3 "2015-08-19T10:37:59Z")

</div>

Not tried myself but I wonder if you could use failed\_when to reduce the boilerplate a little: [http://docs.ansible.com/ansible/playbooks\_error\_handling.html#controlling-what-defines-failure](http://docs.ansible.com/ansible/playbooks_error_handling.html#controlling-what-defines-failure)

Something like this (as I say, untested):

---

<div class="post-metadata">

**Author:** ![Albertdev](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@Albertdev](https://forum.ansible.com/u/Albertdev)\
**Post date:** [August 19, 2015, 11:25am UTC](https://forum.ansible.com/t/ignoring-errors-in-check-mode/18973/4 "2015-08-19T11:25:29Z")

</div>

I believe I did try something like that though, IIRC it still failed the playbook because “failed\_when:” is meant to turn an “Ok” status into “Failed” (e.g. when a command writes an error to stdout while its return status is 0), not turn a “Failed” status into “Ok”.

Once I tried this I went on to add “when: not ansible\_check\_mode” but now you can’t see if that tasks thinks it needs to be executed when running in check mode…

Anyhow, I guess I might try it once more. Thanks for your input.
