Skip to content

template

Collection Note

This module is part of the ansible.builtin collection. To install the collection, use:

ansible-galaxy collection install ansible.builtin
Added in version 0.9.

Synopsis

  • 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.

Parameters

Parameter Defaults / Choices Comments
_terms list of files to template
comment_end_string
str
Default: #}
The string marking the end of a comment statement.
Version Added: 2.12
comment_start_string
str
Default: {#
The string marking the beginning of a comment statement.
Version Added: 2.12
convert_data
bool
Default: True
Whether to convert YAML into data. If V(False), strings that are YAML will be left untouched.
Mutually exclusive with the O(jinja2_native) option.
jinja2_native
bool
Default: True
Controls whether to use Jinja2 native types.
It is off by default even if global O(jinja2_native) is V(True).
Has no effect if global O(jinja2_native) is V(False).
This offers more flexibility than the template module which does not use Jinja2 native types at all.
Version Added: 2.11
template_vars
dict
A dictionary, the keys become additional variables available for templating.
Version Added: 2.3
trim_blocks
bool
Default: True
Determine when newlines should be removed from blocks.
When set to V(yes) the first newline after a block is removed (block, not variable tag!).
Version Added: 2.19
variable_end_string
str
Default: }}
The string marking the end of a print statement.
Version Added: 2.8
variable_start_string
str
Default: {{
The string marking the beginning of a print statement.
Version Added: 2.8

Examples

- name: show templating results
  ansible.builtin.debug:
    msg: "{{ lookup('ansible.builtin.template', './some_template.j2') }}"

- name: show templating results with different variable start and end string
  ansible.builtin.debug:
    msg: "{{ lookup('ansible.builtin.template', './some_template.j2', variable_start_string='[%', variable_end_string='%]') }}"

- name: show templating results with different comment start and end string
  ansible.builtin.debug:
    msg: "{{ lookup('ansible.builtin.template', './some_template.j2', comment_start_string='[#', comment_end_string='#]') }}"

- name: show templating results with trim_blocks
  ansible.builtin.debug:
    msg: "{{ lookup('ansible.builtin.template', './some_template.j2', trim_blocks=True) }}"

Return Values

Key Data Type Description Returned
_raw list file(s) content after templating

Authors

  • Michael Dehaan