From 95c6fe88e42ca6c1466b67834ec8245724a3721b Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Wed, 23 Sep 2015 01:25:58 -0400 Subject: [PATCH] Fix handling of conditional vars_files which contain variables Fixes #12484 --- lib/ansible/vars/__init__.py | 48 ++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/ansible/vars/__init__.py b/lib/ansible/vars/__init__.py index 11b0a491ea..0d5ea5a09d 100644 --- a/lib/ansible/vars/__init__.py +++ b/lib/ansible/vars/__init__.py @@ -33,7 +33,7 @@ except ImportError: from ansible import constants as C from ansible.cli import CLI -from ansible.errors import AnsibleError, AnsibleUndefinedVariable +from ansible.errors import AnsibleError, AnsibleParserError, AnsibleUndefinedVariable from ansible.inventory.host import Host from ansible.parsing import DataLoader from ansible.plugins.cache import FactCache @@ -221,28 +221,34 @@ class VariableManager: all_vars = combine_vars(all_vars, play.get_vars()) for vars_file_item in play.get_vars_files(): + # create a set of temporary vars here, which incorporate the + # extra vars so we can properly template the vars_files entries + temp_vars = combine_vars(all_vars, self._extra_vars) + templar = Templar(loader=loader, variables=temp_vars) + + # we assume each item in the list is itself a list, as we + # support "conditional includes" for vars_files, which mimics + # the with_first_found mechanism. + #vars_file_list = templar.template(vars_file_item) + vars_file_list = vars_file_item + if not isinstance(vars_file_list, list): + vars_file_list = [ vars_file_list ] + + # now we iterate through the (potential) files, and break out + # as soon as we read one from the list. If none are found, we + # raise an error, which is silently ignored at this point. try: - # create a set of temporary vars here, which incorporate the - # extra vars so we can properly template the vars_files entries - temp_vars = combine_vars(all_vars, self._extra_vars) - templar = Templar(loader=loader, variables=temp_vars) - - # we assume each item in the list is itself a list, as we - # support "conditional includes" for vars_files, which mimics - # the with_first_found mechanism. - vars_file_list = templar.template(vars_file_item) - if not isinstance(vars_file_list, list): - vars_file_list = [ vars_file_list ] - - # now we iterate through the (potential) files, and break out - # as soon as we read one from the list. If none are found, we - # raise an error, which is silently ignored at this point. for vars_file in vars_file_list: - data = preprocess_vars(loader.load_from_file(vars_file)) - if data is not None: - for item in data: - all_vars = combine_vars(all_vars, item) - break + vars_file = templar.template(vars_file) + try: + data = preprocess_vars(loader.load_from_file(vars_file)) + if data is not None: + for item in data: + all_vars = combine_vars(all_vars, item) + break + except AnsibleParserError as e: + # we continue on loader failures + continue else: raise AnsibleError("vars file %s was not found" % vars_file_item) except (UndefinedError, AnsibleUndefinedVariable):