Ansible (11) template Module
Template Module
The template module is involved in the first playbook in the previous section. What is this?
Let's go over what we said on the official website (translated in the general section ):
The template uses the Jinjia2 format as the file template to replace the variables in the document. Every time it is used, it is marked as "changed" by ansible.
It's hard to understand, so let's take a look at the actual example.
Common Parameters
Parameter Name |
Required? |
Default Value |
Option |
Description |
Backup |
No |
No |
Yes/no |
Create a file backup, including timestamp, for emergency purposes. |
Dest |
Yes |
|
|
The absolute path on the remote node, used to place the template file. |
Group |
No |
|
|
Set the user group to which the template file belongs on the remote node. |
Mode |
No |
|
|
Set the template file permission on the remote node. Similar to LinuxChmodUsage |
Owner |
No |
|
|
Set the user to which the template file on the remote node belongs. |
Src |
Yes |
|
|
The location of the template file of the local Jinjia2 template. |
Case
# Set/mytemplates/foo. after entering the parameters, the j2 file is copied to the/etc/file on the remote node. conf, File Permission-related skipped-template: src =/mytemplates/foo. j2 dest =/etc/file. conf owner = bin group = wheel mode = 0644 # same effect as above, different file permission setting methods-template: src =/mytemplates/foo. j2 dest =/etc/file. conf owner = bin group = wheel mode = "u = rw, g = r, o = r"
Detailed descriptionWe can check it here.
The key part of the template file in roles/templates/server. xml is as follows:
<user username="{{ admin_username }}" password="{{ admin_password }}" roles="manager-gui" />
When this file has not been executed by the templateAdmin_usernameAndAdmin_passwordAll are variable statuses.
When the playbook executes the template, the remote admin_username * andAdmin_passwordThe value corresponding to the variable.
For example:
In the previous Playbook, if we set these two variables in tomcat-servers:
dmin_username: adminadmin_password: adminsecret
Before executing this Playbook, the corresponding template file (commonly known as the template) will be in the local state of {admin_username }}and {admin_password. When Ansible calls the template for execution, Jinjia2 reads the corresponding value from "tomcat-servers" and replaces the variables in the template, copy the file with the variable value replaced to the remote node.
This is the meaning of the template.
Do you understand the convenience of Ansible?