Python-Fabric automation tool - deploy LNMP architecture (latest and most complete)
fabric introduction
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 download and install
Download Python
apt-get install python 或者 yum -y install python
apt install pip 或者 yum -y install python
python -V
pip -V
Install virtualenv virtualization
pip install virtualenv
mkdir py && cd py/
virtualenv py
source py/bin/activate
pip freeze
deactivate
Download and install fabric
pip install fabric fabric3
pip freeze
fab -V
Use python to view the variables that can call those fabrics
( 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 option parameters
parameter item
meaning
-I
Display the list of defined task functions
-f
Specify the fab entry file, the default entry file name is fabfile.py
-g
designated gateway
-H
Specify the target host, multiple hosts are separated by ","
-P
Run multi-host tasks asynchronously and in parallel, the default is serial tasks
-R
Specify the role (Role)
-u
Specifies the host username
-p
Specify host password
-t
Set device connection timeout
-T
Set the remote host command execution timeout
-w
When the command execution fails, issue a warning instead of terminating the task by default
write fabfile
Attributes
meaning
Format
env.hosts
Define multiple target hosts
env.hosts=[“10.0.0.4”,“10.0.0.5”]
env.user
define username
env.user = “root”
env.port
define port
env.port = 22
env.password
define password
env.password = 1
env.passwords
Define user names, IP addresses, ports, and passwords for multiple hosts
env.passwords = {
“[email protected] :22”:“1”, }
env.gateway
define gateway
env.gateway = “10.0.0.2”
env.roledefs
Define role groupings
ren.roledefs = {
“webservers”:[“10.0.0.4”,“10.0.0.5”] “dbservers”:[“10.0.0.6”,“10.0.0.7”]}
env.exclide_hosts
Exclude specified hosts, expressed as a python list
env.deploy_release_dir
Custom global variables
Commonly used APIs for local and remote operation and maintenance
method
illustrate
local
Execute local commands, such as: local('hostname')
lcd
Switch local directory, lcd('/root')
cd
Switch remote directory, cd('cd')
run
Execute remote commands, such as: run('hostname')
sudo
Sudo executes remote commands, such as: sudo('echo "123456")
put
Upload local files to remote hosts, such as: put(src,des)
get
Download files from remote host to local, such as: get(des,src)
prompt
Obtain user input information, such as: prompt ('please enter a new password:')
confirm
Obtain prompt message confirmation, such as: confirm('failed.Continue[Y/n]?')
reboot
Reboot the remote host, reboot()
@task
Function modifier, the identified function is callable by fab
@runs_once
Function modifier, the indicated function will only be executed once
output color
from fabric.colors import *
color
enter
yellow
yellow(“hello world”)
blue
blue(“hello world”)
red
red(“hello world”)
green
green(“hello world”)
blue
cyan(“hello world”)
Purple
magenta(“hello world”)
White
white(“hello world”)
Multi-host batch parallel operation and maintenance
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 multi-host deployment-LNMP (Linux+Nginx+MySQL+PHP)
server preparation
host name
operating system
IP address
main program
win10 management machine
windows 10
10.0.0.1
Python.Fabric
web01 server
CentOS 7.6
10.0.0.13
LNMP
web02 server
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( )
Writing is not easy, please remember to like and comment, and finally wish you good luck in the new year~~~