Skip to content

Instantly share code, notes, and snippets.

@Voronenko
Created May 18, 2017 07:34
Show Gist options
  • Save Voronenko/59f389b6cccc8afc2bc7b3466b05af92 to your computer and use it in GitHub Desktop.
Save Voronenko/59f389b6cccc8afc2bc7b3466b05af92 to your computer and use it in GitHub Desktop.
ansible /etc/hosts inventory
#!/usr/bin/env python
import json
hosts = open('/etc/hosts','r')
result={}
allservers=[]
for line in hosts:
if line[0].isdigit():
host = line.split()
result[host[1]]= {}
result[host[1]]["hosts"] = [host[0]]
allservers.append(host[1])
result["allhosts"] = {}
result["allhosts"]["children"]=allservers
print json.dumps(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment