def get_esxi_inventory(self, vm_automation): myserver = vm_automation.esxiServer("192.168.31.137", "root", "@ddVantage2019", "443", "first_log_rapid7.log") #print myserver.connect() #print myserver.getVersion() myserver.enumerateVms() vmnamelist = [] for vm in myserver.vmList: #print vm.__dict__.keys() #print vm.vmName #print "vm.vmIp is: {} ".format(vm.vmIp) #print "vm OS is {}".format(vm.vmOS) #print "vm.vmUsername is: {} ".format(vm.vmUsername) #print "" vmnamelist.append(vm.vmName) vmnamelist = [] #create empty list of IPs vmips = [] #print "create vm Dictionary" vmDic = {} for vm in myserver.vmList: vmDic[vm.vmName] = vm #print vmDic[vm.vmName].getVmIp() vmips.append(vmDic[vm.vmName].getVmIp()) #print "vmnamelist" #print vmnamelist #print "vm ips list" #print vmips return vmips
def get_esxi_inventory(self, vm_automation): #create esxi instance myserver = vm_automation.esxiServer("192.168.31.137", "root", "@ddVantage2019", "443", "first_log_rapid7.log") #print myserver.connect() #print myserver.getVersion() #myserver.enumerateVms() return myserver
def get_esxi(self): if self.esxi_config is not None: # TODO: make this more efficient than an new object per call vmServer = vm_automation.esxiServer( self.esxi_config["esxi_host"], self.esxi_config["esxi_username"], self.esxi_config["esxi_password"], '443', # default port for interaction 'esxi_automation.log') vmServer.connect() return vmServer return None
def get_esxi(esxi_file): if os.path.isfile(esxi_file): with open(esxi_file) as config_file: esxi_config = json.load(config_file) # TODO: make this more efficient than an new object per call vmServer = vm_automation.esxiServer(esxi_config["esxi_host"], esxi_config["esxi_username"], esxi_config["esxi_password"], '443', # default port for interaction 'esxi_automation.log') vmServer.connect() return vmServer return None
import vm_automation #create esxi instance myserver = vm_automation.esxiServer("192.168.31.137", "root", "@ddVantage2019", "443", "first_log_rapid7.log") print myserver.connect() print myserver.getVersion() myserver.enumerateVms() mainus={"all": {"children": ["ungrouped","redservers","linxy", "winservers"],"hosts": [],"vars": {}}} #put ALL predefined groups here. Update later with meta and other host groups details based on script findings #"_meta": {metas}, "all": {alles}, "linxy": {debianss}, "redservers": {redss}, "winservers": {winss}, "ungrouped": {ungroupss}} #prepare mainus metas={} #is the value of _meta. hostvars={} # is the content of metas: metas.update("hostvars": hostvars) #alles={"children": [], "vars": {}} #is the VALUE of all debianss={"children": [], "vars": {}} #is the value of linxy redss= {"children": [], "vars": {}} #is the Value of redservers ungroupss = {"children": [], "vars": {}}#is the Value ofungrouped winss = {"children": [], "vars": {}}#is the Value of winservers #prepare _meta hostvars hostvarius={} hostvarius_element= {} #{"{}".format(vm.VmName): "{}".format(hostvaius_element_details)} hostvaius_element_details= {"ansible_user": "******"} #{"ansible_host": "192.168.31.142", } #prepare the non _meta #prepare the hosts list value inside each non _meta reds=[] debians=[] wins=[] ungrouped_hosts=[] groups=["redservers", "linxy", "windows", "ungrouped"] #create empty list of IPs
def get_esxi_inventory(vm_automation): #create esxi instance myserver = vm_automation.esxiServer("192.168.31.137", "root", "@ddVantage2019", "443", "first_log_rapid7.log") print myserver.connect() print myserver.getVersion() myserver.enumerateVms() mainus = {} hostvars = { } # is the content of metas: metas.update("hostvars": hostvars) debianss = {"children": [], "vars": {}} #is the value of linxy redss = {"children": [], "vars": {}} #is the Value of redservers ungroupss = {"children": [], "vars": {}} #is the Value ofungrouped winss = {"children": [], "vars": {}} #is the Value of winservers #prepare _meta hostvars hostvarius = {} hostvarius_element = { } #{"{}".format(vm.VmName): "{}".format(hostvaius_element_details)} hostvaius_element_details = { "ansible_user": "******" } #{"ansible_host": "192.168.31.142", } reds = [] debians = [] wins = [] ungrouped_hosts = [] all = { "children": ["ungrouped", "redservers", "linxy", "winservers"], "hosts": [], "vars": {} } #create empty list of IPs vmips = [] vmDic = {} for vm in myserver.vmList: vmDic[vm.vmName] = vm vmips.append(vmDic[vm.vmName].getVmIp()) new_sub_meta = {"ansible_user": "******"} ipoun = vmDic[vm.vmName].getVmIp() new_sub_meta.update({"ansible_host": vmDic[vm.vmName].getVmIp()}) hostvars.update({vmDic[vm.vmName].vmName: new_sub_meta}) #non_meta. if "Red Hat" in vmDic[vm.vmName].vmOS: reds.append(vmDic[vm.vmName].vmName) elif "Ubuntu" in vmDic[vm.vmName].vmOS: debians.append(vmDic[vm.vmName].vmName) elif "Windows" in vmDic[vm.vmName].vmOS: wins.append(vmDic[vm.vmName].vmName) else: ungrouped_hosts.append(vmDic[vm.vmName].vmName) metastase = {} metastase.update({"hostvars": hostvars}) mainus.update({"_meta": metastase}) mainus.update({"all": all}) debianss.update({"hosts": debians}) mainus.update({"linxy": debianss}) redss.update({"hosts": reds}) mainus.update({"redservers": redss}) winss.update({"hosts": wins}) mainus.update({"winservers": winss}) ungroupss.update({"hosts": ungrouped_hosts}) mainus.update({"ungrouped": ungroupss}) return mainus
def build_base(iso, md5, replace_existing): os_types_vmware = { "10": "windows9-64", "2003": "winnetstandard", "2003r2": "winnetstandard-64", "2008": "longhorn-64", "2008r2": "windows7srv-64", "2012": "windows8srv-64", "2012r2": "windows8srv-64", "2016": "windows9srv-64", "7": "windows7-64", "8": "windows8-64", "8.1": "windows8-64", "xp": "winXPPro-64", } os_parts = parse_iso(iso) vm_name = "Win" + os_parts["version"].replace(".", "") + os_parts["arch"] output = "windows_" + os_parts['version'] + "_" + os_parts['arch'] if os_parts["patch_level"] is not None: vm_name += os_parts["patch_level"] output += "_" + os_parts["patch_level"] if os_parts["build_version"] is not None: vm_name += "_" + os_parts["build_version"] output += "_" + os_parts["build_version"] temp_path = os.path.join(TEMP_DIR, vm_name) if not os.path.exists(temp_path): os.makedirs(temp_path) # building vmware only for now output += "_vmware.box" packerfile = './windows_packer.json' # TODO: create custom vagrant file for the box being created for now packages a generic file vagrant_template = 'vagrantfile-windows_packer.template' only = ['vmware-iso'] packer_vars = {"iso_checksum_type": "md5"} esxi_file = "esxi_config.json" vmServer = None # if an esxi_config file is found add to the packer file params needed for esxi if os.path.isfile(esxi_file): with open(esxi_file) as config_file: esxi_config = json.load(config_file) packer_vars.update(esxi_config) # TODO: make this more efficeint than an object per build vmServer = vm_automation.esxiServer( esxi_config["esxi_host"], esxi_config["esxi_username"], esxi_config["esxi_password"], '443', # default port for interaction temp_path + 'esxi_automation.log') vmServer.connect() with open(packerfile) as packer_source: packer_config = json.load(packer_source) for builder in packer_config['builders']: if builder['type'] == "vmware-iso": builder.update({ "remote_type": "esx5", "remote_host": "{{user `esxi_host`}}", "remote_datastore": "{{user `esxi_datastore`}}", "remote_username": "******", "remote_password": "******", "keep_registered": True, "vnc_port_min": "5900", "vnc_port_max": "5911", "vnc_bind_address": "0.0.0.0", "vnc_disable_password": True, "disk_type_id": "thin", "output_directory": vm_name }) if esxi_config['esxi_cache_datastore'] is not None: builder.update({ "remote_cache_datastore": "{{user `esxi_cache_datastore`}}" }) builder['vmx_data'].update( {"ethernet0.networkName": "{{user `esxi_network`}}"}) packer_config["post-processors"] = [] packerfile = os.path.join(temp_path, "current_packer.json") with open(packerfile, "w") as packer_current: json.dump(packer_config, packer_current) autounattend = create_autounattend(vm_name, os_parts) packer_vars.update({ "iso_url": "./iso/" + iso, "iso_checksum": md5, "autounattend": autounattend, "output": "./box/" + output, "vagrantfile_template": vagrant_template, "guest_os_type": os_types_vmware[os_parts['version']], "vm_name": vm_name }) out_file = os.path.join(temp_path, "output.log") err_file = os.path.join(temp_path, "error.log") p = packer.Packer(str(packerfile), only=only, vars=packer_vars, out_iter=out_file, err_iter=err_file) vm = get_vm(vmServer, vm_name) if vm is not None: if replace_existing: vm.powerOff vm.waitForTask(vm.vmObject.Destroy_Task()) else: return p # just return without exec since ret value is not checked anyways p.build(parallel=True, debug=False, force=False) return p