简介
template模块使用了Jinjia2模版语言,进行文档内变量的替换的模块。
template模块用法和copy模块用法基本一致,它主要用于复制配置文件。可以按需求修改配置文件内容来复制模板到被控主机上。
模版中可以使用如下6个变量:
ansible_managed- 包含一个字符串,可用于描述模板名称,主机,模板文件的修改时间和所有者的uidtemplate_host- 包含模板机器的节点名称template_uid- 所有者的uidtemplate_path- 模版路径template_fullpath- 模版的绝对路径template_run_date- 模版呈现的时间
模块参数
| 名称 | 必选 | 默认值 | 可选值 | 备注 |
|---|---|---|---|---|
| backup | no | no | yes/no |
在覆盖之前将原文件备份,备份文件包含时间戳信息 |
| follow | no | no | yes/no |
是否遵循目的机器中的文件系统链接 |
| force | no | yes | yes/no |
是否强制执行 |
| group | no | 设置文件/目录的所属组 | ||
| mode | no | 设置文件权限,模式实际上是八进制数字(如0644),少了前面的零可能会有意想不到的结果。从版本1.8开始,可以将模式指定为符号模式(例如u+rwx或u=rw,g=r,o=r) |
||
| newline_sequence(2.4+) | no | \n |
\n,\r,\r\n |
指定要用于模板文件的换行符 |
| owner | no | 设置文件/目录的所属用户 | ||
| src | no | Jinja2格式化模板的文件位置 | ||
| trim_blocks | no | no | yes/no |
设置为True,则块之后的第一个换行符被移除 |
| unsafe_writes | no | yes/no |
是否以不安全的方式进行,可能导致数据损坏 | |
| validate | no | None | 复制前是否检验需要复制目的地的路径 |
示例
template模块的使用方法和copy模块基本相同,相同之处这里就不再赘述,出门左转可以看之前copy模块的讲解,这里举个例子讲一下template的使用方法。
建立模版
vim hello_world.txt.j2:
1 | Hello "{{ dynamic_word }}" |
- 由于Ansible是使用
Jinja2来编写template模版的,所以需要使用*.j2为文件后缀 - 上面的
""代表我们在该template里使用了名为dynamic_word的变量
编写playbook,加入变量
vim template_demo.yml:
1 | --- |
- 在第5行,为
dynamic_word变量设了一个预设值 - 在task 里,我们使用
template module,并指定了模版的来源src和目的地dest
执行playbook
1 | [root@centos7 ~]# ansible-playbook template_demo.yml |
获取执行结果
1 | [root@centos7 ~]# ansible test -m command -a "cat /tmp/hello_world.txt" |

可以发现,之前设置的变量dynamic_word已经被替换。
如果配置了重复的变量
比如,这里配置了两个dynamic_word,那么ansible会如何处理呢?1
2
3
4
5
6
7
8
9
10
11
12
13
14
15---
- name: Play the template module
hosts: test
vars:
dynamic_word: "World , liuhao ^_^"
dynamic_word: "Problem?"
tasks:
- name: generation the hello_world.txt file
template:
src: hello_world.txt.j2
dest: /tmp/hello_world.txt
mode: 0644
owner: liuhao
group: liuhao
再次运行
发现这里会给出一个警告,但是程序会继续运行,当存在重复的字典值时,会使用最后配置的那个变量。从执行结果也可以看出。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19[root@centos7 ~]# ansible-playbook template_demo.yml
[WARNING]:While constructing a mapping from /root/template_demo.yml, line 5, column 5, found a duplicate dict key (dynamic_word). Using last
defined value only.
PLAY [Play the template module] *********************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************
ok: [172.20.21.121]
TASK [generation the hello_world.txt file] **********************************************************************************************************
changed: [172.20.21.121]
PLAY RECAP ******************************************************************************************************************************************
172.20.21.121 : ok=2 changed=1 unreachable=0 failed=0
[root@centos7 ~]# ansible test -m command -a "cat /tmp/hello_world.txt"
172.20.21.121 | SUCCESS | rc=0 >>
Hello "Problem?"
目标文件的父目录不存在
与copy模块类似,若目标文件的父目录不存在时,也会报错,如下示例:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16[root@centos7 ~]# ansible-playbook template_demo.yml
[WARNING]: While constructing a mapping from /root/template_demo.yml, line 5, column 5, found a duplicate dict key (dynamic_word). Using
last defined value only.
PLAY [Play the template module] **************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************
ok: [172.20.21.121]
TASK [generation the hello_world.txt file] ***************************************************************************************************
fatal: [172.20.21.121]: FAILED! => {"changed": false, "checksum": "c871758134bd7d3cd7d718ee65ed459b6ca2f0f1", "msg": "Destination directory /tmp/hello does not exist"}
to retry, use: --limit @/root/template_demo.retry
PLAY RECAP ***********************************************************************************************************************************
172.20.21.121 : ok=1 changed=0 unreachable=0 failed=1
这里只是先简单讲述template模块的使用方法,后续讲解playbook编写时还会再接触。