Skip to content

Commit

Permalink
Enable check_mode in command module
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jradtilbrook committed May 19, 2018
1 parent 53b7bf3 commit 9e98249
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/ansible/modules/commands/command.py
Expand Up @@ -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']
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 9e98249

Please sign in to comment.