fab的常用参数:
-l : 显示定义好的任务函数列表
-f : 指定fab入口文件,默认入口文件是fabfile.py
-g : 指定网关
-H:指定目标主机,多个主机用,隔开。
-P : 以异步并行方式运行多主机任务,默认为串行。
-u: 指定主机的用户名,
-p:指定用户的密码。
fab -p 密码 -H 主机1,主机2 –‘命令’
实验:
1:单机操作
fab -u root -p password -H “ip” – “uname -a”
在远端服务器上执行命令 uname -a
2:多机串行
fab -u root -p password -H “ip1,ip2” – “uname -a”
2:多机并行:
fab -u root -p password -H “ip1,ip2” -P – “uname -a && 命令2”
执行是并行输出是串行。
测试代码:
#coding:utf8
from fabric.api import *
env.hosts = ["ip"]
env.user = "root"
env.password = "password"
env.port = 22
@task
def show():
run("hostname")
run("netstat -anptu | grep 22")
run("ls /root/")
@task
def catmem():
run("free -m")
@task
def run_all():
execute(show)
execute(catmem)
if __name__ == "__main__":
execute(run_all)
编写fabfile.py的脚本文件。
全局属性的设定:
env对象的作用是定义fabfile的全局设定,支持多个属性,包括目标主机,用户,密码,角色等等。
1 env.hosts:定义多个目标主机,用IP或者主机名的列表。
格式:env.hosts=[“”,”“]
2 env.user 定义用户名
格式: env.user = “root”
3 env.port 定义端口
格式: env.port = 22
4 env.password定义密码
env.password = “”
5 env.passwords: 定义多台主机的用户名,ip,port,password
格式 env.passwords = {
“root@ip”:”pw”,
“root@ip”:”pw”
}
6 env.gateway定义网关
格式:env.gateway = “ip”
7 env.roledefs: 定义角色分组
env.roledefs = {
“webserver”:[“ip”,”ip”],
“dbserver”:[“ip”,”ip”]
}
fab -f test1.py -l 显示任务函数
fab -f test1.py show 任务2 任务3 执行任务函数
python test1.py 也可以执行 if __name__=”__main__”:
测试代码:
#coding:utf8
from fabric.api import *
env.user = "root"
env.hosts = ["",""]
env.passwords = {
"root@ip":"pw",
"root@ip","pw"
}
env.roledefs = {
"webserver":["ip"],
"dbserver",["ip"]
}
@task
def show():
run("hostname")
@task
@roles("webserver")
def install_httpd():
print("install webserver")
@task
@roles("dbserver")
def install_mysql():
print("install mysql")
@task
def run_all():# 可能会调用两次?
execute(show)
execute(install_httpd)
execute(install_mysql)
if __name__ == "__main__":# 可能会调用两次
execute(run_all)
本地与远程运维常用的api
1,local 执行本地命令
格式: local(本地命令)
2,lcd 切换到本地目录
格式:lcd(本地目录)
3,cd 切换远程目录
格式:cd(远程目录)
4,run 执行远程命令
格式: run(远程命令)
5,sudo sudo的方式执行远端命令
格式:sudo(远程命令)
6,put:上传本地文件到远程主机
格式:put(本地文件,远程文件)
7,get:从远程主机下载文件到本地
格式:get(远程文件,本地文件)
8,prompt获取用户的输入信息
格式:prompt(“please input user password:”)
9,confirm:获取提示信息确认
格式:confirm(”continue[y/n]?”)
10,reboot 重启远程主机
格式:reboot()
11 ,@task 标注过的fab命令可见
12,@runs_once: 表示函数只会执行一次,不受多台主机的影响。
输出颜色
from fabric.colors import *
yellow(“hello world”)
blue(“hello world”)
red(“hello world”)
green(“hello world”)
cyan(“hello world”)
magenta(“hello world”)
white(“hello world”)
测试代码:
#coding:utf8
from fabric.contrib.console import confirm
from fabric.api import *
from fabric.colors import *
env.hosts = ["ip"]
env.user = "root"
env.password = "password"
env.port = 22
@task
def local_cmd():
local("ls -la")
with lcd("test"):
local("ls -la")
@task
def remote_cmd():
#run("free -m")
#sudo("cat /etc/passwd")
#with cd("/etc"):
# run("ls -la")
#put("test1.py","/root/test_put.py")
#run("ls -la /root/")
#get("/root/file.py","file.py")
#local("ls -la")
content1 = prompt("请输入")
print(content1)
content2 = confirm("请确认[Y/N]:")
print(content2)
@task
def show_color():
print(white("hello world"))
print(cyan("hello world"))
@task
def reboot_sys():
reboot()
文件打包上传校验下载:
基本操作:
1,打包:tar -czf xxx.tar.gz xxx
2,解包:tar -zxf xxx.tar.gz -C xxx
3,上传: put(本地文件,远程文件)
4,下载:get(远程文件,本地文件)
5,异常处理:
with settings(warn_only=Ture):
result = put(本地文件,远程文件)
if result.failed and not confirm(“put file failed,Continue[Y/N]?”):
abort(“Aborting file put task”)
6,校验:
with settings(warn_only=Ture):
local_md5 = local(“md5sum xxx.tar.gz”,capture=Ture).split(” “)[0]
remote_md5 = run(“md5sum xxx.tar.gz”).split(” “)[0]
if local_md5 == remote_md5:
print(“local file == remote file”)
else:
print(“local file != remote file”
测试代码:
#coding:utf8
from fabric.colors import *
from fabric.api import *
from fabric.contrib.console import confirm
env.hosts = ["ip"]
env.user = "root"
env.passwords = {
"root@ip":"password"
}
@task
def upload_file():
with settings(warn_only=True):
local("tar -czf test1.tar.gz test1.py")
result = put("test1.tar.gz","/root/test1.tar.gz")
if result.failed and not confirm("continue[Y/N]?"):
abort("put file failed")
with settings(warn_only=True):
local_file = local("md5sum test1.tar.gz",capture=True).split(" ")[0]
remote_file = run("md5sum /root/test1.tar.gz").split(" ")[0]
if local_file == remote_file:
print(green("local == remote"))
else:
print(red("local != remote"))
run("mkdir /root/test")
run("tar -zxf /root/test1.tar.gz -C /root/test/")
@task
def download_file():
with settings(warn_only=True):
get("/root/filename","")
local("mkdir /root/test0")
local("tar -zxf filename -C /root/test0")
多主机批量并行运维
基本操作
1.指定多台主机IP或者域名
env.housts = [ip,ip]
2.指定多台主机的密码
env.passwords = {
“root@ip”:”pw”,
“”:””
}
3.指定主机的角色分组
env.roledefs = {
“xxx1”:[ip,ip,],
“xxx2”:[ip,ip]
}
4.并行装饰器:@parallel
5,角色装饰器:@roles(角色)
6.主机装饰器:@hosts(主机1,主机2)
安装FTP的服务
yum clean all 清除缓存
yum repolist all 刷新仓库
yum -y install vsftpd 安装ftp服务
systemctl start vsftd.service 启动服务
systemctl enable vsftd.service 设置开机自动启动
测试代码:
#coding:utf8
from fabric.api import *
env.user = "root"
host1 = ""
host2 = ""
env.hosts = [host1,host2]
env.passwords = {
"root@ip1":"pw1",
"root@ip2":"pw2"
}
@task
@parallel
@hosts(host1,host2)
def install_ftp():
run("yum clean all")
run("yum repolist all")
run("yum -y install vsftpd")
run("systemctl start vsftpd.service")
run("systemctl enable vsftpd.service")
run("systemctl status vsftpd.service")
自动化创建tornado项目目录:
#coding:utf8
import os
os.system("pip install -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com fabric")
os.system("pip install -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com tornado")
from fabric.api import *
from fabric.colors import *
def index_py_s():
content = """#coding:utf8
from app import app
if __name__ == "__main__":
app()
"""
return content
def server_py_s(port):
content = """#coding:utf8
import tornado.web
import tornado.ioloop
import tornado.options
import tornado.httpserver
from tornado.options import options,define
from configs import configs
from urls import urls
define("port",default = %d ,type = int)
class CustomApplication(tornado.web.Application):
def __init__(self, configs, urls):
settings = configs
handlers = urls
super(CustomApplication, self).__init__(handlers = handlers,**settings)
def create_app():
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(CustomApplication(configs,urls))
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
app = create_app
""" % int(port)
return content
def urls_py_s():
content = """#coding:utf8
from home.views import IndexHandler as home_index
urls = [
(r"/",home_index)
]
"""
return content
def configs_py_s():
import uuid
content = """#coding:utf8
import os
root = os.path.dirname(__file__)
configs = dict(
template_path = os.path.join(root,"templates"),
static_path = os.path.join(root, "static"),
debug = True,
xsrf_cookies = True,
cookie_secret = "%s"
)
""" % str(uuid.uuid4().hex)
return content
def views_py_s():
content = """#coding:utf8
import tornado.web
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html",title = "hello world !")
"""
return content
def index_html_s():
content="""
{{title}}
"""
return content
@task
def mk_web():
dir_name = prompt(green("please input project name"))
local("mkdir %s" % dir_name)
with lcd(dir_name):
local("touch index.py")
local("echo '%s' >> index.py" % index_py_s())
local("mkdir app")
with lcd("app"):
local("touch __init__.py")
port = prompt(cyan("please input project port"))
local("echo '%s' >> __init__.py" % server_py_s(port))
local("touch urls.py")
local("echo '%s' >> urls.py" % urls_py_s())
local("touch configs.py")
local("echo '%s' >> configs.py " % configs_py_s())
local("mkdir home")
with lcd("home"):
local("touch __init__.py")
local("touch views.py")
local("echo '%s' >> views.py" % views_py_s())
local("mkdir templates")
with lcd("templates"):
local("touch index.html")
local("echo '%s' >> index.html" % index_html_s())
local("mkdir static")
local("python index.py")
if __name__ == "__main__":
execute(mk_web)