系統批量運維管理器Fabric詳解

2021-09-20 16:28:55 字數 2610 閱讀 3500

1、fab常用的引數

fab作為fabric程式的命令列入口,提供了豐富的引數呼叫,命令格式如下:

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

2、全域性屬性設定

env物件的作用是定義fabfile的全域性設定,支援多個屬性,包括目標主機、使用者、密碼、角色等,各屬性說明如下:

env.passwords=

env.roledefs=

引用時使用python修飾符的形式進行,角色修飾符下面的任務函式為其作用域,下面來看乙個示例:

@roles('webservers')def webtask():

run('/etc/init.d/nginx start')

@roles('dbservers')def dbtask():

run('/etc/init.d/mysql start')

@roles('webservers','dbservers')

run('uptime')def deploy():

execute(webtask)

execute(dbtask)

execute(pubclitask)

在命令列執行#fab deploy就可以實現不同角色執行不同的任務函式了。

3、常用api

fabric提供了一組簡單但功能強大的fabric.api命令集,簡單地呼叫這些api就能完成大部應用場景需求,fabric支援常用的方法及說明如下:

4、示例1:檢視本地與遠端主機資訊

#!/usr/bin/env python

#-*- codeing:utf-8 -*-

from fabric.api import *

display local or remote host information

env.user = 'root'

env.hosts=['192.168.110.129']

env.password='111111'

@runs_once

def host_type():

run('uname -s')

def remote_task():

with cd('packets'):

run('ls -l')

5、示例2:動態獲取遠端目錄列表

#!/usr/bin/env python

#-*- codeing:utf-8 -*-

#dynamic obtain remote host directory list

env.user = 'root'

env.hosts=['192.168.110.129']

env.password='111111'

@runs_once

def input_raw():

return prompt('please input diectory name:',default='/root')

def worktask(dirname):

run('ls -l ' + dirname)

@task

def go():

getdirname = input_raw()

worktask(getdirname)

6、示例3:閘道器模式檔案上傳與執行

#!/usr/bin/env python

#-*- codeing:utf-8 -*-

#gateway mode put file and execute

from fabric.api import *

from fabric.context_managers import *

from fabric.contrib.console import confirm

env.user='root'

env.gateway='192.168.110.129'

env.hosts=['192.168.110.130','192.168.110.131']

env.passwords=

lpackpath="/home/install/lnmp0.0.tar.gz"

rpackpath='/tmp/install'

@task

def put_task():

run('mkdir -p /tmp/install')

with settings(warn_only=true):

result = put(lpackpath,rpackpath)

if result.failed and not confirm('put file failed,continue[y/n]?'):

abort('aborting file put task!')

@task

def run_task():

with cd('/tmp/install'):

run('tar -zxvf lnmp0.9.tar.gz')

with cd('lnmp0.9/'):

run('./centos.sh')

@task

def go():

put_task()

run_task()

Python之 系統批量運維管理器pexpect

pexpect作為python的乙個普通模組,可以通過pip安裝,也可以通過原始碼安裝 1 pip安裝 pip install pexpect easy install pexpect2 原始碼安裝wget cd pexpect python setup.py installpexpect包含的核心...

系統運維 「任務管理器」記憶體列的含義是什麼?

在 任務管理器 中,可以通過將列新增到 程序 選項卡上所顯示的資訊中,來監視計算機上正在執行的程序。這些列將顯示有關每個程序的資訊,如程序當前正在使用的 處理器 cpu 和記憶體資源的數量。1.通過右鍵單擊工作列,然後單擊 任務管理器 開啟 任務管理器 2.單擊 程序 選項卡。任務管理器將顯示當前正...

系統變數管理器

當前登陸使用者實體 using system namespace loginuserinfo private string loginname 登陸名 public string loginname set private string username 使用者名稱 public string us...