使用 python-request 你可以创建一个这样的函数:
import requests
import os
def accept_salt_clientkey(keyname):
url = 'https://saltmaster:8000'
headers = {'Accept':'application/json'}
login_payload = {'username':'saltuser','password':'saltpasswd','eauth':'pam'}
accept_key_payload = {'fun': 'key.accept','client':'wheel','tgt':'*','match':keyname}
login_request = requests.post(os.path.join(url,'login'),headers=headers,data=login_payload)
request = requests.post(url,headers=headers,data=accept_key_payload,cookies=login_request.cookies)
keytype = request.json()['return'][0]['data']['return']
if keytype:
for key,value in keytype.iteritems():
if value[0] == keyname:
return True
break
else:
raise Exception('{} does not match!'.format(keyname))
else:
raise Exception('{} key does not exist in master until now...'.format(keyname))
您可以使用主机名(应该是键名)作为参数来调用它:
accept_salt_clientkey(mynewhost.com)
当然,您可以将用户名/密码存储在 config.py 文件中或以其他方式存储,具体取决于您将如何调用它,但这可能会给您一个大致的概念。