# Use conditionals to determine if further action is required

**URL:** <https://forum.ansible.com/t/use-conditionals-to-determine-if-further-action-is-required/17486>\
**Category:** Ansible Project\
**Created:** [February 10, 2015, 10:54am UTC](https://forum.ansible.com/t/use-conditionals-to-determine-if-further-action-is-required/17486 "2015-02-10T10:54:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_van\_der\_Hout](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@Mark\_van\_der\_Hout](https://forum.ansible.com/u/Mark_van_der_Hout)\
**Post date:** [February 10, 2015, 10:54am UTC](https://forum.ansible.com/t/use-conditionals-to-determine-if-further-action-is-required/17486/1 "2015-02-10T10:54:14Z")

</div>

So I’m struggling with the following.  
I want to check if a certain directory exists (Tomcat instance directory) , if so I wan’t to stop the playbook since apparently the tomcat instance is already present.

I thought this should work:

`

- name: create tomcat instance {{ tomcat\_instance\_name }} dir  
file: path=“{{ tomcat\_dir }}/instances/{{ tomcat\_instance\_name }}” state=directory  
register: result  
failed\_when: result|ok

- name: debug command result  
debug: result  
`

I expected that the var ‘result’ would contain the output like shown without -v (ok, changed, skipping) or perhaps with -v (changed:false/true etc etc)  
However this is not the case. Debuging said var will give me “Hello World!”. Well Hello to you to, but it’s not being very helpful.

**So the main question would be**. How do you guys check if something already exists and get it to fail/skip if it does.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 10, 2015, 12:22pm UTC](https://forum.ansible.com/t/use-conditionals-to-determine-if-further-action-is-required/17486/2 "2015-02-10T12:22:53Z")

</div>

If you fail on file, that host should not hit the debug task below.  
You can instead use the 'fail' module or set ignore\_errors: true if  
you need to do things afterwards. I'm not sure where you are getting  
'hello world' from.

---

<div class="post-metadata">

**Author:** ![Mark\_van\_der\_Hout](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@Mark\_van\_der\_Hout](https://forum.ansible.com/u/Mark_van_der_Hout)\
**Post date:** [February 10, 2015, 12:38pm UTC](https://forum.ansible.com/t/use-conditionals-to-determine-if-further-action-is-required/17486/3 "2015-02-10T12:38:55Z")

</div>

Brian,

It is true that I shouldn’t hit the debug. In my example I forgot to remove the failed\_when statement.  
If removed I still get the Hello World. No matter what I debug. stdout,err,rc…  
I’m not quite sure if a shell commando would work:

`name: Check to see if the tomcat instance already exists shell: find /usr/local/tomcat/instances -name "{{ tomcat_instance_name}}" register: output`

However this still gives “Hello World” when debugged.  
Am I missing something?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 10, 2015, 12:40pm UTC](https://forum.ansible.com/t/use-conditionals-to-determine-if-further-action-is-required/17486/4 "2015-02-10T12:40:51Z")

</div>

unless you are defining output somewhere else with 'hello world' there  
should be nothing producing this result.

---

<div class="post-metadata">

**Author:** ![Mark\_van\_der\_Hout](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@Mark\_van\_der\_Hout](https://forum.ansible.com/u/Mark_van_der_Hout)\
**Post date:** [February 10, 2015, 12:56pm UTC](https://forum.ansible.com/t/use-conditionals-to-determine-if-further-action-is-required/17486/5 "2015-02-10T12:56:25Z")

</div>

Nope ☹

Doing this:

`

---

<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:** [February 10, 2015, 1:04pm UTC](https://forum.ansible.com/t/use-conditionals-to-determine-if-further-action-is-required/17486/6 "2015-02-10T13:04:01Z")

</div>

“Hello World” is the default string that the debug module prints of the var it was given is undefined.

The problem is improper use of the debug module.

You aren’t giving it either “msg” or “var”.

It should look like:

- debug: var=output

---

<div class="post-metadata">

**Author:** ![Mark\_van\_der\_Hout](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@Mark\_van\_der\_Hout](https://forum.ansible.com/u/Mark_van_der_Hout)\
**Post date:** [February 10, 2015, 1:20pm UTC](https://forum.ansible.com/t/use-conditionals-to-determine-if-further-action-is-required/17486/7 "2015-02-10T13:20:03Z")

</div>

Winrar!  
That’s just plain stupid of me.

`"output.stdout": "/usr/local/tomcat/instances/tcsomething"`  
now with: failed\_when: “‘/usr/local/tomcat/’ in output.stdout” it works.

Ofcourse it would be better to use the fail module so I can write a custom message.

Thanks for pointing out the obvious!

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 10, 2015, 2:10pm UTC](https://forum.ansible.com/t/use-conditionals-to-determine-if-further-action-is-required/17486/8 "2015-02-10T14:10:16Z")

</div>

duh!, i didn't see that either. We might want to change the derfault  
to 'Hello World! debug invoked without parameters'

---

<div class="post-metadata">

**Author:** ![Mark\_Phillips](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Mark\_Phillips](https://forum.ansible.com/u/Mark_Phillips)\
**Post date:** [February 10, 2015, 5:48pm UTC](https://forum.ansible.com/t/use-conditionals-to-determine-if-further-action-is-required/17486/9 "2015-02-10T17:48:54Z")

</div>

Always worth looking at the stat module for checking out paths too Mark…

`

- name: Check for prior download  
stat: path={{ vmwtools\_tmp }}/{{ vmwtools\_tar}}  
register: tar

- name: Fetch tools install  
get\_url:  
url={{ vmwtools\_url }}/{{ vmwtools\_tar }}  
dest={{ vmwtools\_tmp }}  
when: not tar.stat.exists

`
