updated python scripts

This commit is contained in:
Paul 2021-11-14 13:35:25 +01:00
parent 351dab96dc
commit cd4e14a8dd
2 changed files with 24 additions and 19 deletions

View File

@ -48,6 +48,7 @@ def set_config(url, verify, apikey, config):
req = Request(method="POST", req = Request(method="POST",
url=fullurl, url=fullurl,
data=json.dumps(config).encode()) data=json.dumps(config).encode())
req.add_header("Content-Type", "application/json")
req.add_header("X-API-Key", apikey) req.add_header("X-API-Key", apikey)
try: try:
res = urlopen(req, context=get_context(verify)) res = urlopen(req, context=get_context(verify))

View File

@ -3,7 +3,7 @@
import sys import sys
import subprocess import subprocess
import json import json
import salt.modules.smtp from salt.modules.smtp import send_msg
''' '''
@ -16,20 +16,18 @@ def email_errors(fromaddr, toaddrs, subject, data_str, smtp_server):
error = False error = False
changes = False changes = False
try: if type(data['return']) is dict:
if type(data['return']) is dict: for _, result in data['return'].items():
for _, result in data['return'].iteritems(): if not result['result']:
if not result['result']: error = True
error = True break
break if result['changes']:
if result['changes']: changes = True
changes = True break
break else:
else: if "success" in data.keys():
if not data['success']: if not data['success']:
error = True error = True
except KeyError:
sys.exit()
if error or changes: if error or changes:
js = subprocess.check_output(["salt-run", "--out=json", js = subprocess.check_output(["salt-run", "--out=json",
@ -42,17 +40,23 @@ def email_errors(fromaddr, toaddrs, subject, data_str, smtp_server):
name = outdata[nodename][i]["name"] name = outdata[nodename][i]["name"]
comment = outdata[nodename][i]["comment"].rstrip('\n') comment = outdata[nodename][i]["comment"].rstrip('\n')
data = "%s- %s / %s\n" % (body, name, comment) data = "%s- %s / %s\n" % (body, name, comment)
salt.modules.smtp.send_msg(recipient=toaddrs, message=data, send_msg(recipient=toaddrs,
subject=subject, sender=fromaddr, message=data,
server=smtp_server, use_ssl=False) subject=subject,
sender=fromaddr,
server=smtp_server,
use_ssl=False)
return True return True
def email_auth(fromaddr, toaddrs, subject, data_str, smtp_server): def email_auth(fromaddr, toaddrs, subject, data_str, smtp_server):
data = eval(data_str) data = eval(data_str)
salt.modules.smtp.send_msg(recipient=toaddrs, message=data, send_msg(recipient=toaddrs,
subject=subject, sender=fromaddr, message=data,
server=smtp_server, use_ssl=False) subject=subject,
sender=fromaddr,
server=smtp_server,
use_ssl=False)
return True return True