|
devansible.py
#!/usr/bin/python
# coding:utf-8
import sys
try:
import json
except ImportError:
import simplejson as json
dfile = 'ansible.sh'
def grouplist():
inventory = {}
# inventory['local'] = ['127.0.0.1']
# sfile = '/etc/ansible/books.txt'
sfile = 'books.txt'
with open(sfile, 'rb') as f:
for i in f.readlines():
group = i.strip().split()[0]
name = i.strip().split()[1]
if not group in inventory:
inventory[group] = {
'hosts': []
}
inventory[group]['hosts'].append(name)
# print json.dumps(inventory, indent=4)
return inventory
def hostinfo(name):
vars = {}
vars = {
'admin': 'Jane Jolie',
'datacenter': 1
}
print json.dumps(vars, indent=4)
def ansiblelast():
inventory = grouplist()
keys = []
stringlist = []
for key, item in inventory.iteritems():
keys.append(key)
print keys
for key in keys:
stringa = 'ansible-playbook -i /etc/ansible/devhosts.py %s.yml --extra-vars ' \
'"host=%s"' % (key,key)
stringlist.append(stringa)
with open(dfile, 'wb') as f1:
for i in stringlist:
line = str(i) + "\n"
f1.write(line)
if __name__ == '__main__':
ansiblelast() |
|
|