哈喽大家好,我是koten,本篇文章给大家介绍jinja2模板,这是python的全功能模板引擎。在Ansible中,通常会使用jinja2模板来修改被管理主机的配置文件(saltstack中也会用到)
Ansible中使用的jinja2模板其实就是使用template模块,该模块与copy模块一样,都有复制管理机的配置文件到客户机的作用,区别在于template模块可以获取到文件中的变量,而copy则是原封不动把文件内容复制过去。如:我们想把脚本中的变量名改成主机名,如果使用copy推送的就是{
{ ansible_fqdn }},如果使用template,则会变成对应的主机名。
需要注意的是,Ansible允许jinja2模板中使用条件判断和循环,但是不允许在playbook中使用,只能在传输的文件中使用。
Ansible Jinja2模板使用
1、基本语法
1、playbook文件使用template模块
2、模板文件里面变量使用{{名称}},比如{{ port }}或使用facts
2、Jinja2模板逻辑判断
#循环表达式
{% for i in EXPR %}
{% endfor %}
#条件判断
{% if EXPR %}
{% elif EXPR %}
{% else %}
{% endif %}
#注释
{# COMMENT #}
3、Jinja2示例
1、编辑playbook
[root@Ansible test]# cat jinja2.yml
- hosts: web_group
tasks:
- name: copy template file
template:
src: motd.j2
dest: /etc/motd
2、准备motd.j2文件
[root@Ansible test]# cat motd.j2
Welcome to {{ ansible_fqdn }}
This system total mem is : {{ ansible_memtotal_mb }} MB
This system free mem is : {{ ansible_memfree_mb }} MB
3、执行playbook并查看效果
[root@Ansible test]# ansible-playbook jinja2.yml
PLAY [web_group] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [web01]
ok: [web02]
TASK [copy template file] ******************************************************
changed: [web01]
changed: [web02]
PLAY RECAP *********************************************************************
web01 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web02 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@Ansible test]# ansible web_group -a 'cat /etc/motd'
web01 | CHANGED | rc=0 >>
Welcome to Web01
This system total mem is : 972 MB
This system free mem is : 441 MB
web02 | CHANGED | rc=0 >>
Welcome to Web02
This system total mem is : 972 MB
This system free mem is : 84 MB
Ansible Jinja2管理nginx
Ansible使用jinja2的for循环表达式渲染出nginx负载均衡的配置文件
1、编辑playbook
[root@Ansible test]# cat lb.yml
- hosts: lb_group
vars:
http_port: 80
server_name: www.koten.vip
tasks:
- name: copy
template:
src: proxy_7.conf.j2
dest: /etc/nginx/conf.d/proxy_7.conf
notify: reload nginx
handlers:
- name: reload nginx
systemd:
name: nginx
state: reloaded
2、准备配置文件
[root@Ansible test]# cat proxy_7.conf.j2
upstream {{ server_name }} {
{% for n in range(21) %}
server 172.16.1.{{ n }}:{{ http_port }}
{% endfor %}
}
server{
listen 80;
server_name {{ server_name }};
location / {
root /code
index index.html
proxy_pass http://{{ server_name }}
proxy_set_header Host $http_host
}
}
3、执行playbook并查看客户机上的效果
[root@Ansible test]# ansible-playbook lb.yml
PLAY [lb_group] ****************************************************************
TASK [Gathering Facts] *********************************************************
ok: [lb00]
ok: [lb01]
ok: [lb02]
TASK [copy] ********************************************************************
changed: [lb01]
changed: [lb00]
changed: [lb02]
RUNNING HANDLER [reload nginx] *************************************************
changed: [lb00]
changed: [lb02]
changed: [lb01]
PLAY RECAP *********************************************************************
lb00 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
lb01 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
lb02 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
查看效果
[root@LB00 ~]# cat /etc/nginx/conf.d/proxy_7.conf
upstream www.koten.vip {
server 172.16.1.0:80
server 172.16.1.1:80
server 172.16.1.2:80
server 172.16.1.3:80
server 172.16.1.4:80
server 172.16.1.5:80
server 172.16.1.6:80
server 172.16.1.7:80
server 172.16.1.8:80
server 172.16.1.9:80
server 172.16.1.10:80
server 172.16.1.11:80
server 172.16.1.12:80
server 172.16.1.13:80
server 172.16.1.14:80
server 172.16.1.15:80
server 172.16.1.16:80
server 172.16.1.17:80
server 172.16.1.18:80
server 172.16.1.19:80
server 172.16.1.20:80
}
server{
listen 80;
server_name www.koten.vip;
location / {
root /code
index index.html
proxy_pass http://www.koten.vip
proxy_set_header Host $http_host
}
}
Ansible Jinja2管理keepalived
ansible使用jinja模板的if判断表达式渲染出keepalived高可用配置文件,并推送到lb主机上
1、编辑playbook
[root@Ansible test]# cat keepalived.yml
- hosts: lb_group
tasks:
- name: copy file
template:
src: keepalived.conf.j2
dest: /etc/keepalived/keepalived.conf
notify: restart keepalived
when: (ansible_hostname is match "LB01") or (ansible_hostname is match "LB02")
handlers:
- name: restart keepalived
systemd:
name: keepalived
state: restarted
when: (ansible_hostname is match "LB01") or (ansible_hostname is match "LB02")
2、编写keepalived配置文件
可以看着keepalive master配置文件和keepalive backup配置文件参考着编写
[root@Ansible test]# cat keepalived.conf.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
}
}
3、执行playbook并查看效果
[root@Ansible test]# ansible-playbook keepalived.yml
PLAY [lb_group] ****************************************************************************************
TASK [Gathering Facts] *********************************************************************************
ok: [lb02]
ok: [lb00]
ok: [lb01]
TASK [copy file] ***************************************************************************************
skipping: [lb00]
changed: [lb02]
changed: [lb01]
RUNNING HANDLER [restart keepalived] *******************************************************************
changed: [lb01]
changed: [lb02]
PLAY RECAP *********************************************************************************************
lb00 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
lb01 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
lb02 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@LB01 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id LB01
}
vrrp_instance VI_1 {
state MASTER
priority 150
interface eth0
virtual_router_id 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}
[root@LB02 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id LB02
}
vrrp_instance VI_1 {
state BACKUP
priority 100
interface eth0
virtual_router_id 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}
我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!