Salt-api调用模块saltapi.py

本模块用于调用Salt-api功能,内容如下

#!/usr/bin/python
# -*- coding: utf-8 -*-
 
import pycurl
import StringIO
 
#登录salt-api,获取token
def api_login():
    global token
    url='https://192.168.90.62:8000/login'
    ch=pycurl.Curl()    #创建一个pycurl对象的方法
    ch.setopt(ch.URL, url)     #设置要访问的url
    info = StringIO.StringIO()     
    ch.setopt(ch.WRITEFUNCTION, info.write)
    ch.setopt(ch.POST, True)
    #如果是https就要开启这两行
    ch.setopt(ch.SSL_VERIFYPEER, 0)
    ch.setopt(ch.SSL_VERIFYHOST, 2)
    ch.setopt(ch.HTTPHEADER, ['Accept: application/x-yaml'])
    ch.setopt(ch.POSTFIELDS, 'username=saltapi&password=111111&eauth=pam')
    #要包头信息
    #ch.setopt(ch.HEADER, True)
    #不要包头信息
    ch.setopt(ch.HEADER,False)
    ch.perform()
    html = info.getvalue()
    #提取token
    token = html.split("\n")[-3].replace("\n", '')
    token = token.split(' ')[3]
    info.close()
    ch.close()
 
def api_exec(target, expr_form, fun, arg='', arg_num=0):
    global token
    url='https://192.168.90.62:8000/'
    ch=pycurl.Curl()
    ch.setopt(ch.URL, url)
    info = StringIO.StringIO()
    ch.setopt(ch.WRITEFUNCTION, info.write)
    ch.setopt(ch.POST, True)
    ch.setopt(ch.SSL_VERIFYPEER, 0)
    ch.setopt(ch.SSL_VERIFYHOST, 2)
    ch.setopt(ch.HTTPHEADER, ['Accept: application/json', "X-Auth-Token: %s" %(token)])
    if arg_num == 0:
        ch.setopt(ch.POSTFIELDS, "client=local&tgt=%s&fun=%s" %(target, fun))
    elif arg_num == 1:
        ch.setopt(ch.POSTFIELDS, "client=local&tgt=%s&fun=%s&arg=%s" %(target, fun, arg))    
    else arg_num == 2:
        ch.setopt(ch.POSTFIELDS, "client=local&tgt=%s&expr_form=%s&fun=%s&arg=%s" %(target, expr_form, fun, arg))
    ch.setopt(ch.HEADER,False)
    ch.perform()
    html = info.getvalue()
    info.close()
    ch.close()
    return html
 
#测试时用的,做为模块使用时请注释下面两行
api_login()
print api_exec('*.61', 'test.ping')


你可能感兴趣的:(python,自动化,saltstack,salt-api)