ansible.builtin.template – Template a file out to a target host¶
Note
This module is part of ansible-base
and included in all Ansible
installations. In most cases, you can use the short module name
template even without specifying the collections:
keyword.
Despite that, we recommend you use the FQCN for easy linking to the module
documentation and to avoid conflicting with other collections that may have
the same module name.
Synopsis¶
Templates are processed by the Jinja2 templating language.
Documentation on the template formatting can be found in the Template Designer Documentation.
Additional variables listed below can be used in templates.
ansible_managed
(configurable via thedefaults
section ofansible.cfg
) contains a string which can be used to describe the template name, host, modification time of the template file and the owner uid.template_host
contains the node name of the template’s machine.template_uid
is the numeric user id of the owner.template_path
is the path of the template.template_fullpath
is the absolute path of the template.template_destpath
is the path of the template on the remote system (added in 2.8).template_run_date
is the date that the template was rendered.
Note
This module has a corresponding action plugin.
Parameters¶
Notes¶
Note
For Windows you can use ansible.windows.win_template which uses ‘\r\n’ as
newline_sequence
by default.Including a string that uses a date in the template will result in the template being marked ‘changed’ each time.
Since Ansible 0.9, templates are loaded with
trim_blocks=True
.Also, you can override jinja2 settings by adding a special header to template file. i.e.
#jinja2:variable_start_string:'[%', variable_end_string:'%]', trim_blocks: False
which changes the variable interpolation markers to[% var %]
instead of{{ var }}
. This is the best way to prevent evaluation of things that look like, but should not be Jinja2.Using raw/endraw in Jinja2 will not work as you expect because templates in Ansible are recursively evaluated.
To find Byte Order Marks in files, use
Format-Hex <file> -Count 16
on Windows, and useod -a -t x1 -N 16 <file>
on Linux.
See Also¶
See also
- ansible.builtin.copy
The official documentation on the ansible.builtin.copy module.
- ansible.windows.win_copy
The official documentation on the ansible.windows.win_copy module.
- ansible.windows.win_template
The official documentation on the ansible.windows.win_template module.
Examples¶
- name: Template a file to /etc/file.conf
ansible.builtin.template:
src: /mytemplates/foo.j2
dest: /etc/file.conf
owner: bin
group: wheel
mode: '0644'
- name: Template a file, using symbolic modes (equivalent to 0644)
ansible.builtin.template:
src: /mytemplates/foo.j2
dest: /etc/file.conf
owner: bin
group: wheel
mode: u=rw,g=r,o=r
- name: Copy a version of named.conf that is dependent on the OS. setype obtained by doing ls -Z /etc/named.conf on original file
ansible.builtin.template:
src: named.conf_{{ ansible_os_family }}.j2
dest: /etc/named.conf
group: named
setype: named_conf_t
mode: 0640
- name: Create a DOS-style text file from a template
ansible.builtin.template:
src: config.ini.j2
dest: /share/windows/config.ini
newline_sequence: '\r\n'
- name: Copy a new sudoers file into place, after passing validation with visudo
ansible.builtin.template:
src: /mine/sudoers
dest: /etc/sudoers
validate: /usr/sbin/visudo -cf %s
- name: Update sshd configuration safely, avoid locking yourself out
ansible.builtin.template:
src: etc/ssh/sshd_config.j2
dest: /etc/ssh/sshd_config
owner: root
group: root
mode: '0600'
validate: /usr/sbin/sshd -t -f %s
backup: yes
Authors¶
Ansible Core Team
Michael DeHaan