From 6250b64ef9cc10548befdeba65110ce3a2e46ece Mon Sep 17 00:00:00 2001 From: Josh Mandel Date: Thu, 28 Feb 2013 14:38:52 -0800 Subject: [PATCH] Use relative creates/removes path with chdir --- library/command | 64 ++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/library/command b/library/command index 4e43b9fa3c..e996c0c4a7 100644 --- a/library/command +++ b/library/command @@ -88,6 +88,8 @@ def main(): chdir = module.params['chdir'] executable = module.params['executable'] args = module.params['args'] + creates = module.params['creates'] + removes = module.params['removes'] if args.strip() == '': module.fail_json(rc=256, msg="no command given") @@ -95,6 +97,36 @@ def main(): if chdir: os.chdir(os.path.expanduser(chdir)) + if creates: + # do not run the command if the line contains creates=filename + # and the filename already exists. This allows idempotence + # of command executions. + v = os.path.expanduser(creates) + if os.path.exists(v): + module.exit_json( + cmd=args, + stdout="skipped, since %s exists" % v, + skipped=True, + changed=False, + stderr=False, + rc=0 + ) + + if removes: + # do not run the command if the line contains removes=filename + # and the filename does not exist. This allows idempotence + # of command executions. + v = os.path.expanduser(removes) + if not os.path.exists(v): + module.exit_json( + cmd=args, + stdout="skipped, since %s does not exist" % v, + skipped=True, + changed=False, + stderr=False, + rc=0 + ) + if not shell: args = shlex.split(args) startd = datetime.datetime.now() @@ -139,6 +171,8 @@ class CommandModule(AnsibleModule): args = MODULE_ARGS params = {} params['chdir'] = None + params['creates'] = None + params['removes'] = None params['shell'] = False params['executable'] = None if args.find("#USE_SHELL") != -1: @@ -149,38 +183,14 @@ class CommandModule(AnsibleModule): for m in r.finditer(args): v = m.group(4).replace("\\", "") if m.group(2) == "creates": - # do not run the command if the line contains creates=filename - # and the filename already exists. This allows idempotence - # of command executions. - v = os.path.expanduser(v) - if os.path.exists(v): - self.exit_json( - cmd=args, - stdout="skipped, since %s exists" % v, - skipped=True, - changed=False, - stderr=False, - rc=0 - ) + params['creates'] = v elif m.group(2) == "removes": - # do not run the command if the line contains removes=filename - # and the filename do not exists. This allows idempotence - # of command executions. - v = os.path.expanduser(v) - if not os.path.exists(v): - self.exit_json( - cmd=args, - stdout="skipped, since %s does not exist" % v, - skipped=True, - changed=False, - stderr=False, - rc=0 - ) + params['removes'] = v elif m.group(2) == "chdir": v = os.path.expanduser(v) if not (os.path.exists(v) and os.path.isdir(v)): self.fail_json(rc=258, msg="cannot change to directory '%s': path does not exist" % v) - elif v[0] != '/': + elif v[0] != os.sep: self.fail_json(rc=259, msg="the path for 'chdir' argument must be fully qualified") params['chdir'] = v elif m.group(2) == "executable":