O & M manager Fabric

Source: Internet
Author: User
Tags python list

Fabric is an SSH command line tool based on python2.5 or later. It simplifies Application Deployment and system management tasks of SSH, and provides basic operating components of the system, it can implement local or remote shell commands, including command execution, file upload, download, and complete execution log output.

Fabric Installation

Fabric supports Pip, easy_install, or source code installation, which helps solve the package dependency problem. (select Pip or ease_install based on the user environment)

PIP install Fabric

Easy_install Fabric


Source code installation is not introduced.

Verify the installation result. If the import module does not prompt an exception, the installation is successful:

[Email protected] _ S6 :~ # Python

Python 2.7.5 + (default, SEP 19 2013, 13:48:49)

[GCC 4.8.1] On linux2

Type "help", "Copyright", "Credits" or "License" for more information.

>>> Import Fabric

>>>

The official website provides a simple getting started example.:

[Email protected] _ S6:/home/chart7/test/fabric # Cat farbic. py #! /Usr/bin/ENV Python #-*-coding: UTF-8-*-from fabric. API import rundef host_type (): # define a task function and remotely execute the 'uname-S' Command run ('uname-S') using the run method ')

If the running result is as follows:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/54/15/wKioL1R32_KhwHwaAAK-ROg7uYQ972.jpg "Title =" 1.png" alt = "wKioL1R32_KhwHwaAAK-ROg7uYQ972.jpg"/> where the FAB command references the default file name fabfile. py. If a non-default file name is used, it must be specified by '-F', for example, Fab-H 192.168.1.23, 192.168.1.24-F host_type.py host_type, if no key authentication trust is configured for the administrator and target hosts, the system will prompt you to enter the logon password of the account corresponding to the target host.

I. Common Parameters of FAB

As the command entry of the fabric program, FAB provides a wide range of parameter calls. The command format is as follows:

Fab [Options] <command> [: arg1, arg2 = val2, host = Foo, hosts = 'h1; H2 ',...]

The following lists several common parameters. For more parameters, see Fab-help.

  • -L: display the defined task function name;

  • -F: Specify the FAB entry file. The default entry file name is fabfile. py;

  • -G: Specify the gateway device, such as the bastion host environment. Enter the bastion host IP address;

  • -H: Specifies the target host. Multiple hosts are separated by commas;

  • -P: runs multiple host tasks in Asynchronous Parallel mode. The default value is serial;

  • -R: specifies the role to distinguish devices in different service groups with the role name;

  • -T: set the device connection timeout;

  • -T: Set the timeout time for remote host command execution;

  • -W: When the command execution fails, a warning is issued, rather than the default task termination.

Ii. Compiling fabfile

The Fab command is written in combination with the fabfile. PY (other file names must be referenced by-f filename). Some command line parameters can be replaced by appropriate methods to make them more flexible, such as "-H 192.168.1.23, 192.168.1.24 ", we can define Env. hosts, such as "Env. hosts = [192.168.1.23, 192.168.1.24] ". the body of the fabfile consists of multiple custom task functions. Different task functions implement different operation logics. The following describes in detail

3. Global attribute settings

The env object defines the global settings of fabfile and supports multiple attributes, including the target host, user, and password roles. The attributes are described as follows:

  • Env. Host, which defines the target host, can be expressed by IP address or host name and defined in the form of a python list, such as env. hosts = ['2017. 168.1.23, 192.168.1.24 '].

  • Env. exculde_hosts, exclude the specified host, such as env. exclude_hosts = ['192. 168.1.23 ']

  • Env. user, which defines the user name, such as env. User = "root"

  • Env. Port, which defines the target host port, for example, ENV. Port = '22'

  • Env. Password, which defines the password, for example, ENV. Password = '000000'

  • Env. the passwords function is the same as the password function. The difference lies in the application scenarios of different hosts with different passwords. Note that When configuring passwords, You need to configure user, host, port, and other information, such as Env. passwords = {'[email protected]: 22': '123 ',

    '[Email protected]: 22': '123 ',

    '[Email protected]: 23': '123 ',

    }

  • Env. Gateway, which defines the IP address of the Gateway (transit, Bastion host), for example, ENV. Gateway = '192. 168.1.1'

  • Env. roledefs: defines a role group. For example, the web group and the DB Group host are separated and defined as follows:

    Env. roledefs = {

    'Webservers': ['192. 168.1.21 ', '192. 168.1.22', '192. 168.1.23 '],

    'Dbservers': ['2014. 168.1.24 ', '2014. 168.1.25'],

    }

The Python modifier is used for reference. The task function below the role modifier is in its scope. The following is an example:

@roles(‘webservers‘)def webtask():    run(‘/etc/init.d/nginx start‘)@roles(‘dbservers‘):def dbtask():    run(‘/etc/init.d/mysql start‘)@roles(‘webservers‘,‘dbservers‘)def publictask():    run(‘uptime‘)def deploy():    execute(webtask)    execute(dbtask)    execute(publictask)

Execute the command Fab deploy to execute different task functions for different roles.

Common APIs

Fabric provides a simple but powerful set of fabric. API commands, which can be called to meet most application scenarios. Fabric supports the following common methods and instructions:

  • Local: execute local commands, such as local :( 'uname-S ');

  • LCD: Switch the local directory, such as LCD :( '/home ');

  • CD: Switch the remote directory, such as CD :( '/data/logs /');

  • Run: Run ('free-m ')

  • Sudo: execute remote commands in sudo mode, for example, sudo ('/etc/init. d/httpd start ');

  • Put: upload local files to a remote host, for example, put ('/home/user.info', '/data/user.info ');

  • Get: download files from the remote host to the local machine, for example, get ('/home/user.info', '/data/user.info ');

  • Prompt to obtain user input information, such as: prompt ('Please input user password :');

  • Confirm, for example, confirm ('test failed, continue [Y/n] ');

  • Reboot: restart the remote host, such as reboot ();

  • @ Task: the function modifier. The identifier function can be called by FAB. The non-flag is invisible to FAB and is purely business logic;

  • @ Runs_once, function modifier, identifier function will only be executed once, not affected by multiple hosts;

Example 1: View local and remote host information

In this example, call the local () method to execute the local command and add the "@ runs_once" modifier to ensure that the task function is executed only once. Call the run () method to execute a remote command,

#! /Usr/bin/ENV Python #-*-coding: UTF-8-*-from fabric. API import * Env. user = 'root' Env. hosts = ['2017. 168.1.43 ', '2017. 168.1.23 ', '2017. 168.1.24 '] Env. port = '22' Env. password = '000000' @ runs_once # view local system information. When multiple hosts run def local_task () only once: # local task Function Local ('uname-') def remote_task (): with Cd ('/data'): # With is used to make subsequent expression statements inherit the current state, run ('LS-l ')

Use the FAB command to call the local_task task function, as shown in.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/54/17/wKioL1R38guSh6BDAAGJEj-QZ_w798.jpg "Title =" 1.png" alt = "wKioL1R38guSh6BDAAGJEj-QZ_w798.jpg"/>

The results show [192.168.1.23] Executing task 'local _ task', but in fact it is not to execute the task on host 192.168.1.23, but to return the execution result of the local 'uname-a' of the fabric host.

Shows the execution result of calling the remtoe_task function.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/54/17/wKioL1R38lzBIxVoAALuZIpWH6A197.jpg "Title =" 1.png" alt = "wkiol1r38lzbixvoaaluzipwh6a197.jpg"/>

This article is from the "server" blog, please be sure to keep this source http://zhangfang2012.blog.51cto.com/6380212/1583941

O & M manager Fabric

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.