fabric安装
目前,从PyPI可以搜索到主要的fabric库为“ Fabric 2.1.3 ”、“ fabric2 2.1.3 ”和“ Fabric3 1.14.post1 ”。
-
Fabric:官方Fabric,兼容 Python 2 & Python 3,但不兼容Fabric 1.x的fabfile;
-
fabric2: 与Fabric相同,仅作为平滑迁移(使用Fabric包安装1.x 版本,使用Fabric2包安装2.x版本,来实现1.x和2.x的共存);
-
Fabric3:是一个基于Fabric 1.x 的fork,兼容Python2 & Python3,兼容 Fabric1.x 的 fabfile;
1、centos安装fabric3 1.14 post1
sudo yum install python34.x86_64
python3.4 -m venv fabric-env source fabric-env/bin/activate
pip install --upgrade pip pip install fabric3 deactivate
2、 ubuntu 安装fabric3
apt-get update apt install python3-pip pip3 install virtualenv virtualenv learn-virtualenv source fabric-env/bin/activate pip install fabric3 deactivate
3、windows安装
##下载get-pip.py ## https://pip.pypa.io/en/stable/installing/ #安装pip C:Userswongg>python get-pip.py Collecting pip #安装虚拟环境程序 C:Userswongg>pip install virtualenv Collecting virtualenv Successfully installed virtualenv-15.2.0 #创建虚拟环境 C:Userswongg>virtualenv cmdb-virtualenv #进入虚拟环境 C:Userswongg>cmdb-virtualenvScriptsactivate
4、检验安装
(fabric-env) # fab -V Fabric3 1.14.post1 Paramiko 2.4.1
C:Userswongglearn>fab -V Fabric3 1.14.post1 Paramiko 2.4.1
$ pip show fabric3 Name: Fabric3 Version: 1.14.post1 Summary: Fabric is a simple, Pythonic tool for remote execution and deployment (py2.7/py3.4+ compatible fork). Home-page: https://github.com/mathiasertl/fabric/ Author: Jeff Forcier Author-email: jeff@bitprophet.org License: UNKNOWN Location: /home/huangjunfa/fabric-env/lib/python2.7/site-packages Requires: paramiko, six Required-by: $ python -c "from fabric.api import env ; print (env.version)" 1.14.post1
5、常用设置
import 设置
import sys,os from fabric.api import env,local,cd,lcd,run,task,execute
用户密码设置一
env.hosts = ['10.10.3.2','10.10.0.10'] env.user = 'test' env.password ="fabric20180703"
rsa设置一
env.hosts = ['10.10.3.2','10.10.0.10'] env.user = 'test'
env.key_filename = os.path.join(sys.path[0],'rsa','huangjunfa.key')
用户密码分别设置二
env.hosts = ['user111@10.10.0.10','user222@10.10.3.2'] env.passwords ={'user111@10.10.0.10:22':'fabric20180703','user222@10.10.3.2:22':'fabric20180703'}
rsa用户分别设置二
env.hosts = ['user111@10.10.0.10','user222@10.10.3.2'] env.key_filename = os.path.join(sys.path[0],'rsa','id_rsa.key')
6、直接调用命令执行
# @task def local_cmd(): local('ls -la') with lcd('/mnt'): local('ls -l') #@task def remote_cmd(): #run('free -m') #run('cat /etc/passwd') print('host_string:%s '%env.host_string) with cd(''): run('ls') print('host_string:%s '%env.passwords) # run('du -h') out=run('ps -ef |grep "/usr/sbin/httpd"|grep -v grep|wc -l') print('result:%s '%(out)) if out != 0: print('application is running!!') if __name__ == '__main__': #execute(remote_cmd,host="10.10.3.2") execute(remote_cmd) ##按顺序在每台host上执行remote_cmd
环境设置官方文档:
http://fabric-chs.readthedocs.io/zh_CN/chs/usage/env.html
fab命令运行调用过程
Traceback (most recent call last): File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesfabric etwork.py", line 478, in connect client.connect(**kwargs) File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesparamikoclient.py", line 424, in connect passphrase, File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesparamikoclient.py", line 714, in _auth raise saved_exception File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesparamikoclient.py", line 701, in _auth self._transport.auth_password(username, password) File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesparamiko ransport.py", line 1381, in auth_password return self.auth_handler.wait_for_response(my_event) File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesparamikoauth_handler.py", line 226, in wait_for_response raise e paramiko.ssh_exception.AuthenticationException: Authentication failed. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:Userswongglearn t.py", line 49, in <module> execute(remote_cmd) File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesfabric asks.py", line 387, in execute multiprocessing File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesfabric asks.py", line 277, in _execute return task.run(*args, **kwargs) File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesfabric asks.py", line 174, in run return self.wrapped(*args, **kwargs) File "C:Userswongglearn t.py", line 36, in remote_cmd run('ls') File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesfabric etwork.py", line 692, in host_prompting_wrapper return func(*args, **kwargs) File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesfabricoperations.py", line 1095, in run shell_escape=shell_escape, capture_buffer_size=capture_buffer_size, File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesfabricoperations.py", line 935, in _run_command channel=default_channel(), command=wrapped_command, pty=pty, File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesfabricstate.py", line 435, in default_channel chan = _open_session() File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesfabricstate.py", line 416, in _open_session transport = connections[env.host_string].get_transport() File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesfabric etwork.py", line 156, in __getitem__ self.connect(key) File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesfabric etwork.py", line 148, in connect user, host, port, cache=self, seek_gateway=seek_gateway) File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesfabric etwork.py", line 566, in connect password = prompt_for_password(text) File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesfabric etwork.py", line 651, in prompt_for_password new_password = _password_prompt(password_prompt, stream) File "C:UserswonggAppDataLocalProgramsPythonPython36libsite-packagesfabric etwork.py", line 623, in _password_prompt return getpass.getpass(prompt, stream) File "C:UserswonggAppDataLocalProgramsPythonPython36libgetpass.py", line 101, in win_getpass return fallback_getpass(prompt, stream) File "C:UserswonggAppDataLocalProgramsPythonPython36libgetpass.py", line 127, in fallback_getpass return _raw_input(prompt, stream) File "C:UserswonggAppDataLocalProgramsPythonPython36libgetpass.py", line 147, in _raw_input line = input.readline() KeyboardInterrupt