ansible.windows.win_template module – Template a file out to a remote server
Note
This module is part of the ansible.windows collection (version 3.9.1).
It is not included in ansible-core.
To check whether it is installed, run ansible-galaxy collection list.
To install it, use: ansible-galaxy collection install ansible.windows.
To use it in a playbook, specify: ansible.windows.win_template.
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 thedefaultssection 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_hostcontains the node name of the template’s machine.template_uidis the numeric user id of the owner.template_pathis the path of the template.template_fullpathis the absolute path of the template.template_destpathis the path of the template on the remote system (added in 2.8).template_run_dateis the date that the template was rendered.
Note
This module has a corresponding action plugin.
Parameters
Parameter |
Comments |
|---|---|
Determine whether a backup should be created. When set to Choices:
|
|
The string marking the end of a block. Default: |
|
The string marking the beginning of a block. Default: |
|
The string marking the end of a comment statement. Default: |
|
The string marking the beginning of a comment statement. Default: |
|
Location to render the template to on the remote machine. |
|
Determine when the file is being transferred if the destination already exists. When set to When set to Choices:
|
|
Determine when leading spaces and tabs should be stripped. When set to This functionality requires Jinja 2.7 or newer. Choices:
|
|
Specify the newline sequence to use for templating files. Choices:
|
|
Overrides the encoding used to write the template file defined by It defaults to The source template file must always be encoded using Default: |
|
Path of a Jinja2 formatted template on the Ansible controller. This can be a relative or an absolute path. The file must be encoded with |
|
Determine when newlines should be removed from blocks. When set to Choices:
|
|
The validation command to run against the rendered template before it is copied to The rendered template is transferred to a temporary directory on the remote host and its path is substituted into the command wherever the placeholder The substituted path is already quoted for PowerShell so it is safe to use with destinations that contain spaces or backslashes. Do not add quotes of your own around The command is run on the remote Windows host using the configured connection and PowerShell shell. Pipelines and other shell syntax can be used. If the command exits with a non-zero return code the task fails and The temporary copy of the rendered template is removed once the command has run. This option is ignored when the task is run in check mode, as the command could have side effects on the remote host. The command only runs when the rendered content would be copied to |
|
The string marking the end of a print statement. Default: |
|
The string marking the beginning of a print statement. Default: |
Notes
Note
Including a string that uses a date in the template will result in the template being marked ‘changed’ each time.
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: Falsewhich 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 16on Windows, and useod -a -t x1 -N 16 <file>on Linux.Beware fetching files from windows machines when creating templates because certain tools, such as Powershell ISE, and regedit’s export facility add a Byte Order Mark as the first character of the file, which can cause tracebacks.
You can use the ansible.windows.win_copy module with the
content:option if you prefer the template inline, as part of the playbook.For Linux you can use ansible.builtin.template which uses ‘\\n’ as
newline_sequenceby default.
See Also
See also
- ansible.windows.win_copy
Copies files to remote locations on windows hosts.
- ansible.windows.win_shell
Execute shell commands on target hosts.
- ansible.builtin.copy
The official documentation on the ansible.builtin.copy module.
- ansible.builtin.template
The official documentation on the ansible.builtin.template module.
Examples
- name: Create a file from a Jinja2 template
ansible.windows.win_template:
src: /mytemplates/file.conf.j2
dest: C:\Temp\file.conf
- name: Create a Unix-style file from a Jinja2 template
ansible.windows.win_template:
src: unix/config.conf.j2
dest: C:\share\unix\config.conf
newline_sequence: '\n'
backup: true
- name: Render template with trimmed newlines
ansible.windows.win_template:
src: unix/config.conf.j2
dest: C:\share\unix\config.conf
trim_blocks: true
- name: Render an nginx config, only writing it out once nginx has validated it
ansible.windows.win_template:
src: nginx.conf.j2
dest: C:\nginx\conf\nginx.conf
validate: C:\nginx\nginx.exe -t -c %s
- name: Render a JSON file, only writing it out if it parses
ansible.windows.win_template:
src: app.json.j2
dest: C:\Program Files\App\app.json
validate: Get-Content -LiteralPath %s -Raw | ConvertFrom-Json | Out-Null
- name: Render a PowerShell script, only writing it out if it has no syntax errors
ansible.windows.win_template:
src: bootstrap.ps1.j2
dest: C:\Temp\bootstrap.ps1
validate: $null = [ScriptBlock]::Create((Get-Content -LiteralPath %s -Raw))
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
|---|---|
Name of the backup file that was created. Returned: if backup=true Sample: |