jinjia2模板使用
目录
1、jinjia2模板语法
jinja2是Python的全功能模板引擎。
1、变量的使用
{{ 变量名 }}
{{ 变量名 计算公式 }}
2、判断语句的使用
{% if 1 == 1 %}
{% elif 条件 %}
{% else %}
{% endif %}
3、循环语句的使用
{% for i in EXPR %}
{% endfor %}
2、jinjia2模板使用
jinjia2模板不能单独使用,只能跟剧本配合使用。
{% if ansible_distribution == "CentOS" %}
WelCome to {{ ansible_distribution }} - {{ ansible_distribution_version }} 空闲内存大小 {{ ansible_memfree_mb * 1000 }}
{% else %}
Welcome to China
{% endif %}
{% for i in ansible_date_time %}
{{ i }}
{% endfor %}
3、Ansible Roles
解决了剧本的问题?
1、解决了剧本的耦合性
2、还可以利用剧本的便利性
4、创建Roles
[root@localhost ansible]# yum -y install tree
[root@localhost ansible]# ansible-galaxy init nginx
- Role nginx was created successfully
[root@localhost ansible]# tree nginx/
nginx/
├── defaults # 变量(优先级低)
│ └── main.yml
├── files # 存放文件
├── handlers # 存放handlers
│ └── main.yml
├── meta # 存放依赖roles
│ └── main.yml
├── README.md
├── tasks # 存放具体任务的目录
│ └── main.yml
├── templates # 存放jinjia2模板的
├── tests # 存放测试文件
│ ├── inventory
│ └── test.yml
└── vars # 存放变量
└── main.yml
8 directories, 8 files
5、调用Roles
1、创建一个yaml文件
2、使用roles模块
6、部署小游戏
1、创建roles
[root@localhost ansible]# ansible-galaxy init mario
- Role mario was created successfully
2、存放代码
[root@localhost ansible]# cp /root/mario.tar.gz mario/files/
3、编写配置文件
卸载受控端Nginx
[root@localhost ansible]# cat mario/tasks/main.yml
---
- name: 安装Nginx
yum:
name: nginx
state: present
notify: 启动Nginx
- name: 上传代码
unarchive:
src: ./mario.tar.gz
dest: /usr/share/nginx/html
remote_src: no
[root@localhost ansible]# cat mario/handlers/main.yml
---
# handlers file for mario
- name: 启动Nginx
service:
name: nginx
state: restarted
[root@localhost ansible]# cat roles.yaml
- hosts: web01
name: 部署超级玛丽
roles:
- mario
[root@localhost ansible]# ansible-playbook roles.yaml
7、期中架构
7.1、公共roles
[root@localhost ~]# hostnamectl set-hostname m01
[root@localhost ~]# bash
[root@m01 ~]# mkdir project
[root@m01 ~]# cd project/
[root@m01 project]# pwd
/root/project
[root@m01 project]# ansible-galaxy init common
[root@m01 project]# ansible-doc service
[root@m01 project]# cat common/tasks/main.yml
- name: 关闭防火墙
service:
name: firewalld
state: stopped
enabled: no
- name: 关闭Selinux
selinux:
state: disabled
- name: 安装NFS
yum:
name: nfs-utils
state: present
when:
- ansible_distribution == "CentOS"
- name: 创建全局用户组
group:
name: www
state: present
gid: 666
- name: 创建全局用户
user:
name: www
comment: 全局应用程序用户
uid: 666
group: www
shell: /sbin/nologin
state: present
create_home: false
7.2、安装部署NFS
[root@m01 project]# ansible-galaxy init nfs
[root@m01 project]# cat nfs/tasks/main.yml
- name: 安装RpcBind
yum:
name: rpcbind
state: present
- name: 创建挂载点
file:
path: /backup
owner: www
group: www
mode: 777
state: directory
- name: 创建NFS配置文件
template:
src: ./nfs.j2
dest: /etc/exports
- name: 启动NFS
service:
name: "{{ item }}"
state: restarted
with_items:
- nfs-server
- rpcbind
[root@m01 project]# cat nfs/files/nfs.j2
/backup 192.168.15.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
7.3、部署数据库
[root@m01 project]# ansible-galaxy init db
[root@m01 project]# vim db/tasks/mainl.yml
- name: 安装MariaDB
yum:
name: "{{ item }}"
state: present
with_items:
- mariadb
- mariadb-server
- name: 启动Mariadb
service:
name: mariadb
state: restarted
- name: 创建远程连接用户和数据库
shell: /usr/bin/mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Test@666' WITH GRANT OPTION;FLUSH PRIVILEGES;CREATE DATABASE django;"
7.4、部署WEB
[root@m01 ~]# yum install python3 -y
[root@m01 ~]# pip3 install django
[root@m01 ~]# django-admin startproject linux20
[root@m01 ~]# cd linux20/
[root@m01 linux20]# django-admin startapp application
[root@m01 linux20]# vim linux20/settings.py
ALLOWED_HOSTS = ['*']
DATABASES = {}
[root@m01 linux20]# python3 manage.py runserver 0.0.0.0:8000
[root@m01 linux20]# pip3 install uwsgi
[root@m01 linux20]# vim myweb.ini
[uwsgi]
# 端口号
socket = :8000
# 指定项目的目录
chdir = /opt/linux20
# wsgi文件路径
wsgi-file = linux20/wsgi.py
# 模块wsgi路径
module = linux20.wsgi
# 是否开启master进程
master = true
# 工作进程的最大数目
processes = 4
# 结束后是否清理文件
vacuum = true
[root@m01 linux20]# cd ..
[root@m01 ~]# mv linux20 /opt/
[root@m01 ~]# cd /opt/
[root@m01 opt]# cd linux20/
[root@m01 linux20]# pwd
/opt/linux20
[root@m01 linux20]# uwsgi --ini myweb.ini
[root@m01 linux20]# cd ..
[root@m01 linux20]# cd ..
[root@m01 opt]# tar -czvf linux20.tar.gz linux20
[root@m01 opt]# cd
[root@m01 ~]# cd project/
[root@m01 project]# vim web/tasks/main.yml
- name: 卸载残留
yum:
name: "{{ item }}"
state: absent
with_items:
- nginx
- httpd
- name: 安装Nginx
yum:
name: "{{ item }}"
state: present
with_items:
- nginx
- python3
- python3-devel
- name: 安装Django
shell: pip3 install django -i http://pypi.doubanio.com/simple/ --trusted-hostpypi.doubanio.com
- name: 安装uwsgi
shell: pip3 install uwsgi -i http://pypi.doubanio.com/simple--trusted-hostpypi.doubanio.com
- name: 上传代码
unarchive:
src: ./linux20.tar.gz
dest: /opt/
owner: www
group: www
remote_src: no
- name: 上传Nginx配置文件
template:
src: ./nginx.conf.j2
dest: /etc/nginx/nginx.conf
- name: 上传Nginx主机配置文件
template:
src: ./django.conf.j2
dest: /etc/nginx/conf.d/default.conf
- name: 启动UWSGI
shell: "cd /opt/linux20 && /usr/local/bin/uwsgi -d --ini myweb.ini"
- name: 启动Nginx
service:
name: nginx
state: restarted
[root@m01 project]# vim web/templates/diango.conf.j2
server {
listen 80;
server_name www.django.com;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
uwsgi_read_timeout 2;
uwsgi_param UWSGI_SCRIPT linux20.wsgi;
uwsgi_param UWSGI_CHDIR /opt/linux20;
index index.html index.htm;
client_max_body_size 35m;
}
}
[root@m01 project]# vim web/templates/nginx.conf.j2
user www;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
[root@m01 project]# vim /etc/ansible/hosts
[web01]
192.168.15.7 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
[web02]
192.168.15.8 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
[web03]
192.168.15.9 ansible_ssh_user=root ansible_ssh_port=22ansible_ssh_pass='1'
[web:children]
web01
web02
web03
[lb01]
192.168.15.5 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
[lb02]
192.168.15.6 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
[lb:children]
lb01
lb02
[nfs]
192.168.15.31 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
[db01]
192.168.15.51 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
[root@m01 project]# ssh 192.168.15.81
[root@m01 project]# ansible web02 -m setu
[root@m01 project]# ansible-galaxy init lb
[root@m01 project]# vim lb/tasks/main.yml
- name: 安装高可用软件
yum:
name: "{{ item }}"
state: present
with_items:
- nginx
- keepalived
- name: 配置Nginx
template:
src: ./nginx.conf.j2
dest: /etc/nginx/nginx.conf
- name: 配置Upstream
template:
src: ./upstream.conf.j2
dest: /etc/nginx/upstream.conf
- name: 配置Nginx
template:
src: ./lb.conf.j2
dest: /etc/nginx/conf.d/default.conf
- name: 配置keepalived
template:
src: ./keepalived.conf.j2
dest: /etc/keepalived/keepalived.conf
- name: 启动Nginx和Keepalived
service:
name: "{{ item }}"
state: restarted
with_items:
- nginx
- keepalived
[root@m01 project]# vim lb/templates/nginx.conf.j2
user www;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
[root@m01 project]# vim lb/templates/lb.conf.j2
upstream web {
server 192.168.15.7;
server 192.168.15.8;
server 192.168.15.9;
}
server {
listen 80;
server_name www.django.com;
location / {
proxy_pass http://web;
include upstream.conf;
}
}
[root@m01 project]# vim lb/templates/upstream.conf.j2
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_intercept_errors on;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
[root@m01 project]# vim lb/templates/keepalived.conf.j2
! Configuration File for keepalived
# 全局配置
global_defs {
# 当前keepalived唯一标识
router_id {{ ansible_fqdn }}
}
# 配置VRRP协议
vrrp_instance VI_1 {
{% if ansible_fqdn == "lb01" %}
# 状态,MASTER和BACKUP
state MASTER
# 优先级
priority 100
{% else %}
# 状态,MASTER和BACKUP
state BACKUP
# 优先级
priority 90
{% endif %}
# 绑定网卡
interface eth0
# 虚拟路由标示,可以理解为分组
virtual_router_id 50
# 监测心跳间隔时间
advert_int 1
# 配置认证
authentication {
# 认证类型
auth_type PASS
# 认证的密码
auth_pass 1111
}
# 设置VIP
virtual_ipaddress {
# 虚拟的VIP地址
192.168.15.3
}
}
[root@m01 project]# vim roles.yaml
- hosts: all
name: 全局初始化
roles:
- common
- hosts: nfs
name: NFS相关操作
roles:
- nfs
- hosts: db01
name: 数据库相关操作
roles:
- db
- hosts: web
name: WEB相关操作
roles:
- web
- hosts: lb
name: 负载均衡相关操作
roles:
- lb
[root@m01 project]# vim /root/project/nfs/templates/nfs.j2
/backup 192.168.15.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类