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.
reviewable/pr18780/r1
Petros Moisiadis 12 years ago
parent ac02b85aad
commit 218a63f58f

@ -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)

Loading…
Cancel
Save