fabric学习和实验
安装package:virtualenv
[root@BIGDATA1 pip-9.0.1]# pip install virtualenv
Collecting virtualenv
Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
100% |████████████████████████████████| 1.8MB 412kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0
命令virtualenv就可以创建一个独立的Python运行环境,我们还加上了参数--no-site-packages,
这样,已经安装到系统Python环境中的所有第三方包都不会复制过来,这样,我们就得到了一个不带任何第三方包的“干净”的Python运行环境。
新建的Python环境被放到当前目录下的venv目录。有了venv这个Python环境,可以用source进入该环境:
1,创建虚拟环境
[root@BIGDATA1 fb]# virtualenv venv
New python executable in /app/fb/venv/bin/python2
Also creating executable in /app/fb/venv/bin/python
Installing setuptools, pip, wheel...done.
2,连接创建的虚拟环境
[root@BIGDATA1 fb]# source venv/bin/activate
(venv) [root@BIGDATA1 fb]#
注意到命令提示符变了,有个(venv)前缀,表示当前环境是一个名为venv的Python环境。,
3,检查当前环境package
(venv) [root@BIGDATA1 fb]# pip freeze
appdirs==1.4.3
packaging==16.8
pyparsing==2.2.0
six==1.10.0
4,退出当前虚拟环境
(venv) [root@BIGDATA1 fb]# deactivate
[root@BIGDATA1 fb]#
安装package:fbric
centos7 安装报错:
build/temp.linux-x86_64-2.7/_openssl.c:12:24: 致命错误:pyconfig.h:没有那个文件或目录
# include <pyconfig.h>
^
编译中断。
error: command 'gcc' failed with exit status 1
网上查找问题发现缺少依赖包:https://segmentfault.com/q/1010000008036881?_ea=1528598
yum install gcc libffi-devel python-devel openssl-devel -y
开始安装fabric:
Collecting fabric
Using cached Fabric-1.13.2-py2-none-any.whl
Collecting paramiko<3.0,>=1.10 (from fabric)
Using cached paramiko-2.1.2-py2.py3-none-any.whl
Collecting cryptography>=1.1 (from paramiko<3.0,>=1.10->fabric)
Using cached cryptography-1.8.1.tar.gz
Collecting pyasn1>=0.1.7 (from paramiko<3.0,>=1.10->fabric)
Using cached pyasn1-0.2.3-py2.py3-none-any.whl
Requirement already satisfied: idna>=2.1 in ./venv/lib/python2.7/site-packages (from cryptography>=1.1->paramiko<3.0,>=1.10->fabric)
Requirement already satisfied: asn1crypto>=0.21.0 in ./venv/lib/python2.7/site-packages (from cryptography>=1.1->paramiko<3.0,>=1.10->fabric)
Requirement already satisfied: packaging in ./venv/lib/python2.7/site-packages (from cryptography>=1.1->paramiko<3.0,>=1.10->fabric)
Requirement already satisfied: six>=1.4.1 in ./venv/lib/python2.7/site-packages (from cryptography>=1.1->paramiko<3.0,>=1.10->fabric)
Requirement already satisfied: setuptools>=11.3 in ./venv/lib/python2.7/site-packages (from cryptography>=1.1->paramiko<3.0,>=1.10->fabric)
Requirement already satisfied: enum34 in ./venv/lib/python2.7/site-packages (from cryptography>=1.1->paramiko<3.0,>=1.10->fabric)
Requirement already satisfied: ipaddress in ./venv/lib/python2.7/site-packages (from cryptography>=1.1->paramiko<3.0,>=1.10->fabric)
Requirement already satisfied: cffi>=1.4.1 in ./venv/lib/python2.7/site-packages (from cryptography>=1.1->paramiko<3.0,>=1.10->fabric)
Requirement already satisfied: pyparsing in ./venv/lib/python2.7/site-packages (from packaging->cryptography>=1.1->paramiko<3.0,>=1.10->fabric)
Requirement already satisfied: appdirs>=1.4.0 in ./venv/lib/python2.7/site-packages (from setuptools>=11.3->cryptography>=1.1->paramiko<3.0,>=1.10->fabric)
Requirement already satisfied: pycparser in ./venv/lib/python2.7/site-packages (from cffi>=1.4.1->cryptography>=1.1->paramiko<3.0,>=1.10->fabric)
Building wheels for collected packages: cryptography
Running setup.py bdist_wheel for cryptography ... done
Stored in directory: /root/.cache/pip/wheels/c2/12/b8/54077d2a681ad55e244675c3bad78ed27e01916b96f63a32cc
Successfully built cryptography
Installing collected packages: cryptography, pyasn1, paramiko, fabric
Successfully installed cryptography-1.8.1 fabric-1.13.2 paramiko-2.1.2 pyasn1-0.2.3
查看版本:
(venv) [root@BIGDATA1 fb]# fab --version
Fabric 1.13.2
Paramiko 2.1.2
import fabric
dir(fabric)
['builtins', 'doc', 'file', 'name', 'package', 'path']
fabric常用参数
Fab使用格式:
fab [options] <command> [:arg1,arg2=val2,host=,hosts='h1,h2,.....:] .....
-l:显示定义好的任务函数名称
-f:指定fab入口文件,缺少文件名为fabfile.py
-g:指定网关
-H:指定主机名,多主机使用逗号隔开
-P:以异步并行方式运行多主机任务,缺省情况下同步运行;
-R:指定role以角色区分不同业务组设备;
-t:设置主机连接TimeOut时间(Second);
-T:设置远程主机命令执行Timeout时间
-w:当命令执行失败,发出街区,而非默认中止命令。
-p:连接主机的密码
实验操作:
fab -H root@192.168.98.101 -p cmbc1234 -- 'hostname' #查看主机名
fab -H root@192.168.98.101 -p cmbc1234 -- 'netstat -anptu|grep 22' #查看22端口
fab -H root@192.168.98.101 -p cmbc1234 -- 'uname -a' #查看内核
fab -H root@192.168.98.101 -p cmbc1234 -- 'ip addr' #查看IP信息
串行操作:
fab -H 192.168.98.101,192.168.98.102,192.168.98.103 -u root -p cmbc1234 -- 'hostname'
(venv) [root@BIGDATA1 fb]# fab -H 192.168.98.101,192.168.98.102,192.168.98.103 -u root -p cmbc1234 -- 'hostname'
[192.168.98.101] Executing task '<remainder>'
[192.168.98.101] run: hostname
[192.168.98.101] out: BIGDATA1
[192.168.98.101] out:
[192.168.98.102] Executing task '<remainder>'
[192.168.98.102] run: hostname
[192.168.98.102] out: BIGDATA2
[192.168.98.102] out:
[192.168.98.103] Executing task '<remainder>'
[192.168.98.103] run: hostname
[192.168.98.103] out: BIGDATA3
[192.168.98.103] out:
Done.
Disconnecting from 192.168.98.101... done.
Disconnecting from 192.168.98.102... done.
Disconnecting from 192.168.98.103... done.
并行操作:-P
fab -H 192.168.98.101,192.168.98.102,192.168.98.103 -u root -p cmbc1234 -P -- 'hostname'
(venv) [root@BIGDATA1 fb]# fab -H 192.168.98.101,192.168.98.102,192.168.98.103 -u root -p cmbc1234 -P -- 'hostname'
[192.168.98.101] Executing task '<remainder>'
[192.168.98.102] Executing task '<remainder>'
[192.168.98.103] Executing task '<remainder>'
[192.168.98.102] run: hostname
[192.168.98.103] run: hostname
[192.168.98.101] run: hostname
[192.168.98.101] out: BIGDATA1
[192.168.98.101] out:
[192.168.98.103] out: BIGDATA3
[192.168.98.103] out:
[192.168.98.102] out: BIGDATA2
[192.168.98.102] out:
Done.