added a 'chdir' argument to the command module

the 'chdir' argument changes the current working directory to the
fullpath supplied as its value, before the execution of the command.
This commit is contained in:
Petros Moisiadis 2012-07-30 18:39:45 +03:00
parent ac02b85aad
commit 218a63f58f

13
command
View file

@ -31,8 +31,12 @@ def main():
module = CommandModule(argument_spec=dict())
shell = module.params['shell']
chdir = module.params['chdir']
args = module.params['args']
if chdir:
os.chdir(chdir)
if not shell:
args = shlex.split(args)
startd = datetime.datetime.now()
@ -77,6 +81,7 @@ class CommandModule(AnsibleModule):
args = base64.b64decode(MODULE_ARGS)
items = shlex.split(args)
params = {}
params['chdir'] = None
params['shell'] = False
if args.find("#USE_SHELL") != -1:
args = args.replace("#USE_SHELL", "")
@ -99,6 +104,14 @@ class CommandModule(AnsibleModule):
rc=0
)
args = args.replace(x,'')
elif x.startswith("chdir="):
(k,v) = x.split("=", 1)
if not (os.path.exists(v) and os.path.isdir(v)):
self.fail_json(msg="cannot change to directory '%s': path does not exist" % v)
elif v[0] != '/':
self.fail_json(msg="the path for 'chdir' argument must be fully qualified")
params['chdir'] = v
args = args.replace(x, '')
params['args'] = args
return (params, args)