Getting started with the Python fabric module

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.