def setup_vhba_template(server, module): from ucsmsdk.mometa.vnic.VnicSanConnTempl import VnicSanConnTempl from ucsmsdk.mometa.vnic.VnicFcIf import VnicFcIf ansible = module.params args_mo = _get_mo_params(ansible) changed = False for vhba in args_mo['vhba_list']: exists = False mo = server.query_dn(args_mo['org_dn'] + '/san-conn-templ-' + vhba['name']) if mo: exists = True if ansible['state'] == 'absent': if exists: changed = True if not module.check_mode: server.remove_mo(mo) server.commit() else: if not exists: changed = True if not module.check_mode: # create if mo does not already exist mo = VnicSanConnTempl(parent_mo_or_dn=args_mo['org_dn'], ident_pool_name=vhba['wwpn_pool'], name=vhba['name'], switch_id=vhba['fabric']) mo_1 = VnicFcIf(parent_mo_or_dn=mo, name=vhba['vsan']) server.add_mo(mo, True) server.commit() return changed
def main(): argument_spec = ucs_argument_spec argument_spec.update( org_dn=dict(type='str', default='org-root'), name=dict(type='str'), descr=dict(type='str'), fabric=dict(type='str', default='A', choices=['A', 'B']), redundancy_type=dict(type='str', default='none', choices=['none', 'primary', 'secondary']), vsan=dict(type='str', default='default'), template_type=dict(type='str', default='initial-template', choices=['initial-template', 'updating-template']), max_data=dict(type='str', default='2048'), wwpn_pool=dict(type='str', default='default'), qos_policy=dict(type='str'), pin_group=dict(type='str'), stats_policy=dict(type='str', default='default'), state=dict(type='str', default='present', choices=['present', 'absent']), vhba_template_list=dict(type='list'), ) # Note that use of vhba_template_list is an experimental feature which allows multiple resource updates with a single UCSM connection. # Support for vhba_template_list may change or be removed once persistent UCS connections are supported. # Either vhba_template_list or name is required (user can specify either a list of single resource). module = AnsibleModule( argument_spec, supports_check_mode=True, required_one_of=[['vhba_template_list', 'name']], mutually_exclusive=[['vhba_template_list', 'name']], ) ucs = UCSModule(module) err = False from ucsmsdk.mometa.vnic.VnicSanConnTempl import VnicSanConnTempl from ucsmsdk.mometa.vnic.VnicFcIf import VnicFcIf changed = False try: # Only documented use is a single resource, but to also support experimental # feature allowing multiple updates all params are converted to a vhba_template_list below. if module.params['vhba_template_list']: # directly use the list (single resource and list are mutually exclusive vhba_template_list = module.params['vhba_template_list'] else: # single resource specified, create list from the current params vhba_template_list = [module.params] for vhba_template in vhba_template_list: mo_exists = False props_match = False # set default params. Done here to set values for lists which can't be done in the argument_spec if not vhba_template.get('descr'): vhba_template['descr'] = '' if not vhba_template.get('fabric'): vhba_template['fabric'] = 'A' if not vhba_template.get('redundancy_type'): vhba_template['redundancy_type'] = 'none' if not vhba_template.get('vsan'): vhba_template['vsan'] = 'default' if not vhba_template.get('template_type'): vhba_template['template_type'] = 'initial-template' if not vhba_template.get('max_data'): vhba_template['max_data'] = '2048' if not vhba_template.get('wwpn_pool'): vhba_template['wwpn_pool'] = 'default' if not vhba_template.get('qos_policy'): vhba_template['qos_policy'] = '' if not vhba_template.get('pin_group'): vhba_template['pin_group'] = '' if not vhba_template.get('stats_policy'): vhba_template['stats_policy'] = 'default' # dn is <org_dn>/san-conn-templ-<name> dn = module.params['org_dn'] + '/san-conn-templ-' + vhba_template[ 'name'] mo = ucs.login_handle.query_dn(dn) if mo: mo_exists = True # check top-level mo props kwargs = dict(descr=vhba_template['descr']) kwargs['switch_id'] = vhba_template['fabric'] kwargs['redundancy_pair_type'] = vhba_template[ 'redundancy_type'] kwargs['templ_type'] = vhba_template['template_type'] kwargs['max_data_field_size'] = vhba_template['max_data'] kwargs['ident_pool_name'] = vhba_template['wwpn_pool'] kwargs['qos_policy_name'] = vhba_template['qos_policy'] kwargs['pin_to_group_name'] = vhba_template['pin_group'] kwargs['stats_policy_name'] = vhba_template['stats_policy'] if (mo.check_prop_match(**kwargs)): # top-level props match, check next level mo/props child_dn = dn + '/if-default' mo_1 = ucs.login_handle.query_dn(child_dn) if mo_1: kwargs = dict(name=vhba_template['vsan']) if (mo_1.check_prop_match(**kwargs)): props_match = True if module.params['state'] == 'absent': # mo must exist but all properties do not have to match if mo_exists: if not module.check_mode: ucs.login_handle.remove_mo(mo) ucs.login_handle.commit() changed = True else: if not props_match: if not module.check_mode: # create if mo does not already exist mo = VnicSanConnTempl( parent_mo_or_dn=module.params['org_dn'], name=vhba_template['name'], descr=vhba_template['descr'], switch_id=vhba_template['fabric'], redundancy_pair_type=vhba_template[ 'redundancy_type'], templ_type=vhba_template['template_type'], max_data_field_size=vhba_template['max_data'], ident_pool_name=vhba_template['wwpn_pool'], qos_policy_name=vhba_template['qos_policy'], pin_to_group_name=vhba_template['pin_group'], stats_policy_name=vhba_template['stats_policy'], ) mo_1 = VnicFcIf( parent_mo_or_dn=mo, name=vhba_template['vsan'], ) ucs.login_handle.add_mo(mo, True) ucs.login_handle.commit() changed = True except Exception as e: err = True ucs.result['msg'] = "setup error: %s " % str(e) ucs.result['changed'] = changed if err: module.fail_json(**ucs.result) module.exit_json(**ucs.result)
handle.add_mo(mo) mo = FcpoolInitiators(parent_mo_or_dn="org-root", name="WWPN_B", policy_owner="local", descr="", assignment_order="sequential", purpose="port-wwn-assignment") mo_1 = FcpoolBlock(parent_mo_or_dn=mo, to=wwpn_b_to_var, r_from=wwpn_b_from_var) handle.add_mo(mo) vsananame = str(raw_input("What is the Fabric A VSAN Name?")) vsananumber = str(raw_input("What is the Fabric A VSAN Number?")) mo = FabricVsan(parent_mo_or_dn="fabric/san/A", name=vsananame, fcoe_vlan=vsananumber, policy_owner="local", fc_zone_sharing_mode="coalesce", zoning_state="disabled", id=vsananumber) handle.add_mo(mo) vsanbname = str(raw_input("What is the Fabric B VSAN Name?")) vsanbnumber = str(raw_input("What is the Fabric B VSAN Number?")) mo = FabricVsan(parent_mo_or_dn="fabric/san/B", name=vsanbname, fcoe_vlan=vsanbnumber, policy_owner="local", fc_zone_sharing_mode="coalesce", zoning_state="disabled", id=vsanbnumber) handle.add_mo(mo) #CREATE VHBA TEMPLATE mo = VnicSanConnTempl(parent_mo_or_dn="org-root", templ_type="updating-template", name="FC_A", descr="", stats_policy_name="default", switch_id="A", pin_to_group_name="", policy_owner="local", qos_policy_name="", ident_pool_name="WWPN_A", max_data_field_size="2048") mo_1 = VnicFcIf(parent_mo_or_dn=mo, name=vsananame) handle.add_mo(mo) mo = VnicSanConnTempl(parent_mo_or_dn="org-root", templ_type="updating-template", name="FC_B", descr="", stats_policy_name="default", switch_id="B", pin_to_group_name="", policy_owner="local", qos_policy_name="", ident_pool_name="WWPN_B", max_data_field_size="2048") mo_1 = VnicFcIf(parent_mo_or_dn=mo, name=vsanbname) handle.add_mo(mo) else : print "Last Step" #Create Service Profile Template ESX mo = LsServer(parent_mo_or_dn="org-root", vmedia_policy_name="", ext_ip_state="none", bios_profile_name="", mgmt_fw_policy_name="", agent_policy_name="", mgmt_access_policy_name="", dynamic_con_policy_name="", kvm_mgmt_policy_name="", sol_policy_name="", uuid="0", descr="", stats_policy_name="default", policy_owner="local", ext_ip_pool_name="ext-mgmt", boot_policy_name="default", usr_lbl="", host_fw_policy_name="", vcon_profile_name="", ident_pool_name="default", src_templ_name="", type="updating-template", local_disk_policy_name="default", scrub_policy_name="", power_policy_name="default", maint_policy_name="User-Ack", name="ESX", resolve_remote="yes") mo_1 = LsVConAssign(parent_mo_or_dn=mo, admin_vcon="any", order="1", transport="ethernet", vnic_name="VMDATA_A") mo_2 = LsVConAssign(parent_mo_or_dn=mo, admin_vcon="any", order="2", transport="ethernet", vnic_name="VMDATA_B")
mo = StorageLocalDiskConfigPolicy(parent_mo_or_dn=my_Full_Path_Org, protect_config="yes", name="Local_Disk_CP", descr="Local Disk Configuration Policy Desc", flex_flash_raid_reporting_state="enable", flex_flash_state="enable", policy_owner="local", mode="any-configuration") handle.add_mo(mo) handle.commit() #Create Boot Policy (boot from SD) mo = LsbootPolicy(parent_mo_or_dn=my_Full_Path_Org, name="Boot_Policy", descr="Boot Policy Desc", reboot_on_update="no", policy_owner="local", enforce_vnic_name="yes", boot_mode="legacy") mo_1 = LsbootVirtualMedia(parent_mo_or_dn=mo, access="read-write-drive", lun_id="0", mapping_name="", order="2") mo_2 = LsbootStorage(parent_mo_or_dn=mo, order="1") mo_2_1 = LsbootLocalStorage(parent_mo_or_dn=mo_2, ) mo_2_1_1 = LsbootUsbFlashStorageImage(parent_mo_or_dn=mo_2_1, order="1") handle.add_mo(mo) handle.commit() #Create HBA Template if(FC): mo = VnicSanConnTempl(parent_mo_or_dn=my_Full_Path_Org, templ_type="updating-template", name="fc-a", descr="", stats_policy_name="default", switch_id="A", pin_to_group_name="", policy_owner="local", qos_policy_name="", ident_pool_name="WWPN_Pool-A", max_data_field_size="2048") mo_1 = VnicFcIf(parent_mo_or_dn=mo, name="default") handle.add_mo(mo) handle.commit() mo = VnicSanConnTempl(parent_mo_or_dn=my_Full_Path_Org, templ_type="updating-template", name="fc-b", descr="", stats_policy_name="default", switch_id="B", pin_to_group_name="", policy_owner="local", qos_policy_name="", ident_pool_name="WWPN_Pool-B", max_data_field_size="2048") mo_1 = VnicFcIf(parent_mo_or_dn=mo, name="default") handle.add_mo(mo) handle.commit() #Configuring Uplink ports #FI-A Port-3 mo = FabricEthLanEp(parent_mo_or_dn="fabric/lan/A", eth_link_profile_name="default", name="", flow_ctrl_policy="default", admin_speed="10gbps", auto_negotiate="yes", usr_lbl="", slot_id="1", admin_state="enabled", port_id="3") handle.add_mo(mo) handle.commit() #FI-A Port-4
def vhba_template(input): vhba_template = input results = {} ucs_handle = pickle.loads( str( ucs_login.main(vhba_template['ip'], vhba_template['username'], vhba_template['password']))) # set default params if not vhba_template.get('descr'): vhba_template['descr'] = '' if not vhba_template.get('fabric'): vhba_template['fabric'] = 'A' if not vhba_template.get('redundancy_type'): vhba_template['redundancy_type'] = 'none' if not vhba_template.get('vsan'): vhba_template['vsan'] = 'default' if not vhba_template.get('template_type'): vhba_template['template_type'] = 'initial-template' if not vhba_template.get('max_data'): vhba_template['max_data'] = '2048' if not vhba_template.get('wwpn_pool'): vhba_template['wwpn_pool'] = 'default' if not vhba_template.get('qos_policy'): vhba_template['qos_policy'] = '' if not vhba_template.get('pin_group'): vhba_template['pin_group'] = '' if not vhba_template.get('stats_policy'): vhba_template['stats_policy'] = 'default' if not vhba_template.get('org_dn'): vhba_template['org_dn'] = 'org-root' changed = False if vhba_template['state'] == 'present': results['expected'] = True else: results['expected'] = False results['name'] = vhba_template['name'] try: exists = False # dn is <org_dn>/san-conn-templ-<name> dn = vhba_template['org_dn'] + '/san-conn-templ-' + vhba_template[ 'name'] mo = ucs_handle.query_dn(dn) if mo: # check top-level mo props kwargs = {} kwargs['descr'] = vhba_template['descr'] kwargs['switch_id'] = vhba_template['fabric'] kwargs['redundancy_pair_type'] = vhba_template['redundancy_type'] kwargs['templ_type'] = vhba_template['template_type'] kwargs['max_data_field_size'] = vhba_template['max_data'] kwargs['ident_pool_name'] = vhba_template['wwpn_pool'] kwargs['qos_policy_name'] = vhba_template['qos_policy'] kwargs['pin_to_group_name'] = vhba_template['pin_group'] kwargs['stats_policy_name'] = vhba_template['stats_policy'] if (mo.check_prop_match(**kwargs)): # top-level props match, check next level mo/props child_dn = dn + '/if-default' mo_1 = ucs_handle.query_dn(child_dn) if mo_1: kwargs = {} kwargs['name'] = vhba_template['vsan'] if (mo_1.check_prop_match(**kwargs)): exists = True if vhba_template['state'] == 'absent': if exists: if not vhba_template['check_exists']: ucs_handle.remove_mo(mo) ucs_handle.commit() changed = True results['removed'] = True else: # create/modify for state 'present' results['removed'] = False if not exists: if not vhba_template['check_exists']: # create if mo does not already exist mo = VnicSanConnTempl( parent_mo_or_dn=vhba_template['org_dn'], name=vhba_template['name'], descr=vhba_template['descr'], switch_id=vhba_template['fabric'], redundancy_pair_type=vhba_template['redundancy_type'], templ_type=vhba_template['template_type'], max_data_field_size=vhba_template['max_data'], ident_pool_name=vhba_template['wwpn_pool'], qos_policy_name=vhba_template['qos_policy'], pin_to_group_name=vhba_template['pin_group'], stats_policy_name=vhba_template['stats_policy']) mo_1 = VnicFcIf(parent_mo_or_dn=mo, name=vhba_template['vsan']) ucs_handle.add_mo(mo, True) ucs_handle.commit() changed = True results['created'] = True except Exception as e: err = True results['msg'] = "setup error: %s " % str(e) results['changed'] = changed results['present'] = exists ucs_handle = pickle.dumps(ucs_handle) ucs_logout.main(ucs_handle) return results