saltstack
- 在之前使用saltstack安装nginx后,我们继续编写密钥验证的例子,下面是publickey的代码masterApp.py
import codecs
import json
import commands
def publicKey(ipaddr):
with codecs.open('/tmp/{0}.txt'.format(ipaddr), 'w') as f:
f.write("{0} ansible_ssh_pass=123456".format(ipaddr))
resultBean = dict()
cmd = "ansible -i /tmp/{0}.txt all -m script -a '/srv/salt/_shell/installpub.sh'".format(ipaddr)
status, output = commands.getstatusoutput(cmd)
if status == 0:
resultBean['code'] = 0
resultBean['message'] = 'success'
resultBean['data'] = output
return json.dumps(resultBean)
import json
import requests
class SaltServer(object):
def __init__(self):
self.session = requests.session()
self.token = self.getToken()
def getToken(self):
url = "http://192.168.48.135:8000/login"
headers = {"Accept": "application/json"}
data = {
"username": "saltapi",
"password": "saltapi",
"eauth": "pam"
}
res = self.session.post(url=url, headers=headers, data=data)
text = res.text
result = json.loads(text)
token = result.get("return")[0].get("token")
return token
def runModules(self, minionid, fun, arg=None):
url = "http://192.168.48.135:8000"
data = {
"client": "local",
"tgt": minionid,
"fun": fun,
"arg": arg
}
resultBean = dict()
try:
res = self.session.post(url=url, data=data)
text = res.text
data = json.loads(text).get("return")
resultBean['code'] = 0
resultBean['message'] = "success"
resultBean['data'] = data
except Exception as e:
resultBean['code'] = 0
resultBean['message'] = "success"
resultBean['data'] = e
finally:
return resultBean
def runRunner(self, fun, **kwargs):
url = "http://192.168.48.135:8000"
data = {
"client": "runner",
"fun": fun,
}
data.update(kwargs)
resultBean = dict()
try:
res = self.session.post(url=url, data=data)
text = res.text
data = json.loads(text).get("return")
resultBean['code'] = 0
resultBean['message'] = "success"
resultBean['data'] = data
except Exception as e:
resultBean['code'] = 0
resultBean['message'] = "success"
resultBean['data'] = e
finally:
return resultBean
result1 = saltServer.runRunner('masterApp.publicKey', ipaddr='192.168.48.133')
print(result1)