Fix handling of conditional vars_files which contain variables

Fixes #12484
This commit is contained in:
James Cammarata 2015-09-23 01:25:58 -04:00
parent c83f51b7f2
commit 95c6fe88e4

View file

@ -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):