win_template – Template a file out to a remote server¶
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 the- defaultssection of- ansible.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.
Parameters¶
Notes¶
Note
- 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 win_copy module with the content:option if you prefer the template inline, as part of the playbook.
- For Linux you can use template which uses ‘\n’ as newline_sequenceby 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: 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.
See Also¶
See also
- win_copy – Copies files to remote locations on windows hosts
- The official documentation on the win_copy module.
- copy – Copy files to remote locations
- The official documentation on the copy module.
- template – Template a file out to a remote server
- The official documentation on the template module.
Examples¶
- name: Create a file from a Jinja2 template
  win_template:
    src: /mytemplates/file.conf.j2
    dest: C:\Temp\file.conf
- name: Create a Unix-style file from a Jinja2 template
  win_template:
    src: unix/config.conf.j2
    dest: C:\share\unix\config.conf
    newline_sequence: '\n'
    backup: yes
Return Values¶
Common return values are documented here, the following are the fields unique to this module:
| Key | Returned | Description | 
|---|---|---|
| backup_file 
                  string
                                       | if backup=yes | Name of the backup file that was created. Sample: C:\Path\To\File.txt.11540.20150212-220915.bak | 
Status¶
- This module is guaranteed to have backward compatible interface changes going forward. [stableinterface]
- This module is maintained by the Ansible Core Team. [core]
Red Hat Support¶
More information about Red Hat’s support of this module is available from this Red Hat Knowledge Base article.
Authors¶
- Jon Hawkesworth (@jhawkesworth)
Hint
If you notice any issues in this documentation, you can edit this document to improve it.
