How to use Fabric to automate your tasks and fabric automation tasks

Source: Internet
Author: User

How to use Fabric to automate your tasks and fabric automation tasks

First, let's look at an example. We know that under * NIX,unameCommand is to view the release of the system.

You can write such a Fabric script:

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

Save the preceding script as fabfile. py.fabThe command executes the host_type script on multiple hosts:

$ 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 and can be usedpipOreasy_installYou can also download and install the original code.

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)

You can usefabThe command line parameter of is the task function transfer parameter:

$ fab hello:name=HolbrookHello Holbrook!

An example of a combined task is as follows:

from fabric.api import rundef 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 seenrunFunction, which is used 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 localdef 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.

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, runenv.hosts = ['host1', 'host2']def mytask(): run('ls /var/www')

You can also specify the host list for each task:

from fabric.api import env, rundef set_hosts(): env.hosts = ['host1', 'host2']def mytask(): run('ls /var/www')

Runfab set_hosts mytask, You canset_hostsTwohostRunmytaskTask. If you are too lazy to write a functionfabThe command line also specifies the same:

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 passwordenv.passwordSet the default password. If the server password is different, you can alsoenv.passwords(Host, password) pair, 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. Same as the password, you can alsoenv.userSpecify a default user. If none of them are specified, runfabThe command prompts you to enter the password.

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.

Summary

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.

The above is all the content in this article. We hope that the content in this article will arouse your interest in Fabric and solve the problem in your practical application. If you have any questions, you can leave a message.

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.