Skip to content

Instantly share code, notes, and snippets.

@agoddard
Created May 9, 2012 23:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agoddard/2649639 to your computer and use it in GitHub Desktop.
Save agoddard/2649639 to your computer and use it in GitHub Desktop.
Converts Nagios hosts config to knife role from file commands.
#!/usr/bin/env python
def host_groups_to_knife(hosts_config, output_file="knife_commands.sh"):
"""Converts Nagios hosts config to knife role from file commands."""
file = open(hosts_config, 'r')
output = "#!/bin/bash\n"
for line in file:
# all data is collected if we get a }
if line[0] == "}":
# print the knife commands
output += 'echo "updating %s";\n' % host_name
for group in groups:
output += 'knife node run list add %s "role[%s]";\n' % (address, group)
output += "\n"
# gather the data
elif "address" in line:
address = line.strip().split()[1]
elif "host_name" in line:
host_name = line.strip().split()[1]
elif "hostgroups" in line:
groups = line.strip().split()[1].split(',')
# write the commands to file
out = open(output_file, 'w')
out.write(output)
out.close()
if __name__ == "__main__":
import sys
if len(sys.argv) == 2:
host_groups_to_knife(sys.argv[1])
elif len(sys.argv) == 3:
host_groups_to_knife(sys.argv[1], output_file=sys.argv[2])
else:
print "Wrong number of arguments.\n"
print "Usage: python host_groups_to_knife.py infile outfile"
@agoddard
Copy link
Author

agoddard commented May 9, 2012

ah fjük, need to get dns in the mix instead of IPs... this thing is 99% there, needs to support FQDNs :-/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment