Use Fabric to automate your tasks.

Source: Internet
Author: User

Use Fabric to automate your tasks.
What is fabric?

Fabric is a Python library that can run tasks in batches on multiple hosts through SSH. You can write a task script and use Fabric locally to run it automatically on a large number of remote servers using SSH. These functions are ideal for automated application deployment or system management tasks.
Let's first look at an example. We know that under * NIX, The uname command is to view the release version of the system. You can write such a Fabric script:

from fabric.api import run
def host_type():
run('uname -s')

Save the above script as fabfile. py and run the host_type script on multiple hosts using 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 need to enter the system password during execution.

Install

If you see this, you are interested in Fabric. However, you cannot perform the preceding operations because you have not installed Fabric. Installing Fabric is simple. You can use pip or easy_install or download the original code to install Fabric.

Task Functions

Very good. It is not difficult to install Fabric. Maybe you have successfully executed the previous task. Now let's go deeper.

A task in Fabric is a python function. Let's call it a "task function ". Since it is a python function, some usage of the function is also applicable to task functions. For example, passing parameters, calling each other, and returning values. First, let's look at an example of passing parameters:

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

When executing a task, you can use the command line parameters of fab to pass parameters to the task function:
$ Fab hello: name = Holbrook
Hello Holbrook!

An example of a combined task is 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()
Fabric commands

We have seen the run function in the fabric. api module. Its function is to execute commands on a remote host. Fabric. api also provides the local function for executing local commands (the host where Fabric is located. As follows:

from fabric.api import local
def lslocal():
local('ls')

Similar to remote commands and local commands, Fabric also distinguishes between remote and local directories. Fabric provides cd and LCD operations for remote and local directories. If you have used command line ftp, this is easy to understand. Let's take an example:

def filepath():
remote_dir = '/opt/xxx'
with cd(remote_dir):
run("touch README")

The function of the above Code is to enter the remote/opt/xxx directory and create a README file.

Fabric also provides many commands, such as file operations. You can refer to the Fabric operations module.

Manage Server connections

In the preceding example, you must specify the server in the fab command line parameters. It is troublesome to manage a large number of servers. Fabric provides the environment variable dictionary env, which contains the hosts dictionary item and defines the server 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:

from fabric.api import env, run

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

def mytask():
run('ls /var/www')

In this way, when executing the fab set_hosts mytask, You can execute the mytask task for the two hosts specified in set_hosts. If you are too lazy to write a function, you can specify the same in the fab command line:
Fab mytask: hosts = "host1; host2"

To facilitate batch task execution, Fabric also defines Role. If you are interested, read its official documentation.

Manage SSH passwords, users, and ports

Although SSH public key authentication is recommended, Fabric provides a password management mechanism. Fabric provides two-tier passwords.
If your server has the same password. set the default password in password. If the server password is different, you can also set the default password in env. set a (host, password) pair in passwords, and set a separate ssh password for each server.
The preceding host string adopts this format: username @ hostname: port. Therefore, the ssh user is also specified when the ssh password is specified. Like the password, you can specify a default user in env. user. If none of them are specified, you will be prompted to enter the password when executing the fab command.

Summary

Fabric allows you to manage a series of host SSH connections (including host names, users, and passwords), define a series of task functions, and flexibly specify which hosts to execute. This is very useful in scenarios that require management of a large number of hosts, such as O & M, private cloud management, and automated application deployment.
This article is just an entry-level document, far from reflecting the power of Fabric. In fact, Fabric also includes a large number of functions, such as Role definition, remote interaction and exception handling, concurrent execution, and file operations, and is not limited to command line methods, fabric can be called in your application.
I hope this article will arouse your interest in Fabric and solve the problem in your practical application.

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.