toggle for stripping empty shell/command output (#51342)

* toggle for stripping empty shell/command output

  fixes #45367

* Update lib/ansible/modules/commands/command.py

Co-Authored-By: bcoca <bcoca@users.noreply.github.com>
pull/51451/head
Brian Coca 6 years ago committed by GitHub
parent 369076a1a4
commit 3ba3af5058
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
minor_changes:
- add option to shell/command to control stripping of empty lines at end of outputs

@ -69,6 +69,12 @@ options:
description: description:
- If set to C(yes), append a newline to stdin data. - If set to C(yes), append a newline to stdin data.
version_added: "2.8" version_added: "2.8"
strip_empty_ends:
description:
- Strip empty lines from the end of stdout/stderr in result.
version_added: "2.8"
type: bool
default: yes
notes: notes:
- If you want to run a command through the shell (say you are using C(<), C(>), C(|), etc), you actually want the M(shell) module instead. - If you want to run a command through the shell (say you are using C(<), C(>), C(|), etc), you actually want the M(shell) module instead.
Parsing shell metacharacters can lead to unexpected commands being executed if quoting is not done correctly so it is more secure to Parsing shell metacharacters can lead to unexpected commands being executed if quoting is not done correctly so it is more secure to
@ -212,6 +218,7 @@ def main():
warn=dict(type='bool', default=True), warn=dict(type='bool', default=True),
stdin=dict(required=False), stdin=dict(required=False),
stdin_add_newline=dict(type='bool', default=True), stdin_add_newline=dict(type='bool', default=True),
strip_empty_ends=dict(type='bool', default=True),
), ),
supports_check_mode=True, supports_check_mode=True,
) )
@ -225,6 +232,7 @@ def main():
warn = module.params['warn'] warn = module.params['warn']
stdin = module.params['stdin'] stdin = module.params['stdin']
stdin_add_newline = module.params['stdin_add_newline'] stdin_add_newline = module.params['stdin_add_newline']
strip = module.params['strip_empty_ends']
if not shell and executable: if not shell and executable:
module.warn("As of Ansible 2.4, the parameter 'executable' is no longer supported with the 'command' module. Not using '%s'." % executable) module.warn("As of Ansible 2.4, the parameter 'executable' is no longer supported with the 'command' module. Not using '%s'." % executable)
@ -289,10 +297,14 @@ def main():
endd = datetime.datetime.now() endd = datetime.datetime.now()
delta = endd - startd delta = endd - startd
if strip:
out = out.rstrip(b"\r\n")
err = err.rstrip(b"\r\n")
result = dict( result = dict(
cmd=args, cmd=args,
stdout=out.rstrip(b"\r\n"), stdout=out,
stderr=err.rstrip(b"\r\n"), stderr=err,
rc=rc, rc=rc,
start=str(startd), start=str(startd),
end=str(endd), end=str(endd),

Loading…
Cancel
Save