How to automate your task with fabric _python

Source: Internet
Author: User
Tags exception handling ssh

Let's first look at an example. We know that under *nix, the uname command is to view the distribution of the system.

You can write a fabric script like this:

From Fabric.api import Run
def host_type ():
 run (' uname-s ')

By saving the above script as fabfile.py, you can fab execute the Host_type script on multiple hosts by command:

$ fab-h localhost,linuxbox host_type
[localhost] run:uname-s
[localhost] out:darwin
[Linuxbox] Run:uname -S
[Linuxbox] Out:linux

You may need to enter the system password during execution.

Installation

If you see this, you are beginning to be interested in fabric. However, the above operation cannot be performed at your place because you have not yet installed fabric. Installation of fabric is simple, you can use pip or easy_install , you can download the original code installation.

Task functions

Well, installing the fabric doesn't baffle you. Maybe you've successfully performed the previous task and now let's go a little deeper.

The task in fabric is a python function, let's call it "task function." Since it is a python function, some usages of the function also apply to the task function. such as passing parameters, calling each other, returning values, and so on.

First look at an example of passing parameters:

def hello (name= "World"):
 print ("Hello%s!"% name)

When performing a task, you can pass fab parameters for the task function by using command-line arguments:

$ fab hello:name=holbrook
Hello holbrook!

Examples of combinatorial tasks are as follows:

From Fabric.api import Run
def host_type ():
 run (' Uname-s ')

def hello (name= "World"):
 print ("Hello% s! "% name"

def Composite (name= "World"):
 Hello (name)
 Host_type ()

The commands provided by fabric

We've seen the functions in the Fabric.api module run , which function to execute commands on a remote host. The local function is also provided in Fabric.api to execute the native (fabric host) command.

As follows:

From Fabric.api import local
def lslocal (): Local
 (' ls ')

Similar to remote commands and local commands, fabric also distinguishes between a remote directory and a local directory. Fabric provides a remote and local directory operation for both CD and LCD. This is easy to understand if you have used the command line FTP.

Let's look at an example:

def filepath ():
 remote_dir = '/opt/xxx ' with
 CD (remote_dir):
  run ("Touch README")

The function of the above code is to go to the remote/opt/xxx directory and create a readme file.

Fabric also provides a lot of commands, such as file operations.

Managing Server connections

In the previous example, you would need to specify the server in the Fab command-line argument. It is cumbersome to manage a large number of servers. Fabric provides a dictionary of environment variables, env, which contains the hosts dictionary entry, which defines the server you want to connect to.

As follows:

From FABRIC.API import env, run

env.hosts = [' host1 ', ' Host2 ']
def mytask ():
 run (' ls/var/www ')

You can also specify the host list for each task individually to perform the task:

From FABRIC.API import env, run

def set_hosts ():
 env.hosts = [' host1 ', ' Host2 ']

def mytask ():
 Run (' Ls/var/www ')

When you do so fab set_hosts mytask , you can set_hosts perform tasks for the two specified in host mytask . If you are too lazy to write a function, specify the same on the fab command line:

Fab mytask:hosts= "Host1;host2"

For easier execution of the batch task, the fabric also defines role and is interested in reading its official documentation.

Manage SSH passwords, users, ports

Although it is more recommended to use SSH public key authentication, fabric still provides a mechanism for managing passwords. Fabric provides a two-layer password.

If your server has the same password, you can set the default password in, and env.password if the server password is different, you can set a env.passwords separate SSH password for each server by setting up (Host,password) pairs in.

The above host string takes this format: Username@hostname:port. Therefore, the SSH user is specified at the same time as the SSH password is specified. As with passwords, you can also env.user specify a default user in. If none is specified, fab you will be prompted for the password when executing the command.

With fabric, you can manage a series of host SSH connections (including host name, user, password), define a series of task functions, and then flexibly specify what tasks to perform on which host. This is very useful for scenarios where you need to manage a large number of host, such as operational dimensions, private cloud management, application automation deployment, and so on.

Summarize

This article is just a starter document, far from being a powerful fabric. In fact, Fabric also includes a large number of functions, such as role definition, remote interaction and exception handling, concurrent execution, file manipulation, and so on, and not only on the command line, you can invoke fabric in your application.

The above is the entire content of this article, I hope the content of this article can arouse your interest in fabric, and in your practical application to solve the problem. If you have any questions, you can exchange messages.

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.