Fabirc

Fabirc

简介

python+linux批量执行工具,基于python2.x

安装Fabirc

  • 确认python&pip环境&virtualenv
python -V
yum -y install python-pip
pip install --upgrade pip
pip install virtualenv
mkdir fab
cd fab
virtualenv venv
source venv/bin/activite
  • 安装Fabric
pip install fabric
fab --version    #版本检测

fabric常用参数

  • 常用参数
  --complete                        Print tab-completion candidates for given parse remainder.
  --hide=STRING                     Set default value of run()'s 'hide' kwarg.
  --no-dedupe                       Disable task deduplication.
  --prompt-for-login-password       Request an upfront SSH-auth password prompt.
  --prompt-for-passphrase           Request an upfront SSH key passphrase prompt.
  --prompt-for-sudo-password        Prompt user at start of session for the sudo.password
                                    config value.
  --write-pyc                       Enable creation of .pyc files.
  -c STRING, --collection=STRING    Specify collection name to load.
  -d, --debug                       Enable debug output.
  -D INT, --list-depth=INT          When listing tasks, only show the first INT levels.
  -e, --echo                        Echo executed commands before running.
  -f STRING, --config=STRING        Runtime configuration file to use.
  -F STRING, --list-format=STRING   Change the display format used when listing tasks. Should
                                    be one of: flat (default), nested, json.
  -h [STRING], --help[=STRING]      Show core or per-task help and exit.
  -H STRING, --hosts=STRING         Comma-separated host name(s) to execute tasks against.
  -i, --identity                    Path to runtime SSH identity (key) file. May be given
                                    multiple times.
  -l [STRING], --list[=STRING]      List available tasks, optionally limited to a namespace.
  -p, --pty                         Use a pty when executing shell commands.
  -r STRING, --search-root=STRING   Change root directory used for finding task modules.
  -S STRING, --ssh-config=STRING    Path to runtime SSH config file.
  -V, --version                     Show version and exit.
  -w, --warn-only                   Warn, instead of failing, when shell commands fail.

解释:

-l :显示指定的任务函数列表
-f : 指定fab文件位置
-g :指定网关
-H :指定目标主机
-P :异步方式执行任务,默认是串行
-u :指定用户名
-p :指定密码 

实例:

fab -u root -p 123 -H '192.168.56.11' -- 'hostname'    #单机
fab -u root -p 123 -H '192.168.56.11,192.168.56.9' -P -- 'hostname'  #多机异步执行

fabfile

  • 设定全局属性

    1. env:
env.hosts = ["192.168.1.1","192.168.1.2"]    #定义host,使用主机名或者IP
env.user = "root"   #定义用户
env.port = 22    #定义端口
env.password = "234"   #定义密码
env。passwords = {
    "root@192.168.1.1":"123456",
    "wanghui@192.168.1.2":"234455",
}
env.gateway = "10.70.18.11"
env.roledefs = {
    "web":["192.168.1.1","web1.add.bjcc.node1.net"],
    "db":["192.168.12.1","192.168.22.1"]
}
  • 小例子:
from fabric.api import *
env.hosts = ["192.168.56.11","192.168.56.9"]
env.user = "root"
env.password = "wang19910914hui@"
env.port = 22


def show():
    run("hostname")
    run("netstat --antup | grep 22")
    run("ls /root/")
    原文作者:wanghui
    原文地址: https://segmentfault.com/a/1190000014842735
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞