Python-Fabric 自動化ツール - LNMP アーキテクチャを展開 (最新かつ最も完全)
生地紹介
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
ファブリックのダウンロードとインストール
パイソンをダウンロード
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
ファブリックをダウンロードしてインストールする
pip install fabric fabric3
pip freeze
fab -V
Python を使用して、これらのファブリックを呼び出すことができる変数を表示します
( 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( )
ファブリック オプション パラメータ
パラメータ項目
意味
-私
定義されたタスク関数のリストを表示します
-f
fab エントリ ファイルを指定します。デフォルトのエントリ ファイル名は fabfile.py です。
-g
指定ゲートウェイ
-H
対象のホストを指定します。複数のホストは「,」で区切ります
-P
マルチホスト タスクを非同期および並列で実行します。デフォルトはシリアル タスクです。
-R
役割を指定する (Role)
-u
ホストのユーザー名を指定します
-p
ホストのパスワードを指定
-t
デバイス接続タイムアウトの設定
-T
リモート ホスト コマンドの実行タイムアウトを設定する
-w
コマンドの実行が失敗した場合、デフォルトでタスクを終了する代わりに警告を発行する
ファブファイルを書く
属性
意味
フォーマット
環境ホスト
複数のターゲット ホストを定義する
env.hosts=[“10.0.0.4”,“10.0.0.5”]
環境ユーザー
ユーザー名を定義する
env.user =「ルート」
環境ポート
ポートの定義
環境ポート = 22
環境パスワード
パスワードを定義する
環境パスワード = 1
環境パスワード
複数のホストのユーザー名、IP アドレス、ポート、およびパスワードを定義する
env.passwords = { "[email protected] :22":"1", }
環境ゲートウェイ
ゲートウェイを定義する
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('hostname')
液晶
ローカルディレクトリを切り替える、lcd('/root')
CD
リモートディレクトリの切り替え、cd('cd')
走る
以下のようなリモートコマンドを実行します: run('hostname')
須藤
Sudo は、次のようなリモート コマンドを実行します。 sudo('echo "123456")
置く
put(src,des) のようなローカル ファイルをリモート ホストにアップロードします。
得る
get(des,src) のように、リモート ホストからローカルにファイルをダウンロードします。
促す
次のようなユーザー入力情報を取得します: プロンプト (「新しいパスワードを入力してください:」)
確認
次のようなプロンプトメッセージの確認を取得します: confirm('failed.Continue[Y/n]?')
リブート
リモートホストを再起動します、reboot()
@タスク
関数修飾子、識別された関数は fab によって呼び出し可能です
@runs_once
関数修飾子、指定された関数は一度だけ実行されます
出力色
from fabric.colors import *
色
入力
黄色
黄(「ハローワールド」)
青
青(「ハローワールド」)
赤
赤(「ハローワールド」)
緑
緑(「ハローワールド」)
青
シアン(「ハローワールド」)
紫
マゼンタ(「ハローワールド」)
白
白(「ハローワールド」)
多ホスト一括並列運用・保守
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管理機
ウィンドウズ10
10.0.0.1
パイソン.ファブリック
web01 サーバー
CentOS 7.6
10.0.0.13
LNMP
web02 サーバー
CentOS 7.6
10.0.0.14
LNMP
ファブファイル
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( )
書くのは簡単ではありません。いいねとコメントを忘れずにお願いします。最後に新年の幸運を祈ります~~~