Turn: Use fabric to automate your tasks

Source: Internet
Author: User

Ext.: http://www.cnblogs.com/holbrook/archive/2012/03/05/2380398.html

What is fabric?

Fabric is a Python library that can perform bulk tasks on multiple hosts via SSH. You can write task scripts and then use SSH to run automatically on a large number of remote servers using fabric locally. These features are ideal for automated deployment of applications, or for performing system administration tasks.
Let's look at an example first. We know that under *nix, the uname command is to view the release version 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 execute the Host_type script on multiple hosts through the FAB command:
$ fab-h Localhost,linuxbox Host_type
[localhost] run:uname-s
[localhost] Out:darwin
[Linuxbox] Run:uname-s
[Linuxbox] Out:linux

You may be required to enter the system password during execution.

Installation

If you see this, you're starting to get interested in fabric. However, the above operation cannot be performed in your place because you have not installed the fabric yet. Installing fabric is simple, you can use PIP or Easy_install, or you can download the original code installation.

Task function

Well, installing fabric doesn't baffle you. Perhaps you have 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 usage of the function also applies to task functions. 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 parameters to the task function through the FAB 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 run function in the Fabric.api module before, and its function is to execute the command on the remote host. The local function is also available in the Fabric.api to execute the locally (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 remote directories and local directories. The remote and local directory operations provided by fabric are CD and LCD respectively. If you've ever used command-line FTP, it's easy to understand. 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.

The fabric also provides a number of commands, such as file manipulation, which can be referenced in the fabric's operations module.

Manage Server connections

In the previous example, you would need to specify the server in the Fab command-line parameters. It is cumbersome to manage a large number of servers. Fabric provides a dictionary of environment variables, env, which contains the hosts dictionary entry that defines the server that needs to be connected. 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 execute the Fab set_hosts mytask, you can perform mytask tasks for the two hosts specified in Set_hosts. If you are too lazy to write functions, the same is specified on the Fab command line:
Fab mytask:hosts= "Host1;host2"

To make it easier to perform bulk tasks, role is defined in fabric 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 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 Env.password, and if the server password is different, you can set the (Host,password) pair in Env.passwords and set a separate SSH password for each server.
The host string above takes this format: [email protected]:p ort. Therefore, the SSH password is specified while the SSH user is specified. As with the password, you can also specify a default user in Env.user. If none is specified, you are prompted for a password when the Fab command is executed.

Summary

With fabric, you can manage a series of host SSH connections (including host names, users, passwords), define a series of task functions, and then flexibly specify which hosts perform which tasks on which host. This is very useful for scenarios where you need to manage a host of hosts, such as operations, private cloud management, application automation deployment, and more.
This article is just a getting started document, far from showing the power of fabric. In fact, Fabric also includes a number of functions, such as role definition, remote interaction and exception handling, concurrent execution, file manipulation, and not just the command line, which can call fabric in your app.

Turn: Use fabric to automate your tasks

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.