mirror of
https://github.com/ansible/ansible.git
synced 2026-02-03 20:40:24 -05:00
Show file instead of vars on load error
(cherry picked from commit 490795770f34120adc02b08fc24fc50be6ac68a2)
This commit is contained in:
parent
af15ad6a88
commit
11a2d02bf2
9 changed files with 45 additions and 6 deletions
2
changelogs/fragments/varloaderror.yml
Normal file
2
changelogs/fragments/varloaderror.yml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
bugfixes:
|
||||
- Variable loading now uses file source instead of variables when invalidly formmated vars file is loaded.
|
||||
|
|
@ -53,7 +53,7 @@ DOCUMENTATION = '''
|
|||
'''
|
||||
|
||||
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.plugins.vars import BaseVarsPlugin
|
||||
from ansible.utils.path import basedir
|
||||
|
|
@ -75,7 +75,10 @@ class VarsModule(BaseVarsPlugin):
|
|||
for found in found_files:
|
||||
new_data = loader.load_from_file(found, cache='all', unsafe=True)
|
||||
if new_data: # ignore empty files
|
||||
data = combine_vars(data, new_data)
|
||||
try:
|
||||
data = combine_vars(data, new_data)
|
||||
except AnsibleError as e:
|
||||
raise AnsibleParserError("Could not process '%s'." % found, orig_exc=e)
|
||||
return data
|
||||
|
||||
def get_vars(self, loader, path, entities, cache=True):
|
||||
|
|
|
|||
|
|
@ -69,9 +69,7 @@ def _validate_mutable_mappings(a, b):
|
|||
myvars.append(dumps(x))
|
||||
except Exception:
|
||||
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(
|
||||
a.__class__.__name__, b.__class__.__name__, myvars[0], myvars[1])
|
||||
)
|
||||
raise AnsibleError("failed to combine variables, expected dicts but got a '{0}' and a '{1}'.".format(a.__class__.__name__, b.__class__.__name__))
|
||||
|
||||
|
||||
def combine_vars(a, b, merge=None):
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ def preprocess_vars(a):
|
|||
|
||||
for item in data:
|
||||
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
|
||||
|
||||
|
|
@ -367,6 +367,8 @@ class VariableManager:
|
|||
continue
|
||||
except AnsibleParserError:
|
||||
raise
|
||||
except AnsibleError as e:
|
||||
raise AnsibleError("Invalid vars_file '%s'." % found_file, orig_exc=e)
|
||||
except (UndefinedError, AnsibleUndefinedVariable):
|
||||
if host is not None and self._fact_cache.get(host.name, dict()).get('module_setup') and task is not None:
|
||||
raise AnsibleUndefinedVariable("an undefined variable was found when attempting to template the vars_files item '%s'"
|
||||
|
|
|
|||
14
test/integration/targets/var_blending/error_handling.yml
Normal file
14
test/integration/targets/var_blending/error_handling.yml
Normal file
|
|
@ -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
|
||||
|
||||
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 @@
|
|||
test
|
||||
10
test/integration/targets/var_blending/vars/bad_vault.yml
Normal file
10
test/integration/targets/var_blending/vars/bad_vault.yml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
$ANSIBLE_VAULT;1.1;AES256
|
||||
65616232393566313961313431386633366639386133383230656230333934656366363636393730
|
||||
3266313739663639636161373033333865303461343936370a356463393635623664316464353061
|
||||
63396639643331303231616336663562303764653733326532316139643036336436396565653531
|
||||
3836383032626262620a316666363164626537346663383333376330623339633762363932613537
|
||||
32663462613532646139633364363136656132346661373331363164356162313762343337393666
|
||||
38653864363938316534333438643761623264376535336233656630376430346366333262313532
|
||||
33366662663137306431623464303561383730336466613166386136656364306436343032343631
|
||||
37396234633230626263373435623731616664653939343630393935626461396230663734373861
|
||||
3634
|
||||
Loading…
Reference in a new issue