I want to extract file only when particular directory do not exist.I wrote below code to achieve this.
- name: check if program already exists.
shell: ls /tomcats|grep akash
register: programexists
- name: extract archive
command: chdir=/tomcats/ /bin/tar xvf program.tar.gz
when: programexists.stdout == {{ program }}
but this code fails when "akash" directory do not exists as programexits.stdout will be empty in that case.
I wonder if i can create a custom filter in filter_plugins which checks if directory exists and use the same in play book like
code for custom filter
__author__ = 'akthakur'
import os
class FilterModule(object):
''' Custom filters are loaded by FilterModule objects '''
def filters(self):
''' FilterModule objects return a dict mapping filter names to
filter functions. '''
return {
'directory_exists': self.directory_exists,
}
def directory_exists(self,program):
static_location='/usr/local/tomcats'
path=os.path.join(static_location,program)
if os.path.isdir(path):
return True
else:
return False
and use the filter like
- name: extract archive
command: chdir=/tomcats/ /bin/tar xvf program.tar.gz
when: {{ 'akash'|directory_exists }}
I may be expecting too much from ansible but if some one can help me...i will be really thankful.
It should be even simpler. The
command
module has acreates
parameter, which is nothing but a filter and only will execute the command if the path given tocreates
does not exist. So if your command will create/tomcats/akash
this should work: