mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-02 14:40:19 -07:00
centralize ansible_managed and other template vars
now template lookup supports these again.
This commit is contained in:
parent
f7566ef1f1
commit
5b7e8a7bd6
3 changed files with 47 additions and 38 deletions
|
@ -22,6 +22,7 @@ import os
|
|||
from ansible.errors import AnsibleError
|
||||
from ansible.plugins.lookup import LookupBase
|
||||
from ansible.module_utils._text import to_bytes, to_text
|
||||
from ansible.template import generate_ansible_template_vars
|
||||
|
||||
try:
|
||||
from __main__ import display
|
||||
|
@ -47,8 +48,8 @@ class LookupModule(LookupBase):
|
|||
template_data = to_text(f.read(), errors='surrogate_or_strict')
|
||||
|
||||
# set jinja2 internal search path for includes
|
||||
if 'ansible_search_path' in variables:
|
||||
searchpath = variables['ansible_search_path']
|
||||
searchpath = variables.get('ansible_search_path')
|
||||
if searchpath:
|
||||
# our search paths aren't actually the proper ones for jinja includes.
|
||||
# We want to search into the 'templates' subdir of each search path in
|
||||
# addition to our original search paths.
|
||||
|
@ -61,6 +62,11 @@ class LookupModule(LookupBase):
|
|||
searchpath = [self._loader._basedir, os.path.dirname(lookupfile)]
|
||||
self._templar.environment.loader.searchpath = searchpath
|
||||
|
||||
# add ansible 'template' vars
|
||||
temp_vars = variables.copy()
|
||||
temp_vars.update(generate_ansible_template_vars(lookupfile))
|
||||
self._templar.set_available_variables(temp_vars)
|
||||
|
||||
# do the templating
|
||||
res = self._templar.template(template_data, preserve_trailing_newlines=True,convert_data=convert_data_p)
|
||||
ret.append(res)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue