diff --git a/scripts/addHypervisorsToInventory.py b/scripts/addHypervisorsToInventory.py new file mode 100755 index 0000000000000000000000000000000000000000..a25bed6a0fb35a752ca76d76cf51fe5b2af3fe3a --- /dev/null +++ b/scripts/addHypervisorsToInventory.py @@ -0,0 +1,24 @@ +#!/usr/bin/python3 + +# The purpose of this script is to enrich every VM of an ansible inventory file +# in json format with the available hypervisor mapping found in /projects/pMOSP/hypervisor/ + +hypervisormapping = open('m3-latest', 'r') # coming from /projects/pMOSP/hypervisor/m3-latest +# for monarch see /projects/pMOSP/hypervisor/monarch-vm-hw-mapping-2020 +mapping = hypervisormapping.readlines() +mapping=mapping[3:-1] + +import json,socket,sys + +with open('m3inventory.json') as json_file: # this file was created via m3inventory > m3inventory.json + inv = json.load(json_file) + +for map in mapping: + vm=map.split('|')[2].strip() + hyp=map.split('|')[4].strip() + if vm not in inv['_meta']['hostvars'].keys(): + sys.stderr.write("Not found in inventory: {}\n".format(vm)) + continue + inv['_meta']['hostvars'][vm]['hypervisor_ip']=socket.gethostbyname(hyp+'-1g.erc.monash.edu') + +print( "#!/bin/bash\necho '"+json.dumps(inv,indent=4, sort_keys=True)+"'")