1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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>
This commit is contained in:
Brian Coca 2019-01-29 15:55:38 -05:00 committed by GitHub
parent 369076a1a4
commit 3ba3af5058
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View file

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

View file

@ -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),