一,jinja2
1.概述
Jinja2 是一个现代的,设计者友好的,模板语言。 它速度快,被广泛使用,并且提供了可选的沙箱模板执行环境保证安全。
当利用jinja2进行渲染的时候,它会把这些特殊的占位符进行填充/替换,jinja2支持python中所有的Python数据类型比如列表、字段、对象等。
2.Ansible 如何使用 jinja2 模板
Ansible 使用 jinja2 模板,也就是 template 模板。该模块和 copy 模块一样,都是将文件复制到目标机器,但是区别在于 template 模块可以获取要复制文件中的变量的值,而 copy 则是原封不动的把文件内容复制过去。
比如:针对不同的主机定义不同的变量,template 会在将文件分发前读取变量到 jinja2 模板,之后再然后分发到不同的被管理主机上。
3.jinja2渲染NginxProxy配置文件用到的语法三种语法
控制结构 {% %} 变量取值 {{ }} 注释 {# #}。
二,演示
1 变量中的循环语句 | 案例:nginx proxy |
---|---|
2 变量中的判断语法 | 案例:keeplive不同主机的配置 |
3 Inventory方式实现 | 案例:keeplive不同主机的配置 |
**4. jinja变量方式实现 | 案例:keeplive不同主机的配置 |
1. 变量中的循环语句
给10台主机装上Nginx服务,但是要求每台主机的端口都不一样,如何解决?
( jinja配置文件模板,根据不同主机生成不同文件配置nginx proxy)
主机清单:
[root@manager ansible_roles]# cat proxy_blog.oldxu.com.conf.j2
upstream {{ blog_site }} {
{% for host in groups['webservers'] %}
server {{ host }};
{% endfor %}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://{{ blog_site }};
include proxy_params;
}
}
playbook —- 将nginx.conf文件批量拷到对端
[root@manager ansible_roles]# cat nginx_proxy.yml
- hosts: webservers
vars:
- blog_site: blog.oldxu.com
tasks:
- name: Configure Nginx Proxy Vurtual File
template:
src: ./proxy_blog.oldxu.com.conf.j2
dest: /tmp/proxy_blog.oldxu.com.conf
2. 使用判断语法推送不同文件
常规写法:首先需要写好两个不同配置的文件,再判断不同主机推送不同配置(太笨重)
[root@manager ansible_roles]# cat keepalived.yml
#keepalive的安装,配置,启动
- hosts: lbservers
tasks:
- name: Install Keepalived Server
yum:
name: keepalived
state: present
- name: Configure Keepalived Server
copy:
src: ./keepalived-master.conf.j2
dest: /etc/keepalived/keepalived.conf
when: ( ansible_hostname == "lb01" )
notify: Restart Keepalived Server
- name: Configure Keepalived Server
copy:
src: ./keepalived-backup.conf.j2
dest: /etc/keepalived/keepalived.conf
when: ( ansible_hostname == "lb02" )
notify: Restart Keepalived Server
- name: Started Keepalived Server
systemd:
name: keepalived
state: started
enabled: yes
handlers:
- name: Restart Keepalived Server
systemd:
name: keepalived
state: restarted
3. 使用inventory 方式来生成不同配置文件
主机清单 #预先定义好变量
hosts:
[lbservers]
172.16.1.5 state=MASTER priority=150
172.16.1.6 state=BACKUP priority=120
[root@manager ansible_roles]# cat keepalived-template.conf.j2
#keepalvie配置文件
global_defs {
router_id {{ ansible_hostname }}
}
vrrp_instance VI_1 {
state {{ state }}
priority {{ priority }}
interface eth0
virtual_router_id 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}
[root@manager ansible_roles]# cat keepalived.yml
#keepalive的安装,配置,启动
- hosts: lbservers
tasks:
- name: Install Keepalived Server
yum:
name: keepalived
state: present
- name: Configure Keepalived Server
template:
src: keepalived-template.conf.j2
dest: /etc/keepalived/keepalived.conf
notify: Restart Keepalived Server
- name: Started Keepalived Server
systemd:
name: keepalived
state: started
enabled: yes
handlers:
- name: Restart Keepalived Server
systemd:
name: keepalived
state: restarted
*4. 使用 jinja方式来生成不同文件(if,变量)
主机清单
[root@manager ansible_roles]# cat hosts
[lbservers]
172.16.1.5
172.16.1.6
[root@manager ansible_roles]# cat keepalived-template.conf.j2
#keepalive配置的 jinja2文件
global_defs {
router_id {{ ansible_hostname }}
}
vrrp_instance VI_1 {
{% if ansible_hostname == "lb01" %}
state MASTER
priority 200
{% elif ansible_hostname == "lb02" %}
state BACKUP
priority 100
{% endif %}
interface eth0
virtual_router_id 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}
[root@manager ansible_roles]# cat keepalived.yml
#keepalive的安装,配置,启动
- hosts: lbservers
tasks:
- name: Install Keepalived Server
yum:
name: keepalived
state: present
- name: Configure Keepalived Server
template:
src: keepalived-template.conf.j2
dest: /etc/keepalived/keepalived.conf
notify: Restart Keepalived Server
- name: Started Keepalived Server
systemd:
name: keepalived
state: started
enabled: yes
handlers:
- name: Restart Keepalived Server
systemd:
name: keepalived
state: restarted