Show file instead of vars on load error (#86282)

(cherry picked from commit 490795770f34120adc02b08fc24fc50be6ac68a2)
(cherry picked from commit 11a2d02bf2)
pull/86283/head
Brian Coca 1 day ago committed by GitHub
parent 3c4f0f99bc
commit 1c1e37654a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,2 @@
bugfixes:
- Variable loading now uses file source instead of variables when invalidly formmated vars file is loaded.

@ -54,7 +54,7 @@ DOCUMENTATION = '''
''' '''
import os import os
from ansible.errors import AnsibleParserError from ansible.errors import AnsibleError, AnsibleParserError
from ansible.module_utils.common.text.converters import to_native from ansible.module_utils.common.text.converters import to_native
from ansible.plugins.vars import BaseVarsPlugin from ansible.plugins.vars import BaseVarsPlugin
from ansible.utils.path import basedir from ansible.utils.path import basedir
@ -76,7 +76,10 @@ class VarsModule(BaseVarsPlugin):
for found in found_files: for found in found_files:
new_data = loader.load_from_file(found, cache=True, unsafe=True) new_data = loader.load_from_file(found, cache=True, unsafe=True)
if new_data: # ignore empty files if new_data: # ignore empty files
try:
data = combine_vars(data, new_data) data = combine_vars(data, new_data)
except AnsibleError as e:
raise AnsibleParserError("Could not process '%s'." % found, orig_exc=e)
return data return data
def get_vars(self, loader, path, entities, cache=True): def get_vars(self, loader, path, entities, cache=True):

@ -73,9 +73,7 @@ def _validate_mutable_mappings(a, b):
myvars.append(dumps(x)) myvars.append(dumps(x))
except Exception: except Exception:
myvars.append(to_native(x)) myvars.append(to_native(x))
raise AnsibleError("failed to combine variables, expected dicts but got a '{0}' and a '{1}': \n{2}\n{3}".format( raise AnsibleError("failed to combine variables, expected dicts but got a '{0}' and a '{1}'.".format(a.__class__.__name__, b.__class__.__name__))
a.__class__.__name__, b.__class__.__name__, myvars[0], myvars[1])
)
def combine_vars(a, b, merge=None): def combine_vars(a, b, merge=None):

@ -63,7 +63,7 @@ def preprocess_vars(a):
for item in data: for item in data:
if not isinstance(item, MutableMapping): if not isinstance(item, MutableMapping):
raise AnsibleError("variable files must contain either a dictionary of variables, or a list of dictionaries. Got: %s (%s)" % (a, type(a))) raise AnsibleError("variable files must contain either a dictionary of variables, or a list of dictionaries. Got: %s " % (type(a)))
return data return data
@ -364,6 +364,8 @@ class VariableManager:
continue continue
except AnsibleParserError: except AnsibleParserError:
raise raise
except AnsibleError as e:
raise AnsibleError("Invalid vars_file '%s'." % found_file, orig_exc=e)
else: else:
# if include_delegate_to is set to False or we don't have a host, we ignore the missing # if include_delegate_to is set to False or we don't have a host, we ignore the missing
# vars file here because we're working on a delegated host or require host vars, see NOTE above # vars file here because we're working on a delegated host or require host vars, see NOTE above

@ -0,0 +1,14 @@
- hosts: all
name: test vault errors
gather_facts: false
tasks:
- name: bad included vault
include_vars:
file: vars/bad_vault.yml
tags: includevault, never
no_log: false
- name: Show bad vault contents ... if i get here, it was a good vault!
debug:
msg: "{{ test_password }} {{ jdbc_test_password }} {{ api_test_password }}"

@ -3,3 +3,12 @@
set -eux set -eux
ansible-playbook test_var_blending.yml -i inventory -e @test_vars.yml -v "$@" ansible-playbook test_var_blending.yml -i inventory -e @test_vars.yml -v "$@"
# check bad vault file erros
[ "$(ansible-playbook error_handling.yml -i inventory --vault-password-file supersecretvaultsecret -e @vars/bad_vault.yml 2>&1 | grep -c 'dummy')" -eq "0" ]
[ "$(ansible-playbook error_handling.yml -i inventory --vault-password-file supersecretvaultsecret --tags includevault 2>&1 | grep -c 'dummy')" -eq "0" ]
# setup group file for bad vault tests
trap 'rm group_vars/local/bad_vault.yml' EXIT
ln -s "${PWD}/vars/bad_vault.yml" group_vars/local/
[ "$(ansible-playbook error_handling.yml -i inventory --vault-password-file supersecretvaultsecret 2>&1 | grep -c 'dummy')" -eq "0" ]

@ -0,0 +1,10 @@
$ANSIBLE_VAULT;1.1;AES256
65616232393566313961313431386633366639386133383230656230333934656366363636393730
3266313739663639636161373033333865303461343936370a356463393635623664316464353061
63396639643331303231616336663562303764653733326532316139643036336436396565653531
3836383032626262620a316666363164626537346663383333376330623339633762363932613537
32663462613532646139633364363136656132346661373331363164356162313762343337393666
38653864363938316534333438643761623264376535336233656630376430346366333262313532
33366662663137306431623464303561383730336466613166386136656364306436343032343631
37396234633230626263373435623731616664653939343630393935626461396230663734373861
3634
Loading…
Cancel
Save