From 451428af04876bc82b014111152d242ff647eddd Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Fri, 12 Feb 2021 13:01:47 +0100 Subject: [PATCH] Prevented the expansion of parameters in run_command() (#1794) (#1797) (cherry picked from commit 436bbb0077e21a68f4e226662362516e2e8404ba) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- .../fragments/1776-git_config-tilde_value.yml | 2 ++ plugins/modules/source_control/git_config.py | 12 +++---- .../targets/git_config/tasks/main.yml | 2 ++ .../git_config/tasks/set_value_with_tilde.yml | 33 +++++++++++++++++++ 4 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/1776-git_config-tilde_value.yml create mode 100644 tests/integration/targets/git_config/tasks/set_value_with_tilde.yml diff --git a/changelogs/fragments/1776-git_config-tilde_value.yml b/changelogs/fragments/1776-git_config-tilde_value.yml new file mode 100644 index 0000000000..c98912a24d --- /dev/null +++ b/changelogs/fragments/1776-git_config-tilde_value.yml @@ -0,0 +1,2 @@ +bugfixes: + - git_config - prevent ``run_command`` from expanding values (https://github.com/ansible-collections/community.general/issues/1776). diff --git a/plugins/modules/source_control/git_config.py b/plugins/modules/source_control/git_config.py index 66ef45f354..8c63a8436f 100644 --- a/plugins/modules/source_control/git_config.py +++ b/plugins/modules/source_control/git_config.py @@ -151,7 +151,6 @@ config_values: import os from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.six.moves import shlex_quote def main(): @@ -216,7 +215,7 @@ def main(): # Run from root directory to avoid accidentally picking up any local config settings dir = "/" - (rc, out, err) = module.run_command(' '.join(args), cwd=dir) + (rc, out, err) = module.run_command(args, cwd=dir, expand_user_and_vars=False) if params['list_all'] and scope and rc == 128 and 'unable to read config file' in err: # This just means nothing has been set at the given scope module.exit_json(changed=False, msg='', config_values={}) @@ -243,17 +242,16 @@ def main(): if not module.check_mode: if unset: args.insert(len(args) - 1, "--" + unset) - cmd = ' '.join(args) + cmd = args else: - new_value_quoted = shlex_quote(new_value) - cmd = ' '.join(args + [new_value_quoted]) + cmd = args + [new_value] try: # try using extra parameter from ansible-base 2.10.4 onwards - (rc, out, err) = module.run_command(cmd, cwd=dir, ignore_invalid_cwd=False) + (rc, out, err) = module.run_command(cmd, cwd=dir, ignore_invalid_cwd=False, expand_user_and_vars=False) except TypeError: # @TODO remove try/except when community.general drop support for 2.10.x if not os.path.isdir(dir): module.fail_json(msg="Cannot find directory '{0}'".format(dir)) - (rc, out, err) = module.run_command(cmd, cwd=dir) + (rc, out, err) = module.run_command(cmd, cwd=dir, expand_user_and_vars=False) if err: module.fail_json(rc=rc, msg=err, cmd=cmd) diff --git a/tests/integration/targets/git_config/tasks/main.yml b/tests/integration/targets/git_config/tasks/main.yml index 36eee37013..74127eb5c6 100644 --- a/tests/integration/targets/git_config/tasks/main.yml +++ b/tests/integration/targets/git_config/tasks/main.yml @@ -24,5 +24,7 @@ - import_tasks: precedence_between_unset_and_value.yml # testing state=absent with check mode - import_tasks: unset_check_mode.yml + # testing for case in issue #1776 + - import_tasks: set_value_with_tilde.yml when: git_installed is succeeded and git_version.stdout is version(git_version_supporting_includes, ">=") ... diff --git a/tests/integration/targets/git_config/tasks/set_value_with_tilde.yml b/tests/integration/targets/git_config/tasks/set_value_with_tilde.yml new file mode 100644 index 0000000000..f55eb73066 --- /dev/null +++ b/tests/integration/targets/git_config/tasks/set_value_with_tilde.yml @@ -0,0 +1,33 @@ +--- +#- import_tasks: setup_no_value.yml + +- name: setting value + git_config: + name: core.hooksPath + value: '~/foo/bar' + state: present + scope: global + register: set_result + +- name: setting value again + git_config: + name: core.hooksPath + value: '~/foo/bar' + state: present + scope: global + register: set_result2 + +- name: getting value + git_config: + name: core.hooksPath + scope: global + register: get_result + +- name: assert set changed and value is correct + assert: + that: + - set_result is changed + - set_result2 is not changed + - get_result is not changed + - get_result.config_value == '~/foo/bar' +...