Fabric is a Python (2.5-2.7) library and command-line tool used to streamline SSH execution to deploy application or system administration tasks.
Installation:
Install Fabric
Let's start with a general Demo,hello world.
Files: hello_world.py
# Coding:utf-8 def Hello (): Print ' Hello world! '
Operation Result:
The default is to find the current directory fabfile.py file, if the file name is not fabfile.py, you need to specify the file with-F.
$ fab-F hello_world.py hellohelloworld! Done .
ENV (environment variable) object Description:
EnvThe. Hosts #定义目标主机 can be represented by IP or hostname, as a list of Python definitions. such as env.hosts=['192.168.1.21','192.168.1.22']Env. exclude_hosts #排除指定主机, such as env.exclude_hosts=['192.168.1.21']Env. user #定义用户名, such as env.user='Root'Env. Port #定义端口, default is 22, such as env.port=' A'Env. password #定义密码, such as env.password='123456'Env. Passwords #定义多个密码, different hosts correspond to different passwords, such as:Env. passwords = {'[Email protected]:22':'123456','[Email protected]:22':'654321'}Env. Gateway #定义网关 (Transit, bastion) IP, such as env.gateway='192.168.1.23Env. Roledefs #定义角色分组, such as the Web Combination DB Group host, differentiate:Env. Roledefs = {'webserver':['192.168.1.21','192.168.1.22'],'DBServer':['192.168.1.25','192.168.1.26']}Env. Deploy_release_dir #自定义全局变量, Format:Env. +'Variable name', such as Env.age,Env. Sex, etc.
Where Roledefs uses the demo as follows:
Env.roledefs = {' Front':['192.168.1.111'],'API':['192.168.1.112']}@roles ('Front')defNginx_restart (): Run (' Service nginx restart') @roles ('Front','API ')defuptime (): Run ('Uptime')
Common APIs:
Local#executes a local command, such as locally (' uname-s ')Lcd#switch local directories, such as LCD ('/home ')Cd#Switch Remote directoryRun#Execute remote CommandSudo#sudo way to execute remote commands such as sudo ('/etc/init.d/httpd start ')Put#last local file remote host, such as put ('/home/user.info ', '/data/user.info ')Get#download files from remote host to local, such as: Get ('/data/user.info ', '/home/user.info ')Prompt#get user input information, such as: prompt (' Please input user password: ')Confirm#get prompt confirmation, such as: Confirm (' Test failed,continue[y/n]? ')Reboot#Restart The remote host, such as: Reboot ()@task#function modifiers, functions identified as fab callable, non-tagged to fab not visible, pure business logic@runs_once#function modifier, the identified function is executed only once and is not affected by multiple hosts
Demo (demo.py):
Run command: fab-f demo.py list
#Coding:utf-8 fromFabric.apiImport*Env.user='Root'Env.roledefs= { 'API': ['10.211.55.5:22'],}env.passwords= { '[Email protected]:22':'Linjianfeng', }deflist_files (): Run ('ls-l') @task#The Fab command is visible using @task, and other functions that do not use the @task tag are not available to the Fab command, Fab-f demo.py-l can view the Open function@roles ('API')deflist (): List_files ()
Getting started with the Python fabric module