+

Search Tips   |   Advanced Search

ansible.builtin.template - Template a file out to a target host

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

This module has a corresponding action plugin.


Parameters

Parameter Choices/Defaults Comments

attributes

string

The attributes the resulting file or directory should have.

To get supported flags look at the man page for chattr on the target system.

This string should contain the attributes in the same order as the one displayed by lsattr.

The = operator is assumed as default, otherwise + or - operators need to be included in the string.


aliases: attr

backup

boolean

  • no ←
  • yes

Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it incorrectly.

block_end_string

string

Default:

"%}"

The string marking the end of a block.

block_start_string

string

Default:

"{%"

The string marking the beginning of a block.

dest

path / required

Location to render the template to on the remote machine.

follow

boolean

  • no ←
  • yes

Determine whether symbolic links should be followed.

When set to yes symbolic links will be followed, if they exist.

When set to no symbolic links will not be followed.

Previous to Ansible 2.4, this was hardcoded as yes.

force

boolean

  • no

  • yes ←

Determine when the file is being transferred if the destination already exists.

When set to yes, replace the remote file when contents are different than the source.

When set to no, the file will only be transferred if the destination does not exist.

group

string

Name of the group that should own the file/directory, as would be fed to chown.

lstrip_blocks

boolean

  • no ←
  • yes

Determine when leading spaces and tabs should be stripped.

When set to yes leading spaces and tabs are stripped from the start of a line to a block.

This functionality requires Jinja 2.7 or newer.

mode

raw

The permissions the resulting file or directory should have.

For those used to /usr/bin/chmod remember that modes are actually octal numbers. You must either add a leading zero so that Ansible's YAML parser knows it is an octal number (like 0644 or 01777) or quote it (like '644' or '1777') so Ansible receives a string and can do its own conversion from string into number.

Giving Ansible a number without following one of these rules will end up with a decimal number which will have unexpected results.

As of Ansible 1.8, the mode may be specified as a symbolic mode (for example, u+rwx or u=rw,g=r,o=r).

newline_sequence

string

  • \n ←
  • \r
  • \r\n

Specify the newline sequence to use for templating files.

output_encoding

string

Default:

"utf-8"

Overrides the encoding used to write the template file defined by dest.

It defaults to utf-8, but any encoding supported by python can be used.

The source template file must always be encoded using utf-8, for homogeneity.

owner

string

Name of the user that should own the file/directory, as would be fed to chown.

selevel

string

The level part of the SELinux file context.

This is the MLS/MCS attribute, sometimes known as the range.

When set to _default, it will use the level portion of the policy if available.

serole

string

The role part of the SELinux file context.

When set to _default, it will use the role portion of the policy if available.

setype

string

The type part of the SELinux file context.

When set to _default, it will use the type portion of the policy if available.

seuser

string

The user part of the SELinux file context.

By default it uses the system policy, where applicable.

When set to _default, it will use the user portion of the policy if available.

src

path / required

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 utf-8 but output_encoding can be used to control the encoding of the output template.

trim_blocks

boolean

  • no

  • yes ←

Determine when newlines should be removed from blocks.

When set to yes the first newline after a block is removed (block, not variable tag!).

unsafe_writes

boolean

  • no ←
  • yes

Influence when to use atomic operation to prevent data corruption or inconsistent reads from the target file.

By default this module uses atomic operations to prevent data corruption or inconsistent reads from the target files, but sometimes systems are configured or just broken in ways that prevent this. One example is docker mounted files, which cannot be updated atomically from inside the container and can only be written in an unsafe manner.

This option allows Ansible to fall back to unsafe methods of updating files when atomic operations fail (however, it doesn't force Ansible to perform unsafe writes).

IMPORTANT! Unsafe writes are subject to race conditions and can lead to data corruption.

validate

string

The validation command to run before copying into place.

The path to the file to validate is passed in via '%s' which must be present as in the examples below.

The command is passed securely so shell features like expansion and pipes will not work.

variable_end_string

string

Default:

"}}"

The string marking the end of a print statement.

variable_start_string

string

Default:

"{{"

The string marking the beginning of a print statement.



Notes


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


Authors