Python uses the Fabric module for automated O & M, and pythonfabric

Source: Internet
Author: User
Tags python list

Python uses the Fabric module for automated O & M, and pythonfabric

 

Overview: Fabric is an SSH command line tool based on Python. It simplifies SSH Application Deployment and system management tasks. It provides basic system operation components, it can implement local or remote shell commands, including command execution, file upload, download, and complete execution log output. Fabric is encapsulated at a higher layer based on Paramiko, which makes the operation easier.

1. install Fabric by using pip install Fabric. To install fabric, you must use the paramiko package and Crypto package to verify whether the package is successfully installed. The following figure shows the command for successfully Installing Fabric.

  

fab [options] <command>[:arg1,arg2=val2,host=foo,hosts='h1;h2',...] ...

2. Common Parameters

-L # display the defined task function name-f # specify the fab entry file. The default entry file name is fabfile. py-f # specify the gateway (transit) device, such as the bastion host environment. Enter the bastion host IP address.-H # specify the target host and use multiple hosts ', '-p # password of the Remote Account. By default, the root account-P # is used to run multi-host tasks in Asynchronous Parallel Mode during fab execution, the default value is "Serial run-R # specify the role (role), and use the role name to differentiate devices in different service groups-t # set the device connection timeout (seconds) -T # Set the timeout time (in seconds) for remote host command execution-w # When the command execution fails, a warning is issued instead of the default task abort.

 

Example

Compile a fabfile. py file

Vim fabfile. py

#! /Usr/bin/env python #-*-coding: UTF-8-*-from fabric. api import run # defines a task function. The run method is used to remotely execute the 'uname-S' command def host_type (): run ('uname-S ')

 

Fab command operation:

 
Fab-H localhost host_type # result: [localhost] Executing task 'host _ type' [localhost] run: uname-s [localhost] Login password for 'root ': [localhost] out: Linux [localhost] out: Done. disconnecting from localhost... done.

 

The above instance can also be written as a line of code:

Fab-p 123456-H localhost -- 'uname-S' # -- add a space
Compiling fabfile

The fab command is used in combination with the fabfile. py file (other files are referenced by the-f filename parameter. Some command line parameters of fab can also be replaced by corresponding methods.

For example:

Fab-H 192.168.1.21, 192.168.1.22 ...... # In fabfile. env. hosts to implement it. You do not need to write it in the command line # You can write it in fabfile as follows: env. hosts = ['2017. 168.1.21 ', '2017. 168.1.22 ']

 

Env object of fabfile

The env object defines the global settings of fabfile, as shown in the preceding example. The following describes the attributes:

Env. hosts # defines the target host, which can be expressed by IP address or host name and defined in python list. Such as env. hosts = ['2017. 168.1.21 ', '2017. 168.1.22 '] env. exclude_hosts # exclude the specified host, such as env. exclude_hosts = ['2014. 192. 168.1.21 '] env. user # define the user name, such as env. user = 'root' env. port # defines the port. The default value is 22, for example, env. port = '22' env. password # define the password, such as env. password = '000000' env. passwords # define multiple passwords. Different hosts have different passwords, such as env. passwords = {'root @ 192.168.1.21: 22 ': '000000', 'root @ 192.168.1.22: 22': '000000'} env. gateway # defines the IP address of the gateway (transit or bastion host), such as env. gateway = '2017. 168.1.23env.roledefs # defines a role group. For example, the web-based db Group hosts are differentiated by env. roledefs = {'webserver': ['2017. 168.1.21 ', '2017. 168.1.22 '], 'dbserver': ['2017. 168.1.25 ', '2017. 168.1.26 ']} env. deploy_release_dir # customize global variables in the format of env. + 'variable name', such as env. age, env. sex, etc.

 

Example of how to use env. roledefs:

Env. roledefs = {'webserver': ['2017. 168.1.21 ', '2017. 168.1.22 '], 'dbserver': ['2017. 168.1.25 ', '2017. 168.1.26 ']} # Use the python modifier to reference A group, for example, @ roles ('webserver') def webtask (): run ('/usr/local/nginx/sbin/nginx') @ roles ('webserver', 'dbserver') def publictask (): run ('uptime ')

 

Common Fabric APIs

At the top of the simple example, the api function run is used. Next, we will list several common APIs.

Local # execute local commands, such as local ('uname-S') LCD # Switch local directories, such as LCD ('/home ') cd # Switch to the remote directory run # run the remote command sudo # run the remote command in sudo mode, such as sudo ('/etc/init. d/httpd start') put # remote host imported from the last local file, such as put ('/home/user.info', '/data/user.info ') get # download files from a remote host to a local machine, for example, get ('/data/user.info', '/home/user.info') prompt # obtain user input information, such: prompt ('Please input user password: ') confirm # obtain the prompt for confirmation, for example, confirm ('test failed, Continue [Y/N]? ') Reboot # restart the remote host, for example, reboot () @ task # function modifier. The function identified is callable by fab and is not visible to fab, pure business logic @ runs_once # function modifier. The identified function is executed only once and is not affected by multiple hosts.

 

Fabric Application Example 1. View local and remote host information

In this example, the local method is called to execute local commands. The @ runs_once modifier is added to ensure that the task function is executed only once and the run method is called to execute remote commands.

#! /Usr/bin/env python #-*-encoding: UTF-8-*-from fabric. api import * env. user = 'root' env. hosts = ['2017. 168.1.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 ('/var/logs'): # The role of with is to let the subsequent expression statements inherit the current state. Implementation: cd/var/logs & ls-l effect run ('LS-l ')Simple1.py

Run:

fab -f simple1.py local_taskfab -f simple1.py remote_task
2. Dynamically Retrieve the remote directory list

In this example, call the @ task modifier to mark the entry function go () to be visible to the outside, use the @ runs_once modifier to receive user input, and call the worktask () function to execute remote commands.

#! /Usr/bin/env python #-*-encoding: UTF-8-*-from fabric. api import * env. user = 'root' env. hosts = ['2017. 168.1.22 '] env. password = '000000' @ runs_once # during host traversal, only the first host triggers this function def input_raw (): return prompt ('Please input directoryname :', default = '/root') def worktask (dirname): run ('LS-l' + dirname) @ task # Only go functions are visible to fab commands, other function fab commands without @ task flag cannot use def go (): getdirname = input_raw () worktask (getdirname)Simple2.py

Run:

fab -f simple2.py go

Test the above results and find that:

1. if the default value is set, the default value is taken as the standard. If the default value is not set, dirname is null, and ls-l is the home directory of the user you log on to. For example, root is/root.

2. For the content written under the go function, the number of hosts that will be cyclically and exactly times will be measured. The host is used as the loop.

3. This script is used to list the same directory for all hosts. For different hosts, you can simply change the directory:

def worktask(dirname):        run('ls -l '+dirname) @taskdef go():        getdirname=raw_input("please input directory:")        worktask(getdirname)

 

3. File packaging and upload verification #! /Usr/bin/env pythonfrom fabric. api import * from fabric. colors import * env. hosts = ['2017. 168.56.30 '] env. user = 'root' env. passwords = {'root @ 192.168.56.30: 22': 'rooter '} @ runs_once @ taskdef tarfile (): print yellow ('tar file... ') # Use the with LCD command. Otherwise, you need to write the full path. The LCD directly does not use the with LCD ('/var/log'): local ('tar czf messages.tar.gz messages ') @ taskdef putfile (): print blue ('put file... ') run ('mkdir-p/tmp/log') with cd ('/tmp/ Log'): # When warn_only encounters an exception, continue to execute with settings (warn_only = True): result = put ('/var/log/messages.tar.gz ', '/tmp/log') if result. failed and not confirm ('put file filed, Continue [Y/N]? '): Abort ('aborting file put task! ') @ Taskdef checkfile (): print red ('check file... ') with settings (warn_only = True): # The local command must configure capture = True to obtain the returned value lmd5 = local ('md5sum/var/log/messages.tar.gz ', capture = True ). split ('') [0] rmd5 = run ('md5sum/tmp/log/messages.tar.gz '). split ('') [0] if lmd5 = rmd5: print 'OK' else: print 'error' @ task def go (): tarfile () putfile () checkfile ()Simple3.py

 

The following is the running result, which has the color difference:

[192.168.56.30] Executing task 'go'tar file ...[localhost] local: tar czf messages.tar.gz messagesput file ...[192.168.56.30] run: mkdir -p /tmp/log[192.168.56.30] put: /var/log/messages.tar.gz -> /tmp/log/messages.tar.gzcheck file ...[localhost] local: md5sum /var/log/messages.tar.gz[192.168.56.30] run: md5sum /tmp/log/messages.tar.gz[192.168.56.30] out: 958b813fd7bdaa61cc206aa1012d8129  /tmp/log/messages.tar.gz[192.168.56.30] out:  ok Done.Disconnecting from 192.168.56.30... done

  

4. File Upload and execution in Gateway Mode

This example defines the env. gateway mode, which is commonly known as the transit and bastion host environment. Upload and execute files on other hosts through the gateway.

#! /Usr/bin/env python #-*-encoding: UTF-8-*-from fabric. api import * from fabric. context_managers import * from fabric. contrib. console import confirmenv. user = 'root' env. gateway = '2017. 168.1.23 '# defines the bastion host IP address as the file upload and execution intermediate settings env. hosts = ['2017. 168.1.21 ', '2017. 168.1.22 '] env. passwords = {'root @ 192.168.1.21: 22 ': '000000', 'root @ 192.168.1.22: 22': 'abcdef', 'root @ 192.168.1.23: 22': '123abc ', # bastion host account information} lpackpath = '/ho Me/install/lnmp.tar.gz '# local installation package path rpackpath ='/tmp/install' # Remote Installation Package path @ taskdef put_task (): # run ('mkdir-p/tmp/install') # by default, Fabric stops executing Subsequent commands when the command fails. Sometimes, we can ignore failed commands for execution. For example, run ('rm/tmp/abc') may fail if the file does not exist, at this time, you can use with settings (warn_only = True): To execute the command, so that Fabric will only generate a warning message without interrupting the execution. With settings (warn_only = True): result = put (lpackpath, rpackpath) # upload if result. failed and not confirm ('put file failed, Continue [Y/N]? '): Abort ('aborting file put task! ') @ Taskdef run_task (): # install with cd ('/tmp/install'): run ('tar-zxvf lnmp.tar.gz ') with cd ('lnmp /'): # Use with to inherit the/tmp/install directory location status run ('. /centos. sh ') @ taskdef go (): # upload and install the combined command put_task () run_task () simple3.pysimple3. pySimple4.py

Run:

# Uploading a file fab simple3.py put_task # executing the file fab simple3.py run_task # uploading and executing fab simple3.py go

 

---------------------------------------------------------------

References:

Liu tiens automated Python O & M technology and best practices

  

 

  

 

  

  

  

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.