Ansible 配置文件变量Jinja2模板
jinja2介绍
jinja2是Python的全功能模板引擎
Jinja2与Ansible啥关系
Ansible通常会使用jinja2模板来修改被管理主机的配置文件等...在saltstack中同样会使用到jinja2 如果在100台主机上安装nginx,每台nginx的端口都不一样,如何解决?
总结:ansible想要识别配置文件中的变量、判断、循环,只能使用jinjia2模板
Ansible如何使用Jinja2
将原本推送配置文件的copy
模块换成template
模块
template
模块就是一个能识别jinja2语法的模块
Ansible使用Jinja2注意事项
ansible不识别分号,php文件中包含分号的行需要删除
Ansible允许jinja2模板中使用条件判断和循环,但是不允许在playbook中使用。
注意:不是每个管理员都需要这个特性,但是有些时候jinja2模块能大大提高效率。
ansible playbook:
判断:when
循环:with_items
jinja2模板:
判断:if
循环:for while
Jinja模板基本语法
调用和注释
# 注释
{# 注释内容 #}
# 调用变量
{{ 调用变量 }}
其他语言判断
# shell:判断
if [ ansible_hostname == 'web01' ];then
echo 1
echo 2
fi
echo 3
# python:判断
if ansible_hostname == 'web01':
print(1)
print(2)
print(3)
判断
一次循环
{% if ansible_hostname == 'web01' %}
echo 1
{% endif %}
二次循环
{% if ansible_hostname == 'web01' %}
echo 1
{% else %}
echo 2
{% endif %}
三次循环
{% if ansible_hostname == 'web01' %}
echo 1
{% else if ansible_hostname == 'web02' %}
echo 2
{% else %}
echo 3
{% endif %}
shell循环
shell: 循环
for xxx in xxx;do
循环体
done
# 循环显示5 6 7 8 ip号
for ip in 5 6 7 8;do
echo 172.16.1.$ip
done
# 输出1循环10次
for num in `seq 10`;do
echo 1
done
循环
{# 循环 #}
{% for n(变量) in range(1,11) %}
循环体(命令)
{% endfor %}
jinja2实战keepalived
{# if 判断 #}
global_defs {
router_id {{ ansible_hostname }}
}
vrrp_instance VI_1 {
{% if ansible_hostname == 'web01' %}
state MASTER
priority 150
{% else %}
state BACKUP
priority 100
{% endif %}
nopreempt
interface eth0
virtual_router_id 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}
- hosts: all
tasks:
- name: 推送keepalived master配置文件
template:
src: /root/ansible/keepalived/keepalived.conf
dest: /root/keepalived.conf
直接调用变量(使用变量进行计算,然后使用int转成整型)
{# 数据库配置文件 #}
vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip_name_resolve
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
innodb_buffer_pool_size={{ (ansible_memtotal_mb * 0.8)|int }}
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
直接调用变量(字符串切片)
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip_name_resolve
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
server_id={{ ansible_hostname[-1] }}
innodb_buffer_pool_size={{ (ansible_memtotal_mb * 0.8)|int }}
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
Jinja2实战循环负载均衡配置文件
upstream {{ lb_name }} {
{% for num in range(7,10) %}
server 10.0.0.{{ num }}
{% endfor %}
}
server{
listen 80;
server_name {{ wordpress_domain }};
location / {
proxy_pass http://{{ lb_name }};
include /etc/nginx/proxy_param;
}
}
- hosts: all
tasks:
- name: 推送负载均衡配置文件
template:
src: /root/ansible/keepalived/lb.conf
dest: /root/lb.conf
lb_name: web
wordpress_domain: blog.xxx.com
upstream {{ lb_name }} {
{% for num in range(7,10) %}
server 10.0.0.{{ num }}
{% endfor %}
}
server{
listen 80;
server_name {{ zh_domain }};
location / {
proxy_pass http://{{ lb_name }};
include /etc/nginx/proxy_param;
}
}