| 1 |
# Copyright: (c) 2012, Michael DeHaan <[email protected]> |
| 2 |
# Copyright: (c) 2012-17, Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
DOCUMENTATION = """ |
| 8 |
name: template |
| 9 |
author: Michael DeHaan |
| 10 |
version_added: "0.9" |
| 11 |
short_description: retrieve contents of file after templating with Jinja2 |
| 12 |
description: |
| 13 |
- Returns a list of strings; for each template in the list of templates you pass in, returns a string containing the results of processing that template. |
| 14 |
positional: _terms |
| 15 |
options: |
| 16 |
_terms: |
| 17 |
description: list of files to template |
| 18 |
convert_data: |
| 19 |
type: bool |
| 20 |
description: |
| 21 |
- Whether to convert YAML into data. If V(False), strings that are YAML will be left untouched. |
| 22 |
- Mutually exclusive with the O(jinja2_native) option. |
| 23 |
default: true |
| 24 |
deprecated: |
| 25 |
why: This option is no longer used in the Ansible Core code base. |
| 26 |
version: "2.23" |
| 27 |
alternatives: Jinja2 native mode is now the default and only option, which is mutually exclusive with this option. |
| 28 |
variable_start_string: |
| 29 |
description: The string marking the beginning of a print statement. |
| 30 |
default: '{{' |
| 31 |
version_added: '2.8' |
| 32 |
type: str |
| 33 |
variable_end_string: |
| 34 |
description: The string marking the end of a print statement. |
| 35 |
default: '}}' |
| 36 |
version_added: '2.8' |
| 37 |
type: str |
| 38 |
jinja2_native: |
| 39 |
description: |
| 40 |
- Controls whether to use Jinja2 native types. |
| 41 |
- It is off by default even if global O(jinja2_native) is V(True). |
| 42 |
- Has no effect if global O(jinja2_native) is V(False). |
| 43 |
- This offers more flexibility than the template module which does not use Jinja2 native types at all. |
| 44 |
default: True |
| 45 |
version_added: '2.11' |
| 46 |
type: bool |
| 47 |
deprecated: |
| 48 |
why: This option is no longer used in the Ansible Core code base. |
| 49 |
version: "2.23" |
| 50 |
alternatives: Jinja2 native mode is now the default and only option. |
| 51 |
template_vars: |
| 52 |
description: A dictionary, the keys become additional variables available for templating. |
| 53 |
default: {} |
| 54 |
version_added: '2.3' |
| 55 |
type: dict |
| 56 |
comment_start_string: |
| 57 |
description: The string marking the beginning of a comment statement. |
| 58 |
version_added: '2.12' |
| 59 |
type: str |
| 60 |
default: '{#' |
| 61 |
comment_end_string: |
| 62 |
description: The string marking the end of a comment statement. |
| 63 |
version_added: '2.12' |
| 64 |
type: str |
| 65 |
default: '#}' |
| 66 |
trim_blocks: |
| 67 |
description: |
| 68 |
- Determine when newlines should be removed from blocks. |
| 69 |
- When set to V(yes) the first newline after a block is removed (block, not variable tag!). |
| 70 |
type: bool |
| 71 |
default: yes |
| 72 |
version_added: '2.19' |
| 73 |
seealso: |
| 74 |
- ref: playbook_task_paths |
| 75 |
description: Search paths used for relative templates. |
| 76 |
""" |
| 77 |
|
| 78 |
EXAMPLES = """ |
| 79 |
- name: show templating results |
| 80 |
ansible.builtin.debug: |
| 81 |
msg: "{{ lookup('ansible.builtin.template', './some_template.j2') }}" |
| 82 |
|
| 83 |
- name: show templating results with different variable start and end string |
| 84 |
ansible.builtin.debug: |
| 85 |
msg: "{{ lookup('ansible.builtin.template', './some_template.j2', variable_start_string='[%', variable_end_string='%]') }}" |
| 86 |
|
| 87 |
- name: show templating results with different comment start and end string |
| 88 |
ansible.builtin.debug: |
| 89 |
msg: "{{ lookup('ansible.builtin.template', './some_template.j2', comment_start_string='[#', comment_end_string='#]') }}" |
| 90 |
|
| 91 |
- name: show templating results with trim_blocks |
| 92 |
ansible.builtin.debug: |
| 93 |
msg: "{{ lookup('ansible.builtin.template', './some_template.j2', trim_blocks=True) }}" |
| 94 |
|
| 95 |
""" |
| 96 |
|
| 97 |
RETURN = """ |
| 98 |
_raw: |
| 99 |
description: file(s) content after templating |
| 100 |
type: list |
| 101 |
elements: raw |
| 102 |
""" |
| 103 |
|
| 104 |
from copy import deepcopy |
| 105 |
import os |
| 106 |
|
| 107 |
from ansible.errors import AnsibleError |
| 108 |
from ansible.plugins.lookup import LookupBase |
| 109 |
from ansible.template import trust_as_template |
| 110 |
from ansible._internal._templating import _template_vars |
| 111 |
from ansible._internal._templating._engine import TemplateOptions, TemplateOverrides |
| 112 |
from ansible.utils.display import Display |
| 113 |
|
| 114 |
|
| 115 |
display = Display() |
| 116 |
|
| 117 |
|
| 118 |
class LookupModule(LookupBase): |
| 119 |
|
| 120 |
def run(self, terms, variables=None, **kwargs): |
| 121 |
|
| 122 |
ret = [] |
| 123 |
|
| 124 |
self.set_options(var_options=variables, direct=kwargs) |
| 125 |
|
| 126 |
# capture options |
| 127 |
lookup_template_vars = self.get_option('template_vars') |
| 128 |
variable_start_string = self.get_option('variable_start_string') |
| 129 |
variable_end_string = self.get_option('variable_end_string') |
| 130 |
comment_start_string = self.get_option('comment_start_string') |
| 131 |
comment_end_string = self.get_option('comment_end_string') |
| 132 |
trim_blocks = self.get_option('trim_blocks') |
| 133 |
|
| 134 |
templar = self._templar |
| 135 |
|
| 136 |
for term in terms: |
| 137 |
display.debug("File lookup term: %s" % term) |
| 138 |
|
| 139 |
lookupfile = self.find_file_in_search_path(variables, 'templates', term) |
| 140 |
display.vvvv("File lookup using %s as file" % lookupfile) |
| 141 |
if lookupfile: |
| 142 |
template_data = trust_as_template(self._loader.get_text_file_contents(lookupfile)) |
| 143 |
|
| 144 |
# set jinja2 internal search path for includes |
| 145 |
searchpath = variables.get('ansible_search_path', []) |
| 146 |
if searchpath: |
| 147 |
# our search paths aren't actually the proper ones for jinja includes. |
| 148 |
# We want to search into the 'templates' subdir of each search path in |
| 149 |
# addition to our original search paths. |
| 150 |
newsearchpath = [] |
| 151 |
for p in searchpath: |
| 152 |
newsearchpath.append(os.path.join(p, 'templates')) |
| 153 |
newsearchpath.append(p) |
| 154 |
searchpath = newsearchpath |
| 155 |
searchpath.insert(0, os.path.dirname(lookupfile)) |
| 156 |
|
| 157 |
# The template will have access to all existing variables, |
| 158 |
# plus some added by ansible (e.g., template_{path,mtime}), |
| 159 |
# plus anything passed to the lookup with the template_vars= |
| 160 |
# argument. |
| 161 |
# FIXME: why isn't this a chainmap with a sacrificial bottom layer? |
| 162 |
vars = deepcopy(variables) |
| 163 |
vars.update(_template_vars.generate_ansible_template_vars( |
| 164 |
path=term, |
| 165 |
fullpath=lookupfile, |
| 166 |
include_ansible_managed='ansible_managed' not in vars, # do not clobber ansible_managed when set by the user |
| 167 |
)) |
| 168 |
vars.update(lookup_template_vars) |
| 169 |
|
| 170 |
overrides = dict( |
| 171 |
variable_start_string=variable_start_string, |
| 172 |
variable_end_string=variable_end_string, |
| 173 |
comment_start_string=comment_start_string, |
| 174 |
comment_end_string=comment_end_string, |
| 175 |
trim_blocks=trim_blocks, |
| 176 |
) |
| 177 |
|
| 178 |
data_templar = templar.copy_with_new_env(available_variables=vars, searchpath=searchpath) |
| 179 |
# use the internal template API to avoid forced top-level finalization behavior imposed by the public API |
| 180 |
res = data_templar._engine.template(template_data, options=TemplateOptions( |
| 181 |
escape_backslashes=False, |
| 182 |
overrides=TemplateOverrides.from_kwargs(overrides), |
| 183 |
)) |
| 184 |
|
| 185 |
ret.append(res) |
| 186 |
else: |
| 187 |
raise AnsibleError("the template file %s could not be found for the lookup" % term) |
| 188 |
|
| 189 |
return ret |