Use conditionals to determine if further action is required

64 views
Skip to first unread message

Mark van der Hout

unread,
Feb 10, 2015, 5:54:14 AM2/10/15
to ansible...@googlegroups.com
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.

Brian Coca

unread,
Feb 10, 2015, 7:22:56 AM2/10/15
to ansible...@googlegroups.com
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 Coca

Mark van der Hout

unread,
Feb 10, 2015, 7:38:55 AM2/10/15
to ansible...@googlegroups.com
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?

Brian Coca

unread,
Feb 10, 2015, 7:40:54 AM2/10/15
to ansible...@googlegroups.com
unless you are defining output somewhere else with 'hello world' there
should be nothing producing this result.




--
Brian Coca

Mark van der Hout

unread,
Feb 10, 2015, 7:56:25 AM2/10/15
to ansible...@googlegroups.com
Nope :(
Doing this:
---
- name: Check to see if the tomcat instance already exists
  shell
: find /usr/local/tomcat/instances -name "{{ tomcat_instance_name}}"
 
register: output


- name: debug output
  debug
: output


- name: debout output.stdout
  debug
: output.stdout


- name: debug output.stderr
  debug
: output.stderr


- name: debug output.rc
  debug
: output.rc


Results in:

TASK: [tomcat7 | Check to see if the tomcat instance already exists] **********
changed
: [slave]


TASK
: [tomcat7 | debug output] ************************************************
ok
: [slave] => {
   
"msg": "Hello world!"
}


TASK
: [tomcat7 | debout output.stdout] ****************************************
ok
: [slave] => {
   
"msg": "Hello world!"
}


TASK
: [tomcat7 | debug output.stderr] *****************************************
ok
: [slave] => {
   
"msg": "Hello world!"
}


TASK
: [tomcat7 | debug output.rc] *********************************************
ok
: [slave] => {
   
"msg": "Hello world!"
}




Matt Martz

unread,
Feb 10, 2015, 8:04:06 AM2/10/15
to ansible...@googlegroups.com
"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
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/49d82688-2f50-43f6-a3f7-0746cac9bc5b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Mark van der Hout

unread,
Feb 10, 2015, 8:20:03 AM2/10/15
to ansible...@googlegroups.com
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!

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Brian Coca

unread,
Feb 10, 2015, 9:10:19 AM2/10/15
to ansible...@googlegroups.com
duh!, i didn't see that either. We might want to change the derfault
to 'Hello World! debug invoked without parameters'




--
Brian Coca

Mark Phillips

unread,
Feb 10, 2015, 12:48:54 PM2/10/15
to ansible...@googlegroups.com
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

Reply all
Reply to author
Forward
0 new messages