Salt-api调用模块saltapi.py
来源:互联网 发布:java初学者源代码 编辑:程序博客网 时间:2024/06/06 03:47
本模块用于调用Salt-api功能,内容如下
#!/usr/bin/python# -*- coding: utf-8 -*- import pycurlimport StringIO #登录salt-api,获取tokendef 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, fun, expr_form='', 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)) elif arg_num == 2: ch.setopt(ch.POSTFIELDS, "client=local&tgt=%s&expr_form=%s&fun=%s&arg=%s" %(target, expr_form, fun, arg)) else: pass ch.setopt(ch.HEADER,False) ch.perform() html = info.getvalue() info.close() ch.close() return html #测试时用的,做为模块使用时请注释下面两行api_login()print api_exec('*.61', 'test.ping')
0 0
- Salt-api调用模块saltapi.py
- Saltstack-Salt常用模块及API
- salt常用命令、模块、执行
- salt内置模块列表
- salt-api 安装配置
- salt-api安装配置
- salt api开发指南
- salt-api 安装使用
- Ansible常用模块API调用
- saltstack常用操作----salt-api 实现 通过http请求远程调用saltstack的步骤
- salt内置执行模块列表
- 模块和包(几个py文件的调用)
- salt-api安装、配置、使用
- salt-api安装、配置、使用
- Centos6.5配置salt-api
- Salt api安装及配置
- salt的python接口调用
- 二、Salt文件服务器与salt-cp以及cp模块
- iOS相关,过年回来电脑上的证书都失效了,求大神!
- xilinx千兆以太网与万兆以太网IP接口
- HBase shell的基本用法
- Ubuntu下挂载远程目录方法
- 1063. Set Similarity (25)
- Salt-api调用模块saltapi.py
- 再谈spring mvc中的root/child WebApplicationContext
- openwrt /etc/config/network
- UVA253 Cube painting
- ADB的学习
- 工厂模式
- moveTaskToBack Android应用退到后台,类似按下Home键
- web移动--<meta>标签整理
- Nginx的介绍和使用