Ansible jinja2模板
一、Ansible jinja2模板概述
#什么是jinja2模板 jinja2是Python的全功能模板引擎 #Jinja2与Ansible啥关系 Ansible通常会使用jinja2模板来修改被管理主机的配置文件等...在saltstack中同样会使用jinja2 如果在100台主机上安装服务,每台服务的监听端口都不一样,如何解决? #Ansible如何使用Jinja2 使用Ansible的jinja2模板也就是使用template模块,该模块和copy模块一样,都是将文件复制到远端主机上去,但是区别在于,
template模块可以获取到文件中的变量,而copy则是原封不动的把文件内容复制进去 #Ansible使用Jinja2注意事项 Ansible允许jinja2模板中使用条件判断和循环,但是不允许在playbook中使用。
二、Ansible jinja2 模板使用
1.变量使用语法
{{ EXPR }}输出变量值,会输出自定义的变量值或facts 1)playbook文件使用template模块 2)模板文件里面变量使用{{名称}},比如{{PORT}}或使用facts内置变量
2.判断循环语法
#循环表达式 {% for i in EXPR %} {% endfor %} #条件判断 {% if EXPR %} {% elif EXPR %} {% else %} {% endif %} #注释 {# COMMENT #}
#shell中循环 #!/bin/bash for i in `seq 10`;do echo $i done #shell中判断 #!/bin/bash #注释 age=$1 if [ $age -lt 18 ];then echo '小姐姐' else echo '大妈' fi
3.jinja2测试
1)登录文件测试
#1.配置登录文件j2模板 [root@m01 web]# vim motd.j2 Welcome to {{ ansible_fqdn }} This system total mem is : {{ ansible_memtotal_mb }} MB This system free mem is : {{ ansible_memfree_mb }} MB #2.推送j2模板文件 [root@m01 web]# vim motd.yml - hosts: all tasks: - name: config motd template: src: ./motd.j2 dest: /etc/motd
2)数据库配置文件推送
#配置数据库配置文件添加 [root@m01 web]# vim /etc/my.cnf [mysqld] ... {% if ansible_memtotal_mb == 972 %} #只要系统相同,1G内存就是972 innodb_log_buffer_pool_size = 800M {% elif ansible_memtotal_mb == 1996 %} #只要系统相同,2G内存就是1996 innodb_log_buffer_pool_size=1600M {% endif %} ... #配置推送语法 - hosts: web_group tasks: - name: config mysql conf template: src: /root/my.cnf dest: /tmp/my.cnf #查看远端配置 1.如果服务器内存是972,则配置为 innodb_log_buffer_pool_size = 800M 2.如果服务器内存为1996,则配置为 innodb_log_buffer_pool_size=1600M
4.使用jinja2模板配置nginx负载均衡
1)安装两台wordpress
[root@m01 web]# ansible-playbook main.yml [root@m01 web]# cat main.yml - import_playbook: ./lnp.yml - import_playbook: ./mariadb.yml
2)配置负载均衡配置文件
#正经配置 [root@m01 web]# vim upstream.j2 upstream web { server 172.16.1.7:80; servere 172.16.1.8:80; } server { listen 80; server_name blog.linux.com; location / { proxy_pass http://web; proxy_set_header Host $http_host; } } #不正经配置 [root@m01 web]# vim upstream.j2 upstream {{ server_name }} { {% for i in range(7,17) %} server {{ nei_ip }}.{{ i }}:{{ upstream_port }}; {% endfor %} } server { listen {{ nginx_port }}; server_name {{ server_name }}; location / { proxy_pass http://{{ server_name }}; proxy_set_header Host $http_host; } }
3)配置变量
[root@m01 web]# vim upstream_vars.yml server_name: blog.linux.com nei_ip: 172.16.1 upstream_port: 80 nginx_port: 80
4)配置playbook
#1.配置主机清单和hosts文件 #2.编写playbook [root@m01 web]# vim lsb.yml - hosts: lb_group vars_files: ./upstream_vars.yml tasks: - name: Copy Nginx Rpm copy: src: /root/nginx-1.16.1-1.el7.ngx.x86_64.rpm dest: /tmp/ - name: Install Nginx Server yum: name: /root/nginx-1.16.1-1.el7.ngx.x86_64.rpm state: present - name: Config Nginx Upstream Conf copy: src: ./upstream.j2 dest: /etc/nginx/conf.d/upstream.conf - name: Start Nginx Server systemd: name: nginx state: started
5.使用jinja2模板配置nginx负载均衡keepalived
1)配置keepalived配置文件
#正经配置文件 global_defs { router_id lb01 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 50 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3 } } #不正经配置文件 [root@m01 web]# vim keepalived.j2 global_defs { router_id {{ ansible_fqdn }} } vrrp_instance VI_1 { {% if ansible_fqdn == "lb01" %} state MASTER priority 150 {% else %} 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 } }
2)配置推送
#编写playbook [root@m01 web]# vim keepalived.yml - hosts: lb_group tasks: - name: Install Keepalived Server yum: name: keepalived state: presetn - name: Config keepalived Conf template: src: ./keepalived.j2 dest: /etc/keepalived/keepalived.conf - name: Start Keepalived Server systemd: name: keepalived state: started
标签:web,name,30,keepalived,jinja2,root,模板 From: https://www.cnblogs.com/ludingchao/p/18136704