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 <akasurde@redhat.com>
This commit is contained in:
parent
31eb1a1412
commit
b31b5d105a
5 changed files with 28 additions and 1 deletions
2
changelogs/fragments/extra_vars_with_at_sign.yml
Normal file
2
changelogs/fragments/extra_vars_with_at_sign.yml
Normal file
|
@ -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).
|
|
@ -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:
|
||||
|
|
5
test/integration/targets/ansible/playbook.yml
Normal file
5
test/integration/targets/ansible/playbook.yml
Normal file
|
@ -0,0 +1,5 @@
|
|||
- hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- debug:
|
||||
msg: "{{ username }}"
|
|
@ -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
|
||||
|
|
1
test/integration/targets/ansible/vars.yml
Normal file
1
test/integration/targets/ansible/vars.yml
Normal file
|
@ -0,0 +1 @@
|
|||
username: ansiboy
|
Loading…
Reference in a new issue