fabric是python的运维神器,可以方便执行各种远程操作
1.安装
python3 uninstall fabric python3 install fabric3
命令必须写在fabfile.py下的方法中
以fab 方法名 的方式执行
# 文件名必须为fabfile.py from fabric.api import * from fabric.contrib.project import rsync_project env.hosts = ['hw-node1', 'hw-node2', 'hw-node3', 'hw-node5'] env.port = 22 env.user = 'root' def cmd(): # 本地运行 local('ls') # 本地切换目录 lcd('/path/to/dir') # 远程切换目录 cd('/path/to/dir') # root权限运行命令 sudo() # 远程执行 def cmd1(): run("hostname") # 从本地同步到远程 def cmd2(): put('/root/get-pip.py', '/root/get-pip.py') # python版rsync,只能自建一级目录或文件 def sync_file(): rsync_project(remote_dir='/root/root/', local_dir='/root/root/')
2.path
path():配置远程服务器PATH环境变量,只对当前会话有效,不会影响远程服务器的其他操作,path的修改支持多种模式
- append:默认行为,将给定的路径添加到PATH后面。
- prepend:将给定的路径添加到PATH的前面。
- replace:替换当前环境的PATH变量。
def addpath(): with path('/tmp','prepend'): run("echo $PATH") run("echo $PATH")
添加远程机的path
from fabric.api import env, run, path env.hosts = ['bjhee@example1.com', ] env.password = '111111' def hello(): with path('/home/bjhee/tmp'): run('echo $PATH') run('echo $PATH')
假设我们的PATH环境变量默认是”/sbin:/bin”,在上述”with path()”语句块内PATH变量将变为”/sbin:/bin:/home/bjhee/tmp”。出了with语句块后,PATH又回到原来的值。
3.shell_env()
设置 shell 脚本的环境变量。可以用来临时设置远程和本地机上Shell的环境变量。
from fabric.api import env, run, local, shell_env env.hosts = ['bjhee@example1.com', ] env.password = '111111' def hello(): with shell_env(JAVA_HOME='/opt/java'): run('echo $JAVA_HOME') local('echo $JAVA_HOME')
4.装饰器
Fabric提供的命令一般都是执行某一个具体的操作,提供的上下文管理器一般都是用于临时修改配置参数,而fabric提供的装饰器,既不是执行具体的操作,也不是修改参数,而是控制如何执行这些操作,在那些服务器上执行这些操作,fabric的装饰器与人物执行紧密相关。下面从几个方面来进行说明
- hosts:定制执行task的服务器列表
- roles:定义执行task的role列表
- parallel:并行执行task
- serial:串行执行task
- task:定义一个task
- runs_once:该task只执行一次
5.安装redis
#!/usr/bin/env python3 from fabric.api import * from fabric.contrib.console import confirm from fabric.utils import abort from fabric.colors import * env.hosts = ['192.168.10.202',] env.user = 'root' env.password = '123456202' @runs_once @task def test(): with settings(warn_only=True): local('tar xf redis-4.0.9.tar.gz') with lcd('redis-4.0.9'): result = local('make test',capture=True) if result.failed and not confirm('Test is Faild Continue Anyway?'): abort('Aborting at user request.') with lcd('redis-4.0.9'): local("make clean") local('tar zcvf redis-4.0.10.tar.gz redis-4.0.9') @task def deploy(): put('redis-4.0.10.tar.gz','/tmp/') with cd('/tmp'): run('tar xf redis-4.0.10.tar.gz') with cd('redis-4.0.9'): sudo('make install') @task def start_redis(): with settings(warn_only=True): result = run('netstat -lntup | grep -w redis-server') if result.return_code == 0: print(green('redis is started!')) else: run('set -m ; /usr/local/bin/redis-server &') # 用pty=False, fabric进程退不出来,不知道为啥,所以这里用set -m print(green('redis start Successful')) @task def clean_local_file(): local('rm -rf redis-4.0.10.tar.gz') @task def clean_file(): with cd('/tmp'): sudo('rm -rf redis-4.0.9') sudo('rm -rf redis-4.0.10.tar.gz') @task def install(): execute(test) execute(deploy) execute(clean_file) execute(clean_local_file) execute(start_redis)
参考文档:https://blog.csdn.net/freeking101/article/details/81103945