From 04e3c964fba86fd7fca449ebf55d49372b64d479 Mon Sep 17 00:00:00 2001 From: tranceitionalMynd Date: Fri, 25 May 2018 14:40:48 -0500 Subject: [PATCH] Added executable parameter to the script module (#36969) * Added interpreter parameter to the script module * Let required and default parameters get documented implicitly for binary parameter * Renamed interpreter parameter to executable --- lib/ansible/modules/commands/script.py | 14 ++++++++++++++ lib/ansible/plugins/action/script.py | 9 ++++++++- .../targets/script/files/no_shebang.py | 3 +++ test/integration/targets/script/tasks/main.yml | 18 ++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 test/integration/targets/script/files/no_shebang.py diff --git a/lib/ansible/modules/commands/script.py b/lib/ansible/modules/commands/script.py index badafb3e3e6..424d5029ea1 100644 --- a/lib/ansible/modules/commands/script.py +++ b/lib/ansible/modules/commands/script.py @@ -40,6 +40,10 @@ options: description: - cd into this directory on the remote node before running the script version_added: "2.4" + executable: + description: + - Name or path of a executable to invoke the script with + version_added: "2.6" notes: - It is usually preferable to write Ansible modules than pushing scripts. Convert your script to an Ansible module for bonus points! - The ssh connection plugin will force pseudo-tty allocation via -tt when scripts are executed. pseudo-ttys do not have a stderr channel and all @@ -66,4 +70,14 @@ EXAMPLES = ''' - script: /some/local/remove_file.sh --some-arguments 1234 args: removes: /the/removed/file.txt + +# Run a script using a executable in a non-system path +- script: /some/local/script + args: + executable: /some/remote/executable + +# Run a script using a executable in a system path +- script: /some/local/script.py + args: + executable: python3 ''' diff --git a/lib/ansible/plugins/action/script.py b/lib/ansible/plugins/action/script.py index 000b2951f12..76e0d5c9f7e 100644 --- a/lib/ansible/plugins/action/script.py +++ b/lib/ansible/plugins/action/script.py @@ -79,6 +79,9 @@ class ActionModule(ActionBase): parts = [to_text(s, errors='surrogate_or_strict') for s in shlex.split(raw_params.strip())] source = parts[0] + # Support executable paths and files with spaces in the name. + executable = to_native(self._task.args.get('executable', ''), errors='surrogate_or_strict') + try: source = self._loader.get_real_file(self._find_needle('files', source), decrypt=self._task.args.get('decrypt', True)) except AnsibleError as e: @@ -109,7 +112,11 @@ class ActionModule(ActionBase): # add preparation steps to one ssh roundtrip executing the script env_dict = dict() env_string = self._compute_environment_string(env_dict) - script_cmd = ' '.join([env_string, target_command]) + + if executable: + script_cmd = ' '.join([env_string, executable, target_command]) + else: + script_cmd = ' '.join([env_string, target_command]) if self._play_context.check_mode: raise _AnsibleActionDone() diff --git a/test/integration/targets/script/files/no_shebang.py b/test/integration/targets/script/files/no_shebang.py new file mode 100644 index 00000000000..c6c813afc45 --- /dev/null +++ b/test/integration/targets/script/files/no_shebang.py @@ -0,0 +1,3 @@ +import sys + +sys.stdout.write("Script with shebang omitted") diff --git a/test/integration/targets/script/tasks/main.yml b/test/integration/targets/script/tasks/main.yml index 37975ca5d11..e7913c9a9e8 100644 --- a/test/integration/targets/script/tasks/main.yml +++ b/test/integration/targets/script/tasks/main.yml @@ -221,3 +221,21 @@ that: - _check_mode_test3 is skipped - '_check_mode_test3.msg == "{{ output_dir_test | expanduser }}/afile2.txt does not exist, matching removes option"' + +# executable + +- name: Run script with shebang omitted + script: no_shebang.py + args: + executable: python + register: _shebang_omitted_test + tags: + - noshebang + +- name: Assert that script with shebang omitted succeeded + assert: + that: + - _shebang_omitted_test is success + - _shebang_omitted_test.stdout == 'Script with shebang omitted' + tags: + - noshebang