mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
add _meta in the list json
This commit is contained in:
parent
3d62e55abe
commit
7c094c9379
1 changed files with 39 additions and 17 deletions
|
@ -29,7 +29,18 @@ class ProxmoxNodeList(list):
|
||||||
def get_names(self):
|
def get_names(self):
|
||||||
return [node['node'] for node in self]
|
return [node['node'] for node in self]
|
||||||
|
|
||||||
|
class ProxmoxQemu(dict):
|
||||||
|
def get_variables(self):
|
||||||
|
variables = {}
|
||||||
|
for key, value in self.iteritems():
|
||||||
|
variables['proxmox_' + key] = value
|
||||||
|
return variables
|
||||||
|
|
||||||
class ProxmoxQemuList(list):
|
class ProxmoxQemuList(list):
|
||||||
|
def __init__(self, data=[]):
|
||||||
|
for item in data:
|
||||||
|
self.append(ProxmoxQemu(item))
|
||||||
|
|
||||||
def get_names(self):
|
def get_names(self):
|
||||||
return [qemu['name'] for qemu in self if qemu['template'] != 1]
|
return [qemu['name'] for qemu in self if qemu['template'] != 1]
|
||||||
|
|
||||||
|
@ -37,6 +48,13 @@ class ProxmoxQemuList(list):
|
||||||
results = [qemu for qemu in self if qemu['name'] == name]
|
results = [qemu for qemu in self if qemu['name'] == name]
|
||||||
return results[0] if len(results) > 0 else None
|
return results[0] if len(results) > 0 else None
|
||||||
|
|
||||||
|
def get_variables(self):
|
||||||
|
variables = {}
|
||||||
|
for qemu in self:
|
||||||
|
variables[qemu['name']] = qemu.get_variables()
|
||||||
|
|
||||||
|
return variables
|
||||||
|
|
||||||
class ProxmoxPoolList(list):
|
class ProxmoxPoolList(list):
|
||||||
def get_names(self):
|
def get_names(self):
|
||||||
return [pool['poolid'] for pool in self]
|
return [pool['poolid'] for pool in self]
|
||||||
|
@ -95,40 +113,42 @@ class ProxmoxAPI(object):
|
||||||
return ProxmoxPool(self.get('api2/json/pools/{}'.format(poolid)))
|
return ProxmoxPool(self.get('api2/json/pools/{}'.format(poolid)))
|
||||||
|
|
||||||
def main_list(options):
|
def main_list(options):
|
||||||
result = {}
|
results = {
|
||||||
|
'all': {
|
||||||
|
'hosts': [],
|
||||||
|
},
|
||||||
|
'_meta': {
|
||||||
|
'hostvars': {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
proxmox_api = ProxmoxAPI(options)
|
proxmox_api = ProxmoxAPI(options)
|
||||||
proxmox_api.auth()
|
proxmox_api.auth()
|
||||||
|
|
||||||
# all
|
|
||||||
result['all'] = []
|
|
||||||
for node in proxmox_api.nodes().get_names():
|
for node in proxmox_api.nodes().get_names():
|
||||||
result['all'] += proxmox_api.node_qemu(node).get_names()
|
qemu_list = proxmox_api.node_qemu(node)
|
||||||
|
results['all']['hosts'] += qemu_list.get_names()
|
||||||
|
results['_meta']['hostvars'].update(qemu_list.get_variables())
|
||||||
|
|
||||||
# pools
|
# pools
|
||||||
for pool in proxmox_api.pools().get_names():
|
for pool in proxmox_api.pools().get_names():
|
||||||
result[pool] = proxmox_api.pool(pool).get_members_name()
|
results[pool] = {
|
||||||
|
'hosts': proxmox_api.pool(pool).get_members_name(),
|
||||||
|
}
|
||||||
|
|
||||||
print json.dumps(result)
|
return json.dumps(results)
|
||||||
|
|
||||||
def main_host(options):
|
def main_host(options):
|
||||||
results = {}
|
|
||||||
|
|
||||||
proxmox_api = ProxmoxAPI(options)
|
proxmox_api = ProxmoxAPI(options)
|
||||||
proxmox_api.auth()
|
proxmox_api.auth()
|
||||||
|
|
||||||
host = None
|
|
||||||
for node in proxmox_api.nodes().get_names():
|
for node in proxmox_api.nodes().get_names():
|
||||||
qemu_list = proxmox_api.node_qemu(node)
|
qemu_list = proxmox_api.node_qemu(node)
|
||||||
qemu = qemu_list.get_by_name(options.host)
|
qemu = qemu_list.get_by_name(options.host)
|
||||||
if qemu:
|
if qemu:
|
||||||
break
|
return json.dumps(qemu.get_variables())
|
||||||
|
|
||||||
if qemu:
|
print json.dumps({})
|
||||||
for key, value in qemu.iteritems():
|
|
||||||
results['proxmox_' + key] = value
|
|
||||||
|
|
||||||
print json.dumps(results)
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = OptionParser(usage='%prog [options] --list | --host HOSTNAME')
|
parser = OptionParser(usage='%prog [options] --list | --host HOSTNAME')
|
||||||
|
@ -140,12 +160,14 @@ def main():
|
||||||
(options, args) = parser.parse_args()
|
(options, args) = parser.parse_args()
|
||||||
|
|
||||||
if options.list:
|
if options.list:
|
||||||
main_list(options)
|
json = main_list(options)
|
||||||
elif options.host:
|
elif options.host:
|
||||||
main_host(options)
|
json = main_host(options)
|
||||||
else:
|
else:
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
print json
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue