ansible - jinja2模板
作者:互联网
ansible - jinja2模板
什么是jinja模板
jinja2是python的全功能模板引擎
jinja2模板和ansible的关系
ansible通常会使用jinja2模板来修改被管理主机配置文件等 在sltstack中同样会使用到jinja2,模板
如果在100台主机上安装nginx,每台nginx的端口都不一样,怎么解决
ansible如何使用jinja2
使用ansible的jinja2模板也就是使用template模块,该模块和copy模块一样,都是将文件复制到远端主机上去,但是区别在于,template模块可以获取到文件中的变量,而copy则是将原封不动的文件内容复制过去。
之前我们在推送rsync的backup脚本是,想着吧脚本的变量名改成主机名,如果使用copy模块则推送过去就是{{ ansible_fdpn }},不变,如果使用template,则会变成对应的主机名
ansible使用jinja2注意事项
Ansible允许jinja2模板中使用条件判断和循环,但是不允许在playbook中使用
# 注意:不是每个管理员都需要这个特性,但是有些时候jinja2模块能大大提高效率
jinja2模板基础语法
# 调用变量
{{ 变量名 }}
{# 注释 #}
jinja2判断语法
# shell 判断
if [ 条件 ];then
xxx
elif [ 条件 ];then
aaa
else
bbb
fi
# python判断
if 条件:
xxx
elif 条件:
aaa
else:
bbb
xxxx
# jinja2判断
{% if 提条件 %}
xxx
{% elif 条件 %}
aaa
{% else %}
bbb
{% endif %}
jinja2循环
{% for n in 条件 %}
xxx
{% endfor %}
jinja2实战部署keepalived
global_defs {
router_id {{ ansible_hostname }}
}
{% if ansible_hostname == "lb01" %}
vrrp_script check_web_zh {
script "/root/check_web.sh"
interval 5
}
vrrp_instance VI_1 {
track_script {
check_web_zh
}
state MASTER
priority 150
{% else %}
vrrp_instance VI_1 {
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
}
}
jinja2实战部署负载均衡
# nginx配置文件
upstream www.zh.com {
{% for num in range(7,9) %}
server 172.16.1.{{ num }};
{% endfor %}
}
server{
listen 80;
server_name www.zh.com;
location /{
proxy_pass http://www.zh.com;
}
}
作业
# 创建www用户
- hosts: all
tasks:
- include: /root/ansible/ansible2/usadd.yml
# 客户端下载nfs服务端下载nfs和rsync
- include: /root/ansible/ansible2/yum_nfs_rsync.yml
# 配置nfs服务
- include: /root/ansible/ansible2/nfs_conf.yml
- include: /root/ansible/ansible2/nfs_dir.yml
- include: /root/ansible/ansible2/start_nfs.yml
# 部署rsync配置
- include: /root/ansible/ansible2/rsync_conf.yml
- include: /root/ansible/ansible2/rsync_pass.yml
- include: /root/ansible/ansible2/rsync_dir.yml
- include: /root/ansible/ansible2/rsy_start.yml
# 部署wordpress
- include: /root/ansible/ansible2/php_nginx_zip.yml
- include: /root/ansible/ansible2/nginx_php_yes_no_install.yml
- include: /root/ansible/ansible2/nginx_nginxconf_php_conf.yml
- include: /root/ansible/ansible2/nginx_dir.yml
- include: /root/ansible/ansible2/wordpress_unzip.yml
- include: /root/ansible/ansible2/nginx_start.yml
# 部署数据库
- include: /root/ansible/ansible2/mysql_install.yml
- include: /root/ansible/ansible2/mysql_start.yml
- include: /root/ansible/ansible2/mysql_conf.yml
- include: /root/ansible/ansible2/mysqlpython_install.yml
- include: /root/ansible/ansible2/mysql_useradd.yml
- include: /root/ansible/ansible2/mysql_wordpress_ku.yml
- include: /root/ansible/ansible2/mysql_cp_sql.yml
- include: /root/ansible/ansible2/mysql_sql_init.yml
- include: /root/ansible/ansible2/webs_mount_nfs.yml
- include: /root/ansible/ansible2/lb/yum_nginx.yml
- include: /root/ansible/sersync/yum_inot_sersync.yml
when: ansible_hostname == "nfs"
- include: /root/ansible/ansible2/lb/nginx_youhua.yml
when: ansible_hostname is match "lb*"
- include: /root/ansible/ansible2/lb/nginx_conf.yml
when: ansible_hostname is match "lb*"
- include: /root/ansible/ansible2/lb/yum_kep.yml
- include: /root/ansible/ansible2/lb/kep_conf.yml
when: ansible_hostname is match "lb*"
- include: /root/ansible/ansible2/lb/keep_sh.yml
when: ansible_hostname == "lb01"
- include: /root/ansible/ansible2/lb/start_nginx.yml
when: ansible_hostname is match "lb*"
- include: /root/ansible/sersync/sersync_dir.yml
when: ansible_hostname == "nfs"
- include: /root/ansible/sersync/unzip_sersync.yml
when: ansible_hostname == "nfs"
- include: /root/ansible/sersync/sersync_conf.yml
when: ansible_hostname == "nfs"
- include: /root/ansible/sersync/pass_sersync.yml
when: ansible_hostname == "nfs"
- include: /root/ansible/sersync/sersync_data_dir.yml
when: ansible_hostname == "backup"
- include: /root/ansible/sersync/start_sersync.yml
when: ansible_hostname == "nfs"
handlers:
- name: nginx_start
service:
name: nginx
state: restarted
- name: nginx_conf_start
service:
name: nginx
state: restarted
- name: php_start
service:
name: php-fpm
state: restarted
- name: lb_nginx
service:
name: nginx
state: restarted
- name: lb_keep
service:
name: keepalived
state: restarted
# 目录结构
[root@m01 ~]# tree
.
├── anaconda-ks.cfg
├── ansible
│ ├── 1.yml
│ ├── ansible2
│ │ ├── group_vars
│ │ │ └── all
│ │ ├── host_vars
│ │ ├── install_nfs_rsync.yml
│ │ ├── lb
│ │ │ ├── check_web.sh
│ │ │ ├── keepalived.conf
│ │ │ ├── keep_sh.yml
│ │ │ ├── kep_conf.yml
│ │ │ ├── nginx_conf.yml
│ │ │ ├── nginx_youhua
│ │ │ ├── nginx_youhua.yml
│ │ │ ├── start_nginx.yml
│ │ │ ├── www.zh.conf
│ │ │ ├── yum_kep.yml
│ │ │ └── yum_nginx.yml
│ │ ├── mysql_conf.yml
│ │ ├── mysql_cp_sql.yml
│ │ ├── mysql_install.yml
│ │ ├── mysqlpython_install.yml
│ │ ├── mysql_sql_init.yml
│ │ ├── mysql_start.yml
│ │ ├── mysql_useradd.yml
│ │ ├── mysql_wordpress_ku.yml
│ │ ├── nfs_conf.yml
│ │ ├── nfs_dir.yml
│ │ ├── nginx_dir.yml
│ │ ├── nginx_nginxconf_php_conf.yml
│ │ ├── nginx_php_yes_no_install.yml
│ │ ├── nginx_start.yml
│ │ ├── php_nginx_zip.yml
│ │ ├── rsync_conf.yml
│ │ ├── rsync_dir.yml
│ │ ├── rsync_pass.yml
│ │ ├── rsy_start.yml
│ │ ├── start_nfs.yml
│ │ ├── test.yml
│ │ ├── usadd.yml
│ │ ├── webs_mount_nfs.yml
│ │ ├── wordpress_unzip.yml
│ │ └── yum_nfs_rsync.yml
│ ├── group_vars
│ │ └── all
│ ├── host_vars
│ ├── httpd.yml
│ ├── sersync
│ │ ├── GNU-Linux-x86
│ │ │ ├── confxml.xml
│ │ │ └── sersync2
│ │ ├── pass_sersync.yml
│ │ ├── sersync2.5.4_64bit_binary_stable_final.tar.gz
│ │ ├── sersync_conf.yml
│ │ ├── sersync_data_dir.yml
│ │ ├── sersync_dir.yml
│ │ ├── start_sersync.yml
│ │ ├── unzip_sersync.yml
│ │ └── yum_inot_sersync.yml
│ └── zuoye.yml
├── group_vars
│ └── webs
├── host_ip.sh
├── host_vars
│ ├── web01
│ └── web02
├── http.yml
├── kaoshi_modify.zip
├── key.sh
├── latest-zh_CN.tar.gz
├── latest-zh_CN.tar.gz.1
├── nfs.sh
├── phpnginx
│ ├── autoconf-2.69-11.el7.noarch.rpm
│ ├── automake-1.13.4-3.el7.noarch.rpm
│ ├── libjpeg-turbo-1.2.90-8.el7.x86_64.rpm
│ ├── libmcrypt-2.5.8-13.el7.x86_64.rpm
│ ├── libmemcached-1.0.16-5.el7.x86_64.rpm
│ ├── libX11-1.6.7-4.el7_9.x86_64.rpm
│ ├── libX11-common-1.6.7-4.el7_9.noarch.rpm
│ ├── libXau-1.0.8-2.1.el7.x86_64.rpm
│ ├── libxcb-1.13-1.el7.x86_64.rpm
│ ├── libXpm-3.5.12-1.el7.x86_64.rpm
│ ├── m4-1.4.16-10.el7.x86_64.rpm
│ ├── mod_php71w-7.1.33-1.w7.x86_64.rpm
│ ├── nginx-1.22.0-1.el7.ngx.x86_64.rpm
│ ├── nginx_php.tgz
│ ├── pcre2-10.23-2.el7.x86_64.rpm
│ ├── pcre-devel-8.32-17.el7.x86_64.rpm
│ ├── perl-Data-Dumper-2.145-3.el7.x86_64.rpm
│ ├── perl-Test-Harness-3.28-3.el7.noarch.rpm
│ ├── perl-Thread-Queue-3.02-2.el7.noarch.rpm
│ ├── php71w-cli-7.1.33-1.w7.x86_64.rpm
│ ├── php71w-common-7.1.33-1.w7.x86_64.rpm
│ ├── php71w-devel-7.1.33-1.w7.x86_64.rpm
│ ├── php71w-embedded-7.1.33-1.w7.x86_64.rpm
│ ├── php71w-fpm-7.1.33-1.w7.x86_64.rpm
│ ├── php71w-gd-7.1.33-1.w7.x86_64.rpm
│ ├── php71w-mbstring-7.1.33-1.w7.x86_64.rpm
│ ├── php71w-mcrypt-7.1.33-1.w7.x86_64.rpm
│ ├── php71w-mysqlnd-7.1.33-1.w7.x86_64.rpm
│ ├── php71w-opcache-7.1.33-1.w7.x86_64.rpm
│ ├── php71w-pdo-7.1.33-1.w7.x86_64.rpm
│ ├── php71w-pear-1.10.4-1.w7.noarch.rpm
│ ├── php71w-pecl-igbinary-2.0.5-1.w7.x86_64.rpm
│ ├── php71w-pecl-memcached-3.0.4-1.w7.x86_64.rpm
│ ├── php71w-pecl-mongodb-1.5.3-1.w7.x86_64.rpm
│ ├── php71w-pecl-redis-3.1.6-1.w7.x86_64.rpm
│ ├── php71w-process-7.1.33-1.w7.x86_64.rpm
│ └── php71w-xml-7.1.33-1.w7.x86_64.rpm
├── rsyncd.conf
├── rsync.sh
├── test.yml
├── wordpress
│ ├── wordpress.sql
│ └── wordpress.tgz
├── zh_var.yml
└── zuoye.sh
标签:ansible2,rpm,ansible,jinja2,include,root,yml,模板 来源: https://www.cnblogs.com/ghjhkj/p/16441311.html