command modules: optional stdin_add_newline (#45170)

* stdin_add_newline: allow newline suppression on command modules

* command/shell: test for stdin_add_newline

* changelog for stdin_add_newline
This commit is contained in:
James Cassell 2018-10-31 12:53:02 -04:00 committed by Brian Coca
parent 1a91b797bd
commit 8eacaf6a77
4 changed files with 66 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
minor_changes:
- command/shell - new `stdin_add_newline` arg allows suppression of
automatically-added newline `\n` character to the specified in the `stdin`
arg.

View file

@ -58,6 +58,12 @@ options:
description:
- Set the stdin of the command directly to the specified value.
version_added: "2.4"
stdin_add_newline:
type: bool
default: yes
description:
- If set to C(yes), append a newline to stdin data.
version_added: "2.8"
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.
Parsing shell metacharacters can lead to unexpected commands being executed if quoting is not done correctly so it is more secure to
@ -189,6 +195,7 @@ def main():
# The default for this really comes from the action plugin
warn=dict(type='bool', default=True),
stdin=dict(required=False),
stdin_add_newline=dict(type='bool', default=True),
),
supports_check_mode=True,
)
@ -201,6 +208,7 @@ def main():
removes = module.params['removes']
warn = module.params['warn']
stdin = module.params['stdin']
stdin_add_newline = module.params['stdin_add_newline']
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)
@ -255,7 +263,7 @@ def main():
startd = datetime.datetime.now()
if not module.check_mode:
rc, out, err = module.run_command(args, executable=executable, use_unsafe_shell=shell, encoding=None, data=stdin)
rc, out, err = module.run_command(args, executable=executable, use_unsafe_shell=shell, encoding=None, data=stdin, binary_data=(not stdin_add_newline))
elif creates or removes:
rc = 0
out = err = b'Command would have run if not in check mode'

View file

@ -60,6 +60,12 @@ options:
description:
- Set the stdin of the command directly to the specified value.
version_added: "2.4"
stdin_add_newline:
type: bool
default: yes
description:
- If set to C(yes), append a newline to stdin data.
version_added: "2.8"
notes:
- If you want to execute a command securely and predictably, it may be
better to use the M(command) module instead. Best practices when writing

View file

@ -338,5 +338,51 @@
- shell_result7.cmd == 'cat <<EOF\nOne\n Two\n Three\nEOF\n'
- shell_result7.stdout == 'One\n Two\n Three'
- name: execute a shell command with no trailing newline to stdin
shell: cat > {{output_dir_test | expanduser}}/afile.txt
args:
stdin: test
stdin_add_newline: no
- name: make sure content matches expected
copy:
dest: "{{output_dir_test | expanduser}}/afile.txt"
content: test
register: shell_result7
failed_when:
- shell_result7 is failed or
shell_result7 is changed
- name: execute a shell command with trailing newline to stdin
shell: cat > {{output_dir_test | expanduser}}/afile.txt
args:
stdin: test
stdin_add_newline: yes
- name: make sure content matches expected
copy:
dest: "{{output_dir_test | expanduser}}/afile.txt"
content: |
test
register: shell_result8
failed_when:
- shell_result8 is failed or
shell_result8 is changed
- name: execute a shell command with trailing newline to stdin, default
shell: cat > {{output_dir_test | expanduser}}/afile.txt
args:
stdin: test
- name: make sure content matches expected
copy:
dest: "{{output_dir_test | expanduser}}/afile.txt"
content: |
test
register: shell_result9
failed_when:
- shell_result9 is failed or
shell_result9 is changed
- name: remove the previously created file
file: path={{output_dir_test}}/afile.txt state=absent