Browse code

Handle complex quoting in extra args in pull cli (#50212)

* added tests

fixes #40729

(cherry picked from commit b6824669dfff157f3f72640f78f675ddfbf71ae5)
(cherry picked from commit e0100341517fa896f7b056b73d4092935cd7cd58)

Brian Coca authored on 2019/01/24 00:58:37
Showing 4 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,2 @@
0
+bugfixes:
1
+    - fix ansible-pull hanlding of extra args, complex quoting is needed for inline JSON
... ...
@@ -31,6 +31,7 @@ import time
31 31
 from ansible.cli import CLI
32 32
 from ansible.errors import AnsibleOptionsError
33 33
 from ansible.module_utils._text import to_native, to_text
34
+from ansible.module_utils.six.moves import shlex_quote
34 35
 from ansible.plugins.loader import module_loader
35 36
 from ansible.utils.cmd_functions import run_cmd
36 37
 
... ...
@@ -228,7 +229,7 @@ class PullCLI(CLI):
228 228
         cmd = '%s/ansible %s %s -m %s -a "%s" all -l "%s"' % (bin_path, inv_opts, base_opts, self.options.module_name, repo_opts, limit_opts)
229 229
 
230 230
         for ev in self.options.extra_vars:
231
-            cmd += ' -e "%s"' % ev
231
+            cmd += ' -e %s' % shlex_quote(ev)
232 232
 
233 233
         # Nap?
234 234
         if self.options.sleep:
... ...
@@ -263,7 +264,7 @@ class PullCLI(CLI):
263 263
                 cmd += " --vault-id=%s" % vault_id
264 264
 
265 265
         for ev in self.options.extra_vars:
266
-            cmd += ' -e "%s"' % ev
266
+            cmd += ' -e %s' % shlex_quote(ev)
267 267
         if self.options.ask_sudo_pass or self.options.ask_su_pass or self.options.become_ask_pass:
268 268
             cmd += ' --ask-become-pass'
269 269
         if self.options.skip_tags:
... ...
@@ -11,3 +11,9 @@
11 11
       failed_when: "'testhost1.example.com' == inventory_hostname"
12 12
     - name: final task, has to be reached for the test to succeed
13 13
       debug: msg="MAGICKEYWORD"
14
+
15
+    - name: check that extra vars are correclty passed
16
+      assert:
17
+        that:
18
+            - docker_registries_login is defined
19
+      tags: ['never', 'test_ev']
... ...
@@ -23,21 +23,34 @@ cd "${repo_dir}"
23 23
     git commit -m "Initial commit."
24 24
 )
25 25
 
26
+function pass_tests {
27
+	# test for https://github.com/ansible/ansible/issues/13688
28
+	if ! grep MAGICKEYWORD "${temp_log}"; then
29
+	    echo "Missing MAGICKEYWORD in output."
30
+	    exit 1
31
+	fi
32
+
33
+	# test for https://github.com/ansible/ansible/issues/13681
34
+	if egrep '127\.0\.0\.1.*ok' "${temp_log}"; then
35
+	    echo "Found host 127.0.0.1 in output. Only localhost should be present."
36
+	    exit 1
37
+	fi
38
+	# make sure one host was run
39
+	if ! egrep 'localhost.*ok' "${temp_log}"; then
40
+	    echo "Did not find host localhost in output."
41
+	    exit 1
42
+	fi
43
+}
44
+
26 45
 ANSIBLE_CONFIG='' ansible-pull -d "${pull_dir}" -U "${repo_dir}" "$@" | tee "${temp_log}"
27 46
 
28
-# test for https://github.com/ansible/ansible/issues/13688
29
-if ! grep MAGICKEYWORD "${temp_log}"; then
30
-    echo "Missing MAGICKEYWORD in output."
31
-    exit 1
32
-fi
33
-
34
-# test for https://github.com/ansible/ansible/issues/13681
35
-if egrep '127\.0\.0\.1.*ok' "${temp_log}"; then
36
-    echo "Found host 127.0.0.1 in output. Only localhost should be present."
37
-    exit 1
38
-fi
39
-# make sure one host was run
40
-if ! egrep 'localhost.*ok' "${temp_log}"; then
41
-    echo "Did not find host localhost in output."
42
-    exit 1
43
-fi
47
+pass_tests
48
+
49
+# ensure complex extra vars work
50
+PASSWORD='test'
51
+USER=${USER:-'broken_docker'}
52
+JSON_EXTRA_ARGS='{"docker_registries_login": [{ "docker_password": "'"${PASSWORD}"'", "docker_username": "'"${USER}"'", "docker_registry_url":"repository-manager.company.com:5001"}], "docker_registries_logout": [{ "docker_password": "'"${PASSWORD}"'", "docker_username": "'"${USER}"'", "docker_registry_url":"repository-manager.company.com:5001"}] }'
53
+
54
+ANSIBLE_CONFIG='' ansible-pull -d "${pull_dir}" -U "${repo_dir}" -e "${JSON_EXTRA_ARGS}" "$@" --tags untagged,test_ev | tee "${temp_log}"
55
+
56
+pass_tests