From 9e982498e2bbaabc63d89fce9d934bd81b3f010d Mon Sep 17 00:00:00 2001 From: jradtilbrook Date: Sat, 19 May 2018 08:19:07 +0800 Subject: [PATCH] Enable check_mode in command module This only works if supplying creates or removes since it needs something to base the heuristic off. If none are supplied it will just skip as usual. Fixes #15828 --- lib/ansible/modules/commands/command.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/commands/command.py b/lib/ansible/modules/commands/command.py index 288a1e1017e3c0..1f35e6888d916c 100644 --- a/lib/ansible/modules/commands/command.py +++ b/lib/ansible/modules/commands/command.py @@ -183,7 +183,8 @@ def main(): # The default for this really comes from the action plugin warn=dict(type='bool', default=True), stdin=dict(required=False), - ) + ), + supports_check_mode=True, ) shell = module.params['_uses_shell'] chdir = module.params['chdir'] @@ -225,6 +226,15 @@ def main(): changed=False, rc=0 ) + # if in check mode and the filename doesnt exist, report that a change + # would be made + elif module.check_mode: + module.exit_json( + cmd=args, + stdout="changed, since %s does not exist" % creates, + changed=True, + rc=0 + ) if removes: # do not run the command if the line contains removes=filename @@ -237,10 +247,23 @@ def main(): changed=False, rc=0 ) + # if in check mode and the filename exists, report that a change would + # be made + elif module.check_mode: + module.exit_json( + cmd=args, + stdout="changed, since %s exists" % removes, + changed=True, + rc=0 + ) if warn: check_command(module, args) + # skip if in check mode so no changes are made + if module.check_mode: + module.exit_json(msg="skipped, running in check mode", skipped=True) + startd = datetime.datetime.now() rc, out, err = module.run_command(args, executable=executable, use_unsafe_shell=shell, encoding=None, data=stdin)