Python-Fabric自动化工具-部署LNMP架构(最新,最全)
fabric介绍
Fabric是一个高级Python( 2.7-3.4+) 的库和命令行工具,用来提高基于SSH的应用部署和系统管理效率.
更具体的说,Fabric是:
1.一个让你通过命令行执行无阐述python函数的工具;
2.一个让通过SSH执行shell命令更加容易,更符合python风格的命令库,( 建立于一个更低层次的库)
自然而然的,大部分用户把这两件事结合着用,使用Fabric来写和执行Python函数或task,以实现与远程服务器的自动化交互.
官方文档:https://docs.fabfile.org/en/2.6/getting-started.html
fabric下载安装
下载Python
apt-get install python 或者 yum -y install python
apt install pip 或者 yum -y install python
python -V
pip -V
安装virtualenv虚拟化
pip install virtualenv
mkdir py && cd py/
virtualenv py
source py/bin/activate
pip freeze
deactivate
下载安装fabric
pip install fabric fabric3
pip freeze
fab -V
使用python查看能调用那些fabric的变量
( py) root@hy: / home/ hy/ py
Python 3.8 .5 ( default, May 27 2021 , 13 : 30 : 53 )
[ GCC 9.3 .0 ] on linux
Type "help" , "copyright" , "credits" or "license" for more information.
>> > import fabric
>> > dir ( fabric)
[ 'Config' , 'Connection' , 'Executor' , 'Group' , 'GroupResult' , 'Remote' , 'Result' , 'SerialGroup' , 'Task' , 'ThreadingGroup' , '__builtins__' , '__cached__' , '__doc__' , '__file__' , '__loader__' , '__name__' , '__package__' , '__path__' , '__spec__' , '__version__' , '__version_info__' , '_version' , 'config' , 'connection' , 'exceptions' , 'executor' , 'group' , 'runners' , 'task' , 'tasks' , 'transfer' , 'tunnels' , 'util' ]
>> > exit( )
fabric选项参数
参数项
含义
-I
显示定义好的任务函数列表
-f
指定fab入口文件,默认入口文件名为fabfile.py
-g
指定网关
-H
指定目标主机,多个主机用","隔开
-P
以异步并行方式运行多主机任务,默认为串行任务
-R
指定角色(Role)
-u
指定主机用户名
-p
指定主机密码
-t
设置设备连接超时时间
-T
设置远程主机命令执行超时时间
-w
当命令执行失败,发出告警,而非默认终止任务
编写fabfile
属性
含义
格式
env.hosts
定义多个目标主机
env.hosts=[“10.0.0.4”,“10.0.0.5”]
env.user
定义用户名
env.user = “root”
env.port
定义端口
env.port = 22
env.password
定义密码
env.password = 1
env.passwords
定义多台主机的用户名,IP地址,端口,密码
env.passwords = {
“[email protected] :22”:“1”, }
env.gateway
定义网关
env.gateway = “10.0.0.2”
env.roledefs
定义角色分组
ren.roledefs = {
“webservers”:[“10.0.0.4”,“10.0.0.5”] “dbservers”:[“10.0.0.6”,“10.0.0.7”]}
env.exclide_hosts
排除指定主机,以python的列表表示
env.deploy_release_dir
自定义全局变量
本地与远程运维常用API
方法
说明
local
执行本地命令,如:local(‘hostname’)
lcd
切换本地目录,lcd(’/root’)
cd
切换远程目录,cd(‘cd’)
run
执行远程命令,如:run(‘hostname’)
sudo
sudo执行远程命令,如:sudo('echo “123456″)
put
上传本地文件到远程主机,如:put(src,des)
get
从远程主机下载文件到本地,如:get(des,src)
prompt
获取用户输入信息,如:prompt(‘please enter a new password:’)
confirm
获取提示信息确认,如:confirm(‘failed.Continue[Y/n]?’)
reboot
重启远程主机,reboot()
@task
函数修饰符,标识的函数为fab可调用的
@runs_once
函数修饰符,表示的函数只会执行一次
输出颜色
from fabric.colors import *
颜色
输入
黄色
yellow(“hello world”)
蓝色
blue(“hello world”)
红色
red(“hello world”)
绿色
green(“hello world”)
青色
cyan(“hello world”)
紫色
magenta(“hello world”)
白色
white(“hello world”)
多主机批量并行运维
1.指定多台主机IP或域名
env.hosts = [ "10.0.0.x" ,"10.0.0.xx" ,"10.0.0.xxx" ,.. .]
2.指定多台主机的密码
env.passwords = {
"[email protected] :22" : "root" ,
"[email protected] :22" : "root" ,
"[email protected] :22" : "root" ,
.. .
}
3.指定主机角色分组
env.roledefs = {
"xxx1" :[ "10.0.0.x" ,"10.0.0.xx" ,"10.0.0.xxx" ,.. .] ,
"xxx2" :[ "10.0.0.x" ,"10.0.0.xx" ,"10.0.0.xxx" ,.. .] ,
}
4.并行装饰器:@parallel
5.角色装饰器:@roles( 角色)
6.主机装饰器:@hosts( 主机1,主机2)
centos7多主机部署-LNMP(Linux+Nginx+MySQL+PHP)
服务器准备
主机名称
操作系统
IP地址
主要程序
win10管理机
windows 10
10.0.0.1
Python.Fabric
web01服务器
CentOS 7.6
10.0.0.13
LNMP
web02服务器
CentOS 7.6
10.0.0.14
LNMP
Fabfile
from fabric.api import *
env.hosts = [ "10.0.0.13" , "10.0.0.14" ]
env.user = "root"
env.passwords = {
"[email protected] :22" : "1" ,
"[email protected] :22" : "1"
}
@task
def php_install( ) :
a = "" "[php-webtatic]\nname = PHP Repository\nbaseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/\ngpgcheck = 0" ""
run( "echo '%s' > /etc/yum.repos.d/php.repo" % a)
run( "yum -y install php72w php72w-cli php72w-fpm php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml" )
@task
def php_start( ) :
run( "systemctl start php-fpm" )
run( "systemctl enable php-fpm" )
run( "systemctl status php-fpm" )
run( "netstat -tnulp | grep 9000" )
@task
def nginx_install( ) :
ng = "" "[nginx-stable]\nname=nginx stable repo\nbaseurl=http://nginx.org/packages/centos/$releasever /$basearch /\ngpgcheck=1\nenabled=1\ngpgkey=https://nginx.org/keys/nginx_signing.key\nmodule_hotfixes=true" ""
run( "echo '%s' > /etc/yum.repos.d/nginx.repo" % ng)
run( "yum -y install nginx" )
@task
def nginx_start( ) :
run( "nginx -t" )
run( "systemctl start nginx" )
run( "systemctl enable nginx" )
run( "systemctl status nginx" )
run( "netstat -tnulp | grep 80" )
@task
def mariadb_install( ) :
run( "yum -y install mariadb-server" )
@task
def mariadb_start( ) :
run( "systemctl start mariadb" )
run( "systemctl enable mariadb" )
run( "systemctl status mariadb" )
run( "netstat -tnulp | grep 3306" )
@task
def mariadb_password( ) :
run( "mysqladmin -uroot -p password 123456" )
@task
def mariadb_create( ) :
run( "mysql -uroot -p123456 -e 'create database wordpress;'" )
run( "mysql -uroot -p123456 -e 'show databases;'" )
run( "systemctl status mariadb" )
run( "netstat -tnulp | grep 3306" )
@task
def nginx_server( ) :
b = "" "
server {
listen 80;
server_name localhosts;
root /code/wordpress;
index index.php index.html;
location ~ \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root $fastcgi_script_name ;
include fastcgi_params;
}
}
" ""
run( "mkdir /code -p" )
run( "echo '%s' >> /etc/nginx/conf.d/wordpress.conf" % b)
@task
def rz_wordpress( ) :
put( "wordpress-5.0.3-zh_CN.tar.gz" , "/opt/wordpress-5.0.3-zh_CN.tar.gz" )
run( "tar xf /opt/wordpress-5.0.3-zh_CN.tar.gz -C /code/" )
@task
def chmod_chown( ) :
run( "chmod -R 755 /code" )
run( "chmod -R 755 /etc/nginx/conf.d/wordpress.conf" )
run( "chown -R root.root /code" )
@task
def version_server( ) :
run( "mysql --version" )
run( "php -v" )
run( "nginx -V" )
run( "netstat -tnulp" )
@task
def curl_I( ) :
run( "curl -I 10.0.0.13" )
run( "curl -I 10.0.0.14" )
def run_all( ) :
execute( php_install)
execute( php_start)
execute( nginx_install)
execute( nginx_start)
execute( mariadb_install)
execute( mariadb_start)
execute( mariadb_password)
execute( mariadb_create)
execute( nginx_server)
execute( rz_wordpress)
execute( chmod_chown)
execute( version_server)
execute( curl_I)
if __name__ == "__main__" :
run_all( )
编写不易,各位小伙伴记得点赞、评论,最后祝君在新的一年中好运爆棚~~~