一,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

  1. upstream {{ blog_site }} {
  2. {% for host in groups['webservers'] %}
  3. server {{ host }};
  4. {% endfor %}
  5. server {
  6. listen 80;
  7. server_name localhost;
  8. location / {
  9. proxy_pass http://{{ blog_site }};
  10. include proxy_params;
  11. }
  12. }

playbook —- 将nginx.conf文件批量拷到对端
[root@manager ansible_roles]# cat nginx_proxy.yml

  1. - hosts: webservers
  2. vars:
  3. - blog_site: blog.oldxu.com
  4. tasks:
  5. - name: Configure Nginx Proxy Vurtual File
  6. template:
  7. src: ./proxy_blog.oldxu.com.conf.j2
  8. dest: /tmp/proxy_blog.oldxu.com.conf

2. 使用判断语法推送不同文件

常规写法:首先需要写好两个不同配置的文件,再判断不同主机推送不同配置(太笨重)
[root@manager ansible_roles]# cat keepalived.yml #keepalive的安装,配置,启动

  1. - hosts: lbservers
  2. tasks:
  3. - name: Install Keepalived Server
  4. yum:
  5. name: keepalived
  6. state: present
  7. - name: Configure Keepalived Server
  8. copy:
  9. src: ./keepalived-master.conf.j2
  10. dest: /etc/keepalived/keepalived.conf
  11. when: ( ansible_hostname == "lb01" )
  12. notify: Restart Keepalived Server
  13. - name: Configure Keepalived Server
  14. copy:
  15. src: ./keepalived-backup.conf.j2
  16. dest: /etc/keepalived/keepalived.conf
  17. when: ( ansible_hostname == "lb02" )
  18. notify: Restart Keepalived Server
  19. - name: Started Keepalived Server
  20. systemd:
  21. name: keepalived
  22. state: started
  23. enabled: yes
  24. handlers:
  25. - name: Restart Keepalived Server
  26. systemd:
  27. name: keepalived
  28. state: restarted

3. 使用inventory 方式来生成不同配置文件

主机清单      #预先定义好变量

  1. hosts:
  2. [lbservers]
  3. 172.16.1.5 state=MASTER priority=150
  4. 172.16.1.6 state=BACKUP priority=120

[root@manager ansible_roles]# cat keepalived-template.conf.j2 #keepalvie配置文件

  1. global_defs {
  2. router_id {{ ansible_hostname }}
  3. }
  4. vrrp_instance VI_1 {
  5. state {{ state }}
  6. priority {{ priority }}
  7. interface eth0
  8. virtual_router_id 50
  9. advert_int 1
  10. authentication {
  11. auth_type PASS
  12. auth_pass 1111
  13. }
  14. virtual_ipaddress {
  15. 10.0.0.3
  16. }
  17. }

[root@manager ansible_roles]# cat keepalived.yml  #keepalive的安装,配置,启动

  1. - hosts: lbservers
  2. tasks:
  3. - name: Install Keepalived Server
  4. yum:
  5. name: keepalived
  6. state: present
  7. - name: Configure Keepalived Server
  8. template:
  9. src: keepalived-template.conf.j2
  10. dest: /etc/keepalived/keepalived.conf
  11. notify: Restart Keepalived Server
  12. - name: Started Keepalived Server
  13. systemd:
  14. name: keepalived
  15. state: started
  16. enabled: yes
  17. handlers:
  18. - name: Restart Keepalived Server
  19. systemd:
  20. name: keepalived
  21. state: restarted

*4. 使用 jinja方式来生成不同文件(if,变量)

主机清单
[root@manager ansible_roles]# cat hosts

  1. [lbservers]
  2. 172.16.1.5
  3. 172.16.1.6

[root@manager ansible_roles]# cat keepalived-template.conf.j2 #keepalive配置的 jinja2文件

  1. global_defs {
  2. router_id {{ ansible_hostname }}
  3. }
  4. vrrp_instance VI_1 {
  5. {% if ansible_hostname == "lb01" %}
  6. state MASTER
  7. priority 200
  8. {% elif ansible_hostname == "lb02" %}
  9. state BACKUP
  10. priority 100
  11. {% endif %}
  12. interface eth0
  13. virtual_router_id 50
  14. advert_int 1
  15. authentication {
  16. auth_type PASS
  17. auth_pass 1111
  18. }
  19. virtual_ipaddress {
  20. 10.0.0.3
  21. }
  22. }

[root@manager ansible_roles]# cat keepalived.yml #keepalive的安装,配置,启动

  1. - hosts: lbservers
  2. tasks:
  3. - name: Install Keepalived Server
  4. yum:
  5. name: keepalived
  6. state: present
  7. - name: Configure Keepalived Server
  8. template:
  9. src: keepalived-template.conf.j2
  10. dest: /etc/keepalived/keepalived.conf
  11. notify: Restart Keepalived Server
  12. - name: Started Keepalived Server
  13. systemd:
  14. name: keepalived
  15. state: started
  16. enabled: yes
  17. handlers:
  18. - name: Restart Keepalived Server
  19. systemd:
  20. name: keepalived
  21. state: restarted