Use conditionals to determine if further action is required

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.

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.

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?

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

Nope :frowning:

Doing this:

`

“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

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!

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

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

`