ansible中的template模板

657 阅读2分钟

这是我参与 8 月更文挑战的第 19 天,活动详情查看: 8月更文挑战

template介绍

Jinja是基于Python的模板引擎。template类是Jinja的另一个重要组件,可以看作一个编译过的模块文件,用来生产目标文本,传递Python的变量给模板去替换模板中的标记。

template范例:

1. # scp root@192.168.175.130:/etc/httpd/conf/httpd.conf ./templates    //复制被管理端的配置文件到本地# vim templates/httpd.conf    //在管理端将配置文件要修改的地方定义变量
  1. 修改htto.conf配置文件里的内容,将参数设置成变量。
Listen {{http_port}}
ServerName {{server_name}}
MaxClients {{access_num}}

3.在/etc/ansible/hosts 添加变量 vim /etc/ansible/hosts

[abc]
192.168.200.129 http_port=192.168.200.129:80 access_num=100 server_name="www.yun.com:80"# vim apache.yml# ansible-playbook apache.yml   

4.然后执行脚本 然后去abc组的主机上查看下配置文件是否已经改了

ansible-playbook yaml文件如下:

cat httpd_template.yaml 
---
- hosts: test
  remote_user: root
  vars: 
​    - package: httpd
​    - service: httpd
  tasks:
​    - name: install httpd package
​      yum: name={{ package }} state=latest
​    - name: install configure file
​      template: src=./template/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
​      notify:
​        - restart httpd
​    - name: start httpd server
​      service: name={{ service }} enabled=true state=started
  handlers:
​    - name: restart httpd
​      service: name={{ service }} state=restartd

template for循环

template中也可以使用流程控制for 循环和if 条件判断,实现动态生成文件功能。

范例一:

cat temnginx.yaml 
---
- hosts: test
  remote_user: root
  vars: 
​    nginx_vhosts:
​      - listen: 80
  tasks:
​    - name: config file 
​      template: src=./template/nginx.conf.j2 dest=/data/nginx.conf
 cat template/nginx.conf.j2 
{% for vhost in nginx_vhosts %}
server {
  listen {{ vhost.listen }}
}
{% endfor %}
生成的结果
cat nginx.conf 
server {
  listen 80
}

范例二:

cat temnginx2.yaml 
---
- hosts: test
  remote_user: root
  vars:
​    nginx_vhosts:
​      - 81
​      - 82 
​      - 83
  tasks:
​    - name: template config
​      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf

cat nginx.conf.j2 
{% for vhost in nginx_vhosts %}
server {
  listen {{ vhost.listen }}
}
{% endfor %}
生成的结果
server {
  listen 81
}
server {
  listen 82
}
server {
  listen 83
}

范例三:

cat temnginx3.yaml 
---
- hosts: test
  remote_user: root
  vars:
​    nginx_vhosts:  
​      - listen: 8080
​        server_name: "web1.baidu.com"
​        root: /var/www/nginx/web1/
​      - listen: 8081
​        server_name: "web1.baidu.com"
​        root: "/var/www/nginx/web2/"
  tasks:
​    - name: template config
​      template: src=./template/nginx.conf2.j2 dest=/data/nginx2.conf

cat nginx.conf2.j2
{% for vhost in nginx_vhosts %}
server{
  listen {{ vhost.listen }}
  server_name {{ vhost.server_name }}
  root {{ vhost.root }}
}
{% endfor %}

在模板文件中还可以使用if条件判断,决定是否生成相关的配置信息 案例一:

cat templnginx4.yaml 
- hosts: test
  remote_user: root
  vars:
​    nginx_vhosts:
​      - web1:
​        listen: 8080
​        root: "/var/www/nginx/web1/"
​      - web2: 
​        listen: 8080
​        server_name: "web2.baidu.com"
​        root: "/var/www/nginx/web2/"
​      - web3: 
​        listen: 8080
​        server_name: "web3.baidu.com"
​        root: "/var/www/nginx/web3/"
  tasks:
​    - name: template config to 
​      template: src=./template/nginx.conf3.j2 dest=/data/nginx3.conf

cat template/nginx.conf3.j2 
{% for vhost in nginx_vhosts %}
server {
  listen {{ vhost.listen }}
  {% if vhost.server_name is defined %}
server_name {{ vhost.server_name }}
  {% endif %}
root {{ vhost.root }}
}
{% endfor %}

输出结果:
cat nginx3.conf 
server {
  listen 8080
  root /var/www/nginx/web1/
}

server {
  listen 8080
  server_name web2.baidu.com
  root /var/www/nginx/web2/
}
server {
  listen 8080
  server_name web3.baidu.com
  root /var/www/nginx/web3/
}