fabric是一款命令行工具,支持执行本地命令,执行远程命令,上传下载等。fabric像一个subprocess+paramiko的集合,又像一个更加轻量级的ansible,可以批量对服务进行操作
''' 安装fabric3 pip3 install fabric3 ''' # C:\Users\Administrator>pip3 install fabric3 # Collecting fabric3 # Downloading Fabric3-1.14.post1-py3-none-any.whl (92 kB) # |████████████████████████████████| 92 kB 73 kB/s # Requirement already satisfied: six>=1.10.0 in c:\python38\lib\site-packages (from fabric3) (1.15.0) ''' 查看版本信息 fab -V ''' # C:\Users\Administrator>fab -V # Fabric3 1.14.post1 # Paramiko 2.7.2 ''' 查看帮助信息 fab -h ''' # C:\Users\Administrator>fab -h # Usage: fab [options] <command>[:arg1,arg2=val2,host=foo,hosts='h1;h2',...] ... # # Options: # -h, --help show this help message and exit # -d NAME, --display=NAME # print detailed info about command NAM
# 导入Connection连接对象 from fabric import Connection def run(): ''' 应用部署 :return: ''' # 连接服务器 conn = Connection("docker@10.3.210.19", connect_kwargs={"password": "docker"}) # 执行控制台命令 with conn.cd('/usr/load/project'): # 拉取hello world的docker镜像 conn.run("docker pull hello world") # 启动镜像 conn.run("docker run hello world")
# 创建fabfile.py文件 # 导入本地local from fabric.api import local def hello_world(): ''' 本地命令行 :return: ''' print("查看当前文件目录") local("ll -a") # 命令行调用函数 # $ fab hello_world