From b31b5d105a84aa14b17302d2778c7606c78912bf Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Thu, 19 Dec 2019 18:01:22 +0530 Subject: [PATCH] vars: Fail with warning for extra_vars filename without @ sign (#59915) Rather than silently processing extra_vars filename without @ sign, CLI now fails with appropriate warning about requirement. Fixes: #51857 Signed-off-by: Abhijeet Kasurde --- changelogs/fragments/extra_vars_with_at_sign.yml | 2 ++ lib/ansible/utils/vars.py | 7 ++++++- test/integration/targets/ansible/playbook.yml | 5 +++++ test/integration/targets/ansible/runme.sh | 14 ++++++++++++++ test/integration/targets/ansible/vars.yml | 1 + 5 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/extra_vars_with_at_sign.yml create mode 100644 test/integration/targets/ansible/playbook.yml create mode 100644 test/integration/targets/ansible/vars.yml diff --git a/changelogs/fragments/extra_vars_with_at_sign.yml b/changelogs/fragments/extra_vars_with_at_sign.yml new file mode 100644 index 0000000000..277b628bb7 --- /dev/null +++ b/changelogs/fragments/extra_vars_with_at_sign.yml @@ -0,0 +1,2 @@ +minor_changes: + - Ansible CLI fails with warning if extra_vars parameter is used with filename without @ sign (https://github.com/ansible/ansible/issues/51857). diff --git a/lib/ansible/utils/vars.py b/lib/ansible/utils/vars.py index bc6fece057..b0d5bc5b79 100644 --- a/lib/ansible/utils/vars.py +++ b/lib/ansible/utils/vars.py @@ -125,10 +125,15 @@ def load_extra_vars(loader): for extra_vars_opt in context.CLIARGS.get('extra_vars', tuple()): data = None extra_vars_opt = to_text(extra_vars_opt, errors='surrogate_or_strict') + if extra_vars_opt is None or not extra_vars_opt: + continue + if extra_vars_opt.startswith(u"@"): # Argument is a YAML file (JSON is a subset of YAML) data = loader.load_from_file(extra_vars_opt[1:]) - elif extra_vars_opt and extra_vars_opt[0] in u'[{': + elif extra_vars_opt[0] in [u'/', u'.']: + raise AnsibleOptionsError("Please prepend extra_vars filename '%s' with '@'" % extra_vars_opt) + elif extra_vars_opt[0] in [u'[', u'{']: # Arguments as YAML data = loader.load(extra_vars_opt) else: diff --git a/test/integration/targets/ansible/playbook.yml b/test/integration/targets/ansible/playbook.yml new file mode 100644 index 0000000000..c38b906077 --- /dev/null +++ b/test/integration/targets/ansible/playbook.yml @@ -0,0 +1,5 @@ +- hosts: all + gather_facts: false + tasks: + - debug: + msg: "{{ username }}" diff --git a/test/integration/targets/ansible/runme.sh b/test/integration/targets/ansible/runme.sh index 7d264cfe9d..6561d09588 100755 --- a/test/integration/targets/ansible/runme.sh +++ b/test/integration/targets/ansible/runme.sh @@ -45,3 +45,17 @@ if [[ $file_count -ne 1 ]]; then fi exit 1 fi + +# Ensure extra vars filename is prepended with '@' sign +if ansible-playbook -i ../../inventory --extra-vars /tmp/non-existing-file playbook.yml; then + echo "extra_vars filename without '@' sign should cause failure" + exit 1 +fi + +# Ensure extra vars filename is prepended with '@' sign +if ansible-playbook -i ../../inventory --extra-vars ./vars.yml playbook.yml; then + echo "extra_vars filename without '@' sign should cause failure" + exit 1 +fi + +ansible-playbook -i ../../inventory --extra-vars @./vars.yml playbook.yml diff --git a/test/integration/targets/ansible/vars.yml b/test/integration/targets/ansible/vars.yml new file mode 100644 index 0000000000..a19e454f65 --- /dev/null +++ b/test/integration/targets/ansible/vars.yml @@ -0,0 +1 @@ +username: ansiboy