From 16f3d018c091ed4544e4296d43b5fdbfea0d6964 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Fri, 6 Apr 2012 11:16:53 -0400 Subject: [PATCH] Added 'creates=filename' to the shell/command module, which can skip command execution if a file already exists --- command | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/command b/command index 90b6b09d7db..77ccb2c1a6a 100755 --- a/command +++ b/command @@ -39,6 +39,26 @@ if args.find("#USE_SHELL") != -1: args = args.replace("#USE_SHELL", "") shell = True +check_args = shlex.split(args) +for x in check_args: + if x.startswith("creates="): + # do not run the command if the line contains creates=filename + # and the filename already exists. This allows idempotence + # of command executions. + (k,v) = x.split("=",1) + if os.path.exists(v): + print json.dumps({ + "cmd" : args, + "stdout" : "skipped, since %s exists" % v, + "skipped" : True, + "changed" : False, + "stderr" : "", + "rc" : 0, + }) + sys.exit(0) + args = args.replace(x,'') + + if not shell: args = shlex.split(args) @@ -71,13 +91,14 @@ if err is None: err = '' result = { - "cmd" : args, - "stdout" : out.strip(), - "stderr" : err.strip(), - "rc" : cmd.returncode, - "start" : str(startd), - "end" : str(endd), - "delta" : str(delta), + "cmd" : args, + "stdout" : out.strip(), + "stderr" : err.strip(), + "rc" : cmd.returncode, + "start" : str(startd), + "end" : str(endd), + "delta" : str(delta), + "changed" : True } print json.dumps(result)