Skip to content

Instantly share code, notes, and snippets.

@LeTink
Last active August 29, 2015 14:22
Show Gist options
  • Save LeTink/6311cb2f12567f62efef to your computer and use it in GitHub Desktop.
Save LeTink/6311cb2f12567f62efef to your computer and use it in GitHub Desktop.
Attempt at salt module
#!/usr/bin/env python
import json
import sys
import dns.resolver
import re
import subprocess
# as salt is installed in the OS'es python path and conda knows nothing about it ...
sys.path.append("/usr/lib/python2.7/dist-packages")
import salt.client
# regex, we only want to see the oobs in our list.
regex=re.compile("^(oob-[^.]+).*")
# get a list of all accepted keys from the salt-master
jdata = subprocess.Popen(["sudo salt-key -l acc --out json"], stdout=subprocess.PIPE, shell=True)
(out, err) = jdata.communicate()
#print "program output:", out
#print type( out )
#print "program error:", err
data = json.loads(out)
# set up a DNS resolver to we can query bind on the NMS for oobs interfaces
my_resolver = dns.resolver.Resolver()
my_resolver.nameservers = ['192.168.1.1']
# empty dictionary to receive ib and ob IPs per minion.
ips = {}
def __virtual__():
'''
Dummy virtual because I have no idea why this is needed
'''
return True
def get_oobs_extra_ips():
# list of oobs extracted from salt's list.
oobs = [ str(x) for x in [m.group(1) for l in data['minions'] for m in [regex.search(l)] if m] ]
for minion in oobs:
tmpips = list()
print minion
try:
answer = my_resolver.query(minion)
for rdata in answer:
print "Address", rdata.address
try:
ib = my_resolver.query(minion+'-ib')
print( ib[0].address )
tmpips.append( ib[0].address )
except dns.exception.DNSException:
print "no IB"
try:
ob = my_resolver.query(minion+'-ob')
print( ob[0].address )
tmpips.append( ob[0].address )
except dns.exception.DNSException:
print "no OB"
except dns.exception.DNSException:
print "nothing here"
print ""
ips[minion] = tmpips
for key, value in ips.iteritems():
print key
print ' '.join(value)
return ips
if __name__ == "__main__":
print get_oobs_extra_ips()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment