author: headsen chen
date: 2018-08-12 23:13:16
1,安装
yum -y install epel-release
yum -y install fabric
2,指定密码的使用:
fab -p 123456 -H 192.168.10.10 -f f1.py w
#cat f1.py
#!/usr/bin/env python
from fabric.api import run
def w():
run('w')
确定:当指定的密码不正确的时候,会一直让你尝试输入密码
3,通过定义的密码文件来执行fab命令
[root@localhost mnt]# vim f2.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from fabric.api import *
env.hosts = ['192.168.10.104','192.168.10.105','192.168.10.101']
env.port = ''
env.user = 'root'
env.password = '' def a():
with cd('/tmp'):
run('touch a{1..10}')
run('ls /tmp')
def b():
run('uptime')
@task
def go():
a()
b()
[root@localhost mnt]# fab -f f2.py go
[192.168.10.104] Executing task 'go'
[192.168.10.104] run: touch a{1..10}
[192.168.10.104] run: ls /tmp
[192.168.10.104] out: a1 a10 a2 a3 a4 a5 a6 a7 a8 a9 yum.log
[192.168.10.104] out: [192.168.10.104] run: uptime
[192.168.10.104] out: 05:21:33 up 57 min, 3 users, load average: 0.00, 0.00, 0.00
[192.168.10.104] out: [192.168.10.105]
...
[192.168.10.101]
...
Done.
Disconnecting from 192.168.10.101... done.
Disconnecting from 192.168.10.104... done.
Disconnecting from 192.168.10.105... done.
4,指定秘钥对连接(将该密钥对的公钥放到对方的机器的 /root/.ssh/id_pub_rsa文件中去)
[root@localhost mnt]# cat bak-com.py
#!/usr/bin/env python
from fabric.api import * def w():
run("w")
[root@localhost mnt]# fab -i /root/.ssh/id_rsa -H 192.168.8.222 -f bak-com.py w
[192.168.8.222] Executing task 'w'
[192.168.8.222] run: w
[192.168.8.222] out: :: up days, :, users, load average: 0.16, 0.15, 0.10
[192.168.8.222] out: USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
[192.168.8.222] out: root pts/ 192.168.20.29 : : .28s .04s -bash
[192.168.8.222] out: root pts/ 192.168.20.29 : .00s .01s .01s w
[192.168.8.222] out:
Done.
Disconnecting from 192.168.8.222... done.
4,多个客户端的不同端口和不同密码的模板
[root@localhost mnt]# cat f4.py
#!/usr/bin/env python
# coding:utf-8
from fabric.api import *
from fabric.context_managers import *
from fabric.contrib.console import * env.hosts=['root@192.168.10.104:22','root@192.168.10.105:22','root@192.168.10.101:22']
# -----> 这一步当都为root和22端口时可以简写为:env.hosts=['192.168.10.104','192.168.10.105','192.168.10.101']
env.user = 'root'
env.passwords = {
'root@192.168.10.104:22':'', # ------->注意:这里的为固定格式,22端口指定不可少,否则报错
'root@192.168.10.105:22':'',
'root@192.168.10.101:22':'jack123',
}
def a():
with cd('/tmp'):
run('touch a{1..10}')
def b():
run('uptime')
@task
def c():
a()
b()
5,利用fabric来传送本地文件到客户端上去
[root@localhost mnt]# vim f6.py
#!/usr/bin/env python
# coding:utf-8
from fabric.api import *
from fabric.context_managers import *
from fabric.contrib.console import *
env.hosts=['192.168.10.104','192.168.10.105','192.168.10.101']
env.user = 'root'
env.passwords = {
'root@192.168.10.104:22':'',
'root@192.168.10.105:22':'',
'root@192.168.10.101:22':'jack123',
}
def a():
put('/tmp/jack123','/tmp/') # --------> 此时在客户端上生成 jack123的文件,如果要在客户度上改名:put('/tmp/jack123','/tmp/jack')
[root@localhost mnt]# fab -f f6.py a
[192.168.10.104] Executing task 'a'
[192.168.10.104] put: /tmp/jack123 -> /tmp/jack123
[192.168.10.105] Executing task 'a'
[192.168.10.105] put: /tmp/jack123 -> /tmp/jack123
[192.168.10.101] Executing task 'a'
[192.168.10.101] put: /tmp/jack123 -> /tmp/jack123 Done.
Disconnecting from 192.168.10.101... done.
Disconnecting from 192.168.10.104... done.
Disconnecting from 192.168.10.105... done.
6,从客户端上拉取文件到本地
[root@localhost mnt]# cat f7.py
#!/usr/bin/env python
# coding:utf-8
from fabric.api import *
from fabric.context_managers import *
from fabric.contrib.console import *
env.host=['192.168.10.104']
env.user = 'root'
env.password = 'jack123'
def a():
get('/tmp/tom','/opt/')
[root@localhost mnt]# fab -f f7.py a
[192.168.10.104] Executing task 'a'
[192.168.10.104] download: /opt/tom <- /tmp/tom
Disconnecting from 192.168.10.104... done.
7,task装饰器的作用
被标识的函数为fab可调用的,程序内的其他函数不可通过fab -f x.py go 调用,当整个程序内都没有@task时,则任何函数都可以被
fab命令调用
实例:
#!/usr/bin/env python
from fabric.api import *
def lsfab():
with lcd('/mnt/'):
local('ls')
def host_name():
local('uptime')
@task
def go():
lsfab()
host_name()
此时不可以通过:fab -f f1.py lsfab 此时不成功。当没有@task时,这样调用可以
8,run_once
标识的函数只会执行一次,只会在第一台主机执行,后面的主机不再执行其下的函数,不受多台主机影响。
实例:
[root@localhost mnt]# cat f3.py
#!/usr/bin/env python
# coding:utf-8
from fabric.api import * env.hosts=['192.168.13.128','192.168.13.130']
env.user='root'
env.password=''
def a():
with cd('/tmp'):
run('ls')
def b():
run('uptime')
@runs_once
def c():
a()
b()
[root@localhost mnt]# fab -f f3.py c # 此时在13.130机器上就不执行了。
[root@192.168.13.128:10000] Executing task 'c'
[root@192.168.13.128:10000] run: ls
[root@192.168.13.128:10000] out: yum.log [root@192.168.13.128:10000] run: uptime
[root@192.168.13.128:10000] out: 01:59:19 up 2:21, 3 users, load average: 0.00, 0.00, 0.00 Done.
Disconnecting from 192.168.13.128:10000... done.
[root@localhost mnt]#
9,fab的本地执行和远程执行
[root@localhost 7]# cat f2.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from fabric.api import *
env.user='root'
env.hosts=['192.168.13.128','192.168.13.129']
env.password='' @runs_once
def local_task () : #本地任务函数
local("uname -a") def remote_task():
with cd ("/home") : # “with”的作用是让后面的表达式的语句继承当前状态,实现
run ("ls -1") # “cd /data/logs && Is -1"的效果
10,交互式fab使用方法(执行命令时输入参数)
[root@localhost 7]# cat f3.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from fabric.api import *
env.user='root'
env.hosts=['192.168.13.128','192.168.13.129']
env.password='' @runs_once #主机遍历过程中,只有第一台触发此函数
def input_raw():
return prompt("please input directory name:",default="/home") def worktask(dirname):
run("ls -l "+dirname)
@task #限定只有go函数对fab命令可见
def go():
getdirname = input_raw()
worktask(getdirname)
[root@localhost mnt]# fab -f f8.py go
[192.168.10.104] Executing task 'go'
please input directory name: [/home] /tmp
[192.168.10.104] run: ls -l /tmp
[192.168.10.104] out: total 12
[192.168.10.104] out: -rw-r--r-- 1 root root 0 Aug 13 05:45 a1
... [192.168.10.105] Executing task 'go'
[192.168.10.105] run: ls -l /tmp
[192.168.10.105] out: total 8
[192.168.10.105] out: -rw-r--r-- 1 root root 0 Aug 13 05:45 a1
...
[192.168.10.105] out: Done.
Disconnecting from 192.168.10.104... done.
Disconnecting from 192.168.10.105... done.