ansible----template

template简介

template功能: 根据模板文件动态生成对应的配置文件
template文件必须存放于templates目录下,且命名为 .j2 结尾
ansible的template模板使用的是Jinja2语言
yaml/yml 文件需和templates目录平级,目录结构如下:
./
├── temnginx.yml
└── templates
       └── nginx.conf.j2

template模板应用

[root@centos7 ansible]# cp /etc/nginx/nginx.conf templates/nginx.conf.j2              #准备nginx配置文件复制到templates目录下做模板文件,文件后缀必须是j2
[root@centos7 ansible]# vim templates/nginx.conf.j2          #对nginx的模板文件稍作修改
user {{user}};                                               #jinja2语言支持变量和数字运算等
worker_processes {{ansible_processor_vcpus*2}};              #ansible_processor_vcpus变量为ansible的setup模块自带的系统变量,后面跟*2做乘法运算
[root@centos7 ansible]# vim template_nginx.yml               #编译安装nginx的playbook
---
#install nginx
- hosts: appsrvs
  remote_user: root

  tasks:
    - name: install nginx package
      yum: name=nginx
    - name: config file
      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf       #把模板文件复制到远程主机;系统会自动到templates目录下找到nginx.conf.j2文件
    - name: start service
      service: name=nginx state=started enabled=yes
[root@centos7 ansible]# ansible-playbook -e user="daemon" template_nginx.yml     #执行安装nginx的playbook,执行playbook,到复制nginx配置文件时,会调用模板文件进行复制,并且命令行指定的user变量值赋值给模板文件中的user变量

template中的for应用

测试:准备一个playbook,在playbook中定义变量;准备一个template文件

[root@centos7 ansible]# vim for1.yml    #编辑一个playbook
---
#test for
- hosts: appsrvs
  remote_user: root
  vars:               #自定义变量
    ports:            #指定列表,并且给列表中设置三个元素
      - 81
      - 82
      - 83

  tasks:
    - name: test for
      template: src=app.conf.j2 dest=/data/app.conf            #引用模板

[root@centos7 ansible]# vim templates/app.conf.j2            #编辑模板
{% for p in ports %}           #读取playbook中ports列表中的元素,赋值给变量p;列表中有几个元素,执行几次循环
server {
    listen {{p}}
}
{%endfor%}

执行结果如下:
[root@wwwserver817 ~]$ cat /data/app.conf 
server {
	listen 81
}
server {
	listen 82
}
server {
	listen 83
}
for循环中的复杂应用----字典:
[root@centos7 ansible]# vim for2.yml
---
#test for
- hosts: appsrvs
  remote_user: root
  vars:                  #自定义变量
    ports:               #指定列表,每个元素由一个字典组成,每个字典中有两个键值对儿
      - {listen: 81, web: www.xx1.com }
      - {listen: 82, web: www.xx2.com }
      - {listen: 83, web: www.xx3.com }

  tasks:
    - name: test for
      template: src=app.conf.j2 dest=/data/app.conf

[root@centos7 ansible]# vim templates/app.conf.j2
{% for p in ports %}            #把列表中的元素赋值给变量p
server {
    listen {{p.listen}}         #把元素中的listen值赋值给变量p
    name {{p.web}}              #把元素中的web值赋值给变量p
}
{%endfor%}

执行结果如下:
server {
	listen 81
	name www.xx1.com
}
server {
	listen 82
	name www.xx2.com
}
server {
	listen 83
	name www.xx3.com
}
for循环的复杂应用----配合if:
[root@centos7 ansible]# vim for3.yml 
---
#test for
- hosts: appsrvs
  remote_user: root
  vars:
    ports:              #指定列表,列表中设置三个元素,用字典进行表示;元素二的字典中有两个键值对儿,listen和web;其余元素的字典中只有一个键值对儿listen
      - {listen: 81 }
      - {listen: 82, web: www.xx2.com }
      - {listen: 83 }

  tasks:
    - name: test for
      template: src=app.conf.j2 dest=/data/app.conf

[root@centos7 ansible]# vim templates/app.conf.j2
{% for p in ports %}
server {
    listen {{p.listen}}
{% if p.web is defined %}        #判断列表元素中如果定义了web的值,就进行打印p.web;反之,就不进行打印
    name {{p.web}}
{%endif%}
}
{%endfor%}

结果如下:
server {
	listen 81
}
server {
	listen 82
	name www.xx2.com
}
server {
	listen 83
}
原文地址:https://www.cnblogs.com/dongzhanyi123/p/11914505.html