def assign_vlan_to_sp_vnic(handle, args): # Remove any existing ironic-<vlan> vifs from this UCSM server existing_ironic_vifs = handle.query_classid( 'VnicEtherIf', filter_str=('(name, ".*ironic-.*") and (dn, ".*{0}.*")'.format( args.sp_name))) for vif in existing_ironic_vifs: handle.remove_mo(vif) handle.commit() # Add the vlan to UCSM globally if it doesn't already exist vlan = handle.query_dn('fabric/lan/net-ironic-{0}'.format(args.vlan)) if not vlan: vp1 = handle.query_dn("fabric/lan") handle.add_mo( FabricVlan(vp1, name="ironic-{0}".format(args.vlan), id=args.vlan)) handle.commit() # Add the the VLAN as the default network for the first NIC on the server eth0 = handle.query_classid('VnicEther', filter_str='(dn, ".*{0}.*")'.format( args.sp_name))[0] VnicEtherIf(parent_mo_or_dn=eth0, default_net="yes", name="ironic-{0}".format(args.vlan)) handle.set_mo(eth0) handle.commit()
def createVNICTemplates(handle, vlan, org): print "Creating Kubernetes VNIC Templates" from ucsmsdk.mometa.vnic.VnicLanConnTempl import VnicLanConnTempl from ucsmsdk.mometa.vnic.VnicEtherIf import VnicEtherIf mo = VnicLanConnTempl(parent_mo_or_dn=org, templ_type="updating-template", name="kubeA", descr="", stats_policy_name="default", switch_id="A", pin_to_group_name="", mtu="1500", policy_owner="local", qos_policy_name="", ident_pool_name="kubeA", nw_ctrl_policy_name="") mo_1 = VnicEtherIf(parent_mo_or_dn=mo, default_net="yes", name=vlan) handle.add_mo(mo) mob = VnicLanConnTempl(parent_mo_or_dn=org, templ_type="updating-template", name="kubeB", descr="", stats_policy_name="default", switch_id="B", pin_to_group_name="", mtu="1500", policy_owner="local", qos_policy_name="", ident_pool_name="kubeB", nw_ctrl_policy_name="") mo_2 = VnicEtherIf(parent_mo_or_dn=mob, default_net="yes", name=vlan) handle.add_mo(mob) try: handle.commit() except UcsException as err: if err.error_code == "103": print "\tVNIC Templates already exist"
def modify_UCS_VNIC(vl_range): handle = UcsHandle(credentials.UCS_login['ipaddr'], credentials.UCS_login['username'], credentials.UCS_login['password']) handle.login() for vlan_id in vl_range: mo = VnicLanConnTempl(parent_mo_or_dn="org-root", name="ACI-A") mo_vnic_a = VnicEtherIf(parent_mo_or_dn=mo, default_net="no", name="ACI-" + vlan_id) handle.set_mo(mo) mo = VnicLanConnTempl(parent_mo_or_dn="org-root", name="ACI-B") mo_vnic_b = VnicEtherIf(parent_mo_or_dn=mo, default_net="no", name="ACI-" + vlan_id) handle.set_mo(mo) handle.commit()
def create_service_profile(name, handle): # Service Profile sp = LsServer(parent_mo_or_dn="org-root", name=name) # Vnic eth0 vnic_eth0 = VnicEther(parent_mo_or_dn=sp, name="eth0") VnicEtherIf(parent_mo_or_dn=vnic_eth0, default_net="yes", name="default") # Vnic eth1 vnic_eth1 = VnicEther(parent_mo_or_dn=sp, name="eth1") VnicEtherIf(parent_mo_or_dn=vnic_eth1, default_net="yes", name="default") VnicFcNode(parent_mo_or_dn=sp, ident_pool_name="", addr="pool-derived") handle.add_mo(sp) try: handle.commit() except: print "Error creating Service Profile" sys.exit(1) handle.logout()
def setup_vnic_template(server, module): from ucsmsdk.mometa.vnic.VnicLanConnTempl import VnicLanConnTempl from ucsmsdk.mometa.vnic.VnicEtherIf import VnicEtherIf ansible = module.params args_mo = _get_mo_params(ansible) changed = False for vnic in args_mo['vnic_list']: exists = False mo = server.query_dn(args_mo['org_dn'] + '/lan-conn-templ-' + vnic['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 #if not "description" in vnic: # vnic["description"] = "" #if not "qos_policy_name" in vnic: # vnic["qos_policy_name"] = "" if not "mtu" in vnic: vnic["mtu"] = "1500" mo = VnicLanConnTempl( parent_mo_or_dn=args_mo['org_dn'], descr=vnic['description'], ident_pool_name=vnic['mac_pool'], name=vnic['name'], mtu=vnic['mtu'], #qos_policy_name=vnic['qos_policy_name'], switch_id=vnic['side']) # make the first network the native vlan def_net = "yes" for v in vnic['vlan']: mo_1 = VnicEtherIf(parent_mo_or_dn=mo, default_net=def_net, name=v) if def_net == "yes": def_net = "no" server.add_mo(mo, True) server.commit() return changed
def add_vlan_to_vnic_template(): """ Add VLAN to VNIC Template """ handle = UcsHandle(UCS_HOST, UCS_USER, UCS_PASS) handle.login() my_vlan = handle.query_classid("fabricVlan", filter_str='(id,"301")') my_templ = handle.query_classid("vnicLanConnTempl", filter_str='(name,"Trunk_B")') VnicEtherIf(parent_mo_or_dn=my_templ[0], default_net=my_vlan[0].default_net, name=my_vlan[0].name) handle.add_mo(my_templ[0], True) handle.commit()
def update_nic(server, vnic, org): from ucsmsdk.mometa.vnic.VnicLanConnTempl import VnicLanConnTempl from ucsmsdk.mometa.vnic.VnicEtherIf import VnicEtherIf # create if mo does not already exist if not "description" in vnic: vnic["description"] = "" if not "qos_policy" in vnic: vnic["qos_policy"] = "" if not "mtu" in vnic: vnic["mtu"] = "1500" if not "stats_policy" in vnic: vnic["stats_policy"] = "default" # default template is updating, this is different than # standard UCS because UCS made the wrong decision for # default. if "updating" in vnic: if vnic["updating"] == "yes": vnic["updating"] = "updating-template" else: vnic["updating"] = "initial-template" else: vnic["updating"] = "updating-template" if not "nw_ctrl_policy" in vnic: vnic["nw_ctrl_policy"] = "" mo = VnicLanConnTempl(parent_mo_or_dn=org, templ_type=vnic['updating'], descr=vnic['description'], ident_pool_name=vnic['mac_pool'], name=vnic['name'], mtu=vnic['mtu'], qos_policy_name=vnic['qos_policy'], stats_policy_name=vnic['stats_policy'], nw_ctrl_policy_name=vnic['nw_ctrl_policy'], switch_id=vnic['side']) if "vlans" in vnic: for v in vnic['vlans']: if not 'native' in v: v['native'] = 'no' mo_x = VnicEtherIf(parent_mo_or_dn=mo, default_net=v['native'], name=v['name']) server.add_mo(mo, True) server.commit()
def ucs_add_vlan_to_vnic(module): vlan_name = module.params.get('vlan_name') ucsm_ip = module.params.get('ip') ucsm_pw = module.params.get('password') ucsm_login = module.params.get('login') vnic_template = module.params.get('vnic_template') default_net = module.params.get('default_net') ucsm = UCS(ucsm_ip, ucsm_login, ucsm_pw) results = {} #Login to UCSM try: ucsm.login() results['logged_in'] = True except Exception as e: module.fail_json(msg=e) FILIST = ['A'] for FI in FILIST: try: obj = ucsm.handle.query_dn("org-root/lan-conn-templ-" + vnic_template) mo_1 = VnicEtherIf(parent_mo_or_dn=obj, default_net=default_net, name=vlan_name) ucsm.handle.add_mo(mo_1) ucsm.handle.commit() results['changed'] = True except Exception as e: results['changed'] = False try: ucsm.handle.logout() results['logged_out'] = True except Exception as e: module.fail_json(msg=e) try: ucsm.handle.logout() results['logged_out'] = True except Exception as e: module.fail_json(msg=e) return results
def add_vlan_to_vnic_template(ucsm, module, vnic_templ_dn, vlan_name, vlan_on_vnic_template, results): #uses the VnicEtherIf class imported earlier to specify #the vlan you want to add to the vnic template #vlan specified in 'name', target vnic template is the #'parent_mo_or_dn' value mo = VnicEtherIf(parent_mo_or_dn=vnic_templ_dn, default_net="no", name=str(vlan_name)) try: #Add the vlan to the vnic template if not vlan_on_vnic_template: ucsm.handle.add_mo(mo) #have not figured out how to deal with what ucsm calls 'ambigious impact' in GUI #ucsm doesn't apply this change if results are unknown, example if a server is in 'config error' state #doesn't error out here but doesn't actually apply ucsm.handle.commit() results['changed'] = True except Exception as e: module.fail_json(msg=e) results['changed'] = False #return results dictionary with the changed / unchanged value return results
def test_002_sp_expert(): ''' This case is generated based on SP expert mode creation wizard. ''' from ucsmsdk.mometa.ls.LsServer import LsServer from ucsmsdk.mometa.ls.LsVConAssign import LsVConAssign from ucsmsdk.mometa.vnic.VnicEther import VnicEther from ucsmsdk.mometa.vnic.VnicEtherIf import VnicEtherIf from ucsmsdk.mometa.vnic.VnicFc import VnicFc from ucsmsdk.mometa.vnic.VnicFcIf import VnicFcIf from ucsmsdk.mometa.vnic.VnicFcNode import VnicFcNode from ucsmsdk.mometa.storage.StorageIniGroup import StorageIniGroup from ucsmsdk.mometa.vnic.VnicFcGroupDef import VnicFcGroupDef from ucsmsdk.mometa.storage.StorageInitiator import StorageInitiator from ucsmsdk.mometa.ls.LsPower import LsPower from ucsmsdk.mometa.fabric.FabricVCon import FabricVCon mo = LsServer(parent_mo_or_dn="org-root", vmedia_policy_name="", ext_ip_state="none", bios_profile_name="SRIOV", mgmt_fw_policy_name="", agent_policy_name="", mgmt_access_policy_name="", dynamic_con_policy_name="", kvm_mgmt_policy_name="", sol_policy_name="", uuid="00000000-0000-0000-0000-0000000000bb", 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="", src_templ_name="", local_disk_policy_name="default", scrub_policy_name="", power_policy_name="default", maint_policy_name="", name="test_sp", resolve_remote="yes") mo_1 = LsVConAssign(parent_mo_or_dn=mo, admin_vcon="any", order="1", transport="ethernet", vnic_name="eth0") mo_2 = LsVConAssign(parent_mo_or_dn=mo, admin_vcon="any", order="2", transport="fc", vnic_name="fc0") mo_3 = VnicEther(parent_mo_or_dn=mo, nw_ctrl_policy_name="", name="eth0", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", admin_cdn_name="", switch_id="A", pin_to_group_name="", mtu="1500", qos_policy_name="", adaptor_profile_name="", ident_pool_name="default", order="1", nw_templ_name="", addr="derived") mo_3_1 = VnicEtherIf(parent_mo_or_dn=mo_3, default_net="yes", name="default") mo_4 = VnicFc(parent_mo_or_dn=mo, addr="derived", name="fc0", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", admin_cdn_name="", switch_id="A", pin_to_group_name="", pers_bind="disabled", pers_bind_clear="no", qos_policy_name="", adaptor_profile_name="", ident_pool_name="", order="2", nw_templ_name="", max_data_field_size="2048") mo_4_1 = VnicFcIf(parent_mo_or_dn=mo_4, name="default") mo_5 = VnicFcNode(parent_mo_or_dn=mo, ident_pool_name="", addr="20:00:00:25:B5:00:00:00") mo_6 = StorageIniGroup(parent_mo_or_dn=mo, name="test", descr="", group_policy_name="", policy_name="", policy_owner="local", rmt_disk_cfg_name="") mo_6_1 = VnicFcGroupDef(parent_mo_or_dn=mo_6, storage_conn_policy_name="", policy_owner="local", name="", descr="", stats_policy_name="default") mo_6_2 = StorageInitiator(parent_mo_or_dn=mo_6, policy_owner="local", name="fc0", descr="") mo_7 = LsPower(parent_mo_or_dn=mo, state="admin-up") mo_8 = FabricVCon(parent_mo_or_dn=mo, placement="physical", fabric="NONE", share="shared", select="all", transport="ethernet,fc", id="1", inst_type="auto") mo_9 = FabricVCon(parent_mo_or_dn=mo, placement="physical", fabric="NONE", share="shared", select="all", transport="ethernet,fc", id="2", inst_type="auto") mo_10 = FabricVCon(parent_mo_or_dn=mo, placement="physical", fabric="NONE", share="shared", select="all", transport="ethernet,fc", id="3", inst_type="auto") mo_11 = FabricVCon(parent_mo_or_dn=mo, placement="physical", fabric="NONE", share="shared", select="all", transport="ethernet,fc", id="4", inst_type="auto") handle.add_mo(mo) handle.commit() obj = handle.query_dn("org-root/ls-test_sp") handle.remove_mo(obj) handle.commit()
def main(): argument_spec = ucs_argument_spec argument_spec.update( org_dn=dict(type='str', default='org-root'), name=dict(type='str', required=True), description=dict(type='str', aliases=['descr'], default=''), fabric=dict(type='str', default='A', choices=['A', 'B', 'A-B', 'B-A']), redundancy_type=dict(type='str', default='none', choices=['none', 'primary', 'secondary']), peer_redundancy_template=dict(type='str', aliases=['peer_redundancy_templ'], default=''), target=dict(type='str', default='adapter', choices=['adapter', 'vm']), template_type=dict(type='str', default='initial-template', choices=['initial-template', 'updating-template']), vlans_list=dict(type='list'), cdn_source=dict(type='str', default='vnic-name', choices=['vnic-name', 'user-defined']), cdn_name=dict(type='str', default=''), mtu=dict(type='str', default='1500'), mac_pool=dict(type='str', default=''), qos_policy=dict(type='str', default=''), network_control_policy=dict(type='str', default=''), pin_group=dict(type='str', default=''), stats_policy=dict(type='str', default='default'), state=dict(type='str', default='present', choices=['present', 'absent']), ) module = AnsibleModule( argument_spec, supports_check_mode=True, required_if=[ ['cdn_source', 'user-defined', ['cdn_name']], ], ) ucs = UCSModule(module) err = False # UCSModule creation above verifies ucsmsdk is present and exits on failure. Additional imports are done below. from ucsmsdk.mometa.vnic.VnicLanConnTempl import VnicLanConnTempl from ucsmsdk.mometa.vnic.VnicEtherIf import VnicEtherIf changed = False try: mo_exists = False props_match = False # dn is <org_dn>/lan-conn-templ-<name> dn = module.params['org_dn'] + '/lan-conn-templ-' + module.params[ 'name'] mo = ucs.login_handle.query_dn(dn) if mo: mo_exists = 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: # set default params for lists which can't be done in the argument_spec if module.params.get('vlans_list'): for vlan in module.params['vlans_list']: if not vlan.get('native'): vlan['native'] = 'no' if not vlan.get('state'): vlan['state'] = 'present' # for target 'adapter', change to internal UCS Manager spelling 'adaptor' if module.params['target'] == 'adapter': module.params['target'] = 'adaptor' if mo_exists: # check top-level mo props kwargs = dict(descr=module.params['description']) kwargs['switch_id'] = module.params['fabric'] kwargs['redundancy_pair_type'] = module.params[ 'redundancy_type'] kwargs['peer_redundancy_templ_name'] = module.params[ 'peer_redundancy_template'] kwargs['ident_pool_name'] = module.params['mac_pool'] # do not check shared props if this is a secondary template if module.params['redundancy_type'] != 'secondary': kwargs['target'] = module.params['target'] kwargs['templ_type'] = module.params['template_type'] kwargs['cdn_source'] = module.params['cdn_source'] kwargs['admin_cdn_name'] = module.params['cdn_name'] kwargs['mtu'] = module.params['mtu'] kwargs['qos_policy_name'] = module.params['qos_policy'] kwargs['nw_ctrl_policy_name'] = module.params[ 'network_control_policy'] kwargs['pin_to_group_name'] = module.params['pin_group'] kwargs['stats_policy_name'] = module.params['stats_policy'] if mo.check_prop_match(**kwargs): # top-level props match, check next level mo/props if not module.params.get('vlans_list'): props_match = True else: # check vlan props for vlan in module.params['vlans_list']: child_dn = dn + '/if-' + str(vlan['name']) mo_1 = ucs.login_handle.query_dn(child_dn) if vlan['state'] == 'absent': if mo_1: props_match = False break else: if mo_1: kwargs = dict(default_net=vlan['native']) if mo_1.check_prop_match(**kwargs): props_match = True else: props_match = False break if not props_match: if not module.check_mode: # create if mo does not already exist # secondary template only sets non shared props if module.params['redundancy_type'] == 'secondary': mo = VnicLanConnTempl( parent_mo_or_dn=module.params['org_dn'], name=module.params['name'], descr=module.params['description'], switch_id=module.params['fabric'], redundancy_pair_type=module. params['redundancy_type'], peer_redundancy_templ_name=module. params['peer_redundancy_template'], ident_pool_name=module.params['mac_pool'], ) else: mo = VnicLanConnTempl( parent_mo_or_dn=module.params['org_dn'], name=module.params['name'], descr=module.params['description'], switch_id=module.params['fabric'], redundancy_pair_type=module. params['redundancy_type'], peer_redundancy_templ_name=module. params['peer_redundancy_template'], target=module.params['target'], templ_type=module.params['template_type'], cdn_source=module.params['cdn_source'], admin_cdn_name=module.params['cdn_name'], mtu=module.params['mtu'], ident_pool_name=module.params['mac_pool'], qos_policy_name=module.params['qos_policy'], nw_ctrl_policy_name=module. params['network_control_policy'], pin_to_group_name=module.params['pin_group'], stats_policy_name=module.params['stats_policy'], ) if module.params.get('vlans_list'): for vlan in module.params['vlans_list']: if vlan['state'] == 'absent': child_dn = dn + '/if-' + str(vlan['name']) mo_1 = ucs.login_handle.query_dn(child_dn) ucs.login_handle.remove_mo(mo_1) else: mo_1 = VnicEtherIf( parent_mo_or_dn=mo, name=str(vlan['name']), default_net=vlan['native'], ) 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)
def sp_create(): # ########################################### # Create the SP that will be associated later # ########################################### log.debug("sp_create") from ucsmsdk.mometa.ls.LsServer import LsServer from ucsmsdk.mometa.lsboot.LsbootDef import LsbootDef from ucsmsdk.mometa.lsboot.LsbootStorage import LsbootStorage from ucsmsdk.mometa.lsboot.LsbootLocalStorage import LsbootLocalStorage from ucsmsdk.mometa.lsboot.LsbootDefaultLocalImage import\ LsbootDefaultLocalImage from ucsmsdk.mometa.vnic.VnicEther import VnicEther from ucsmsdk.mometa.vnic.VnicEtherIf import VnicEtherIf from ucsmsdk.mometa.vnic.VnicFcNode import VnicFcNode 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="", usr_lbl="", host_fw_policy_name="", vcon_profile_name="", ident_pool_name="", src_templ_name="", local_disk_policy_name="", scrub_policy_name="", power_policy_name="default", maint_policy_name="", name="test_sp", resolve_remote="yes") mo_1 = LsbootDef(parent_mo_or_dn=mo, descr="", reboot_on_update="no", adv_boot_order_applicable="no", policy_owner="local", enforce_vnic_name="yes", boot_mode="legacy") mo_1_1 = LsbootStorage(parent_mo_or_dn=mo_1, order="1") mo_1_1_1 = LsbootLocalStorage(parent_mo_or_dn=mo_1_1, ) mo_1_1_1_1 = LsbootDefaultLocalImage(parent_mo_or_dn=mo_1_1_1, order="1") mo_2 = VnicEther(parent_mo_or_dn=mo, nw_ctrl_policy_name="", name="eth0", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", admin_cdn_name="", switch_id="A", pin_to_group_name="", mtu="1500", qos_policy_name="", adaptor_profile_name="", ident_pool_name="", order="unspecified", nw_templ_name="", addr="derived") mo_2_1 = VnicEtherIf(parent_mo_or_dn=mo_2, default_net="yes", name="default") mo_3 = VnicFcNode(parent_mo_or_dn=mo, ident_pool_name="", addr="pool-derived") handle.add_mo(mo) handle.commit()
def run(job, resource, *args, **kwargs): username = conn.username password = conn.password organization = "{{ organization }}" service_profile_template = "{{ service_profile_template }}" create_sp_from_sp_template = "{{ create_sp_from_sp_template }}" service_profile_name = "{{ service_profile_name }}" service_profile_description = "{{ service_profile_description }}" use_blade_servers = "{{ use_blade_servers }}" chassis = "{{ chassis }}" ucs_server_dn = "{{ ucs_server_dn }}" ucs_rack_server = "{{ rack_server }}" mac_pool_name = "{{ mac_pool_name }}" service_profile_server_dn, _ = CustomField.objects.get_or_create( name="service_profile_server_dn", label="Service Profile Server DN", type="STR", show_on_servers=True) handler.login() ucs_event_handler = UcsEventHandle(handler) # create SP in an org set_progress(f"Creating service profile named {service_profile_name}") if create_sp_from_sp_template == "True": dn_set = ucscbasetype.DnSet() dn_set.child_add(Dn(value=f"{service_profile_name}")) xml_element = mf.ls_instantiate_n_named_template( cookie=handler.cookie, dn=service_profile_template, in_error_on_existing="true", in_name_set=dn_set, in_target_org=organization) handler.process_xml_elem(xml_element) else: mo = LsServer(parent_mo_or_dn=organization, 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=service_profile_description, stats_policy_name="default", policy_owner="local", ext_ip_pool_name="ext-mgmt", boot_policy_name="", usr_lbl="", host_fw_policy_name="", vcon_profile_name="", ident_pool_name="", src_templ_name="", local_disk_policy_name="", scrub_policy_name="", power_policy_name="default", maint_policy_name="", name=service_profile_name, resolve_remote="yes") mo_1 = LsbootDef(parent_mo_or_dn=mo, descr="", reboot_on_update="no", adv_boot_order_applicable="no", policy_owner="local", enforce_vnic_name="yes", boot_mode="legacy") mo_1_1 = LsbootStorage(parent_mo_or_dn=mo_1, order="1") mo_1_1_1 = LsbootLocalStorage(parent_mo_or_dn=mo_1_1, ) mo_1_1_1_1 = LsbootDefaultLocalImage(parent_mo_or_dn=mo_1_1_1, order="1") mo_2 = VnicEther(parent_mo_or_dn=mo, nw_ctrl_policy_name="", name="eth0", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", admin_cdn_name="", switch_id="A", pin_to_group_name="", mtu="1500", qos_policy_name="", adaptor_profile_name="", ident_pool_name=mac_pool_name, order="unspecified", nw_templ_name="", addr="derived") mo_2_1 = VnicEtherIf(parent_mo_or_dn=mo_2, default_net="yes", name="default") mo_3 = VnicFcNode(parent_mo_or_dn=mo, ident_pool_name="", addr="pool-derived") handler.add_mo(mo) # Associate a server to a service profile. if use_blade_servers == "True": set_progress( f"Associating service profile {service_profile_name} with {ucs_server_dn}" ) mo = LsBinding( parent_mo_or_dn=f"{organization}/ls-{service_profile_name}", pn_dn=ucs_server_dn, restrict_migration="no") else: set_progress( f"Associating service profile {service_profile_name} with {ucs_rack_server}" ) mo = LsBinding( parent_mo_or_dn=f"{organization}/ls-{service_profile_name}", pn_dn=ucs_rack_server, restrict_migration="no") handler.add_mo(mo) handler.commit() mo = handler.query_dn(f"{organization}/ls-{service_profile_name}") # Save the service profile dn resource.service_profile_server_dn = f"{organization}/ls-{service_profile_name}" resource.name = service_profile_name resource.save() return "SUCCESS", f"Created service profile named {service_profile_name}", ""
def test_001_policy(): from ucsmsdk.mometa.flowctrl.FlowctrlItem import FlowctrlItem # ##################################### # Create a Flow Control Policy # ##################################### mo = FlowctrlItem(parent_mo_or_dn="fabric/lan/flowctrl", snd="off", rcv="off", name="test", prio="on") handle.add_mo(mo) handle.commit() # ##################################### # Delete Flow Control Policy # ##################################### obj = handle.query_dn("fabric/lan/flowctrl/policy-test") handle.remove_mo(obj) handle.commit() # ##################################### # Create Dynamic vNIC Connection Policy # ##################################### from ucsmsdk.mometa.vnic.VnicDynamicConPolicy import VnicDynamicConPolicy mo = VnicDynamicConPolicy(parent_mo_or_dn="org-root", name="test", descr="test", adaptor_profile_name="Linux", policy_owner="local", protection="protected-pref-b", dynamic_eth="54") handle.add_mo(mo) handle.commit() # ##################################### # Delete Dynamic vNIC Connection Policy # ##################################### obj = handle.query_dn("org-root/dynamic-con-test") handle.remove_mo(obj) handle.commit() # ##################################### # Create LACP Policy # ##################################### from ucsmsdk.mometa.fabric.FabricLacpPolicy import FabricLacpPolicy mo = FabricLacpPolicy(parent_mo_or_dn="org-root", fast_timer="fast", policy_owner="local", suspend_individual="true", name="test", descr="") handle.add_mo(mo) handle.commit() # ##################################### # Modify LACP Policy # ##################################### obj = handle.query_dn("org-root/lacp-test") obj.fast_timer = "normal" obj.suspend_individual = "false" obj.policy_owner = "local" obj.descr = "" handle.set_mo(obj) handle.commit() # ##################################### # Delete LACP Policy # ##################################### obj = handle.query_dn("org-root/lacp-test") handle.remove_mo(obj) handle.commit() # ##################################### # Create LAN Connectivity Policy # ##################################### from ucsmsdk.mometa.vnic.VnicLanConnPolicy import VnicLanConnPolicy from ucsmsdk.mometa.vnic.VnicEther import VnicEther from ucsmsdk.mometa.vnic.VnicEtherIf import VnicEtherIf mo = VnicLanConnPolicy(parent_mo_or_dn="org-root", policy_owner="local", name="test", descr="test_policy") mo_1 = VnicEther(parent_mo_or_dn=mo, nw_ctrl_policy_name="default", name="test", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", admin_cdn_name="", switch_id="A", pin_to_group_name="", mtu="1500", qos_policy_name="qos-1", adaptor_profile_name="Linux", ident_pool_name="mac-pool-1", order="1", nw_templ_name="", addr="derived") mo_1_1 = VnicEtherIf(parent_mo_or_dn=mo_1, default_net="yes", name="default") handle.add_mo(mo) handle.commit() # ##################################### # Delete LAN Connectivity Policy # ##################################### obj = handle.query_dn("org-root/lan-conn-pol-test") handle.remove_mo(obj) handle.commit() # ##################################### # Create QoS Policy # ##################################### from ucsmsdk.mometa.epqos.EpqosDefinition import EpqosDefinition from ucsmsdk.mometa.epqos.EpqosEgress import EpqosEgress mo = EpqosDefinition(parent_mo_or_dn="org-root", policy_owner="local", name="test", descr="") mo_1 = EpqosEgress(parent_mo_or_dn=mo, rate="line-rate", host_control="none", name="", prio="best-effort", burst="10240") handle.add_mo(mo) handle.commit() # ##################################### # Delete QoS Policy # ##################################### obj = handle.query_dn("org-root/ep-qos-test") handle.remove_mo(obj) handle.commit()
mtu=row['vNIC_MTU'], policy_owner="local", qos_policy_name="", ident_pool_name=row['vNIC_MAC'], cdn_source="vnic-name", nw_ctrl_policy_name="Enable_CDP") # allow multiple vlans if specified as a list in the csv # note that native vlan is set globally, so native only works here if a single VLAN is specified for vlans in [row['vNIC_VLAN']]: vlans = vlans.split(',') for vlan in vlans: # strip unnecessary characters vlan = vlan.strip(' ') vlan = vlan.strip('\"') mo_1 = VnicEtherIf(parent_mo_or_dn=mo, default_net=row['vNIC_Native_VLAN'], name=vlan) handle.add_mo(mo) handle.commit() if row['LAN_Policy'] and row['vNIC_Name'] and row[ 'vNIC_Order'] and row['LAN_Policy_vNIC_Template']: # LAN Connectivity policy from ucsmsdk.mometa.vnic.VnicLanConnPolicy import VnicLanConnPolicy from ucsmsdk.mometa.vnic.VnicEther import VnicEther # if policy doesn't exist, create it dn = org + '/lan-conn-pol-' + row['LAN_Policy'] mo = handle.query_dn(dn) if not mo: mo = VnicLanConnPolicy(parent_mo_or_dn=org,
vlancount = int(raw_input("How Many VLANS would you like to enter?")) while(vlancount > 0): vlancount -= 1 vlan_name = str(raw_input("What is the VLAN Name?")) vlan_num = str(raw_input("What is the VLAN Number?")) #create vlans mo = FabricVlan(parent_mo_or_dn="fabric/lan", sharing="none", name=vlan_name, id=vlan_num, mcast_policy_name="", policy_owner="local", default_net="no", pub_nw_name="", compression_type="included") handle.add_mo(mo) #Create VNIC Templates vnamevm = str(raw_input("vMotion VLAN Name?")) mo = VnicLanConnTempl(parent_mo_or_dn="org-root", templ_type="updating-template", name="vMotion_A", descr="", stats_policy_name="default", switch_id="A", pin_to_group_name="", mtu="1500", policy_owner="local", qos_policy_name="", ident_pool_name="Fabric_A", nw_ctrl_policy_name="default") mo_1 = VnicEtherIf(parent_mo_or_dn=mo, default_net="yes", name=vnamevm) handle.add_mo(mo) mo = VnicLanConnTempl(parent_mo_or_dn="org-root", templ_type="updating-template", name="vMotion_B", descr="", stats_policy_name="default", switch_id="B", pin_to_group_name="", mtu="1500", policy_owner="local", qos_policy_name="", ident_pool_name="Fabric_B", nw_ctrl_policy_name="default") mo_1 = VnicEtherIf(parent_mo_or_dn=mo, default_net="yes", name=vnamevm) handle.add_mo(mo) vnamem = str(raw_input("ESX Management VLAN Name?")) mo = VnicLanConnTempl(parent_mo_or_dn="org-root", templ_type="updating-template", name="mgmt_A", descr="", stats_policy_name="default", switch_id="A", pin_to_group_name="", mtu="1500", policy_owner="local", qos_policy_name="", ident_pool_name="Fabric_A", nw_ctrl_policy_name="default") mo_1 = VnicEtherIf(parent_mo_or_dn=mo, default_net="yes", name=vnamem) handle.add_mo(mo) mo = VnicLanConnTempl(parent_mo_or_dn="org-root", templ_type="updating-template", name="mgmt_B", descr="", stats_policy_name="default", switch_id="B", pin_to_group_name="", mtu="1500", policy_owner="local", qos_policy_name="", ident_pool_name="Fabric_B", nw_ctrl_policy_name="default") mo_1 = VnicEtherIf(parent_mo_or_dn=mo, default_net="yes", name=vnamem) handle.add_mo(mo)
handle.add_mo(mo) handle.commit() mo = FcpoolInitiators(parent_mo_or_dn=my_Full_Path_Org, name="WWPN_Pool-A", policy_owner="local", descr="WWPN Pool FI-A", assignment_order="sequential", purpose="port-wwn-assignment") mo_1 = FcpoolBlock(parent_mo_or_dn=mo, to="20:01:00:25:B5:A0:00:0F", r_from="20:01:00:25:B5:A0:00:00") handle.add_mo(mo) handle.commit() mo = FcpoolInitiators(parent_mo_or_dn=my_Full_Path_Org, name="WWPN_Pool-B", policy_owner="local", descr="WWPN Pool FI-B", assignment_order="sequential", purpose="port-wwn-assignment") mo_1 = FcpoolBlock(parent_mo_or_dn=mo, to="20:01:00:25:B5:B0:00:0F", r_from="20:01:00:25:B5:B0:00:00") handle.add_mo(mo) handle.commit() #Create vNIC Templates mo = VnicLanConnTempl(parent_mo_or_dn=my_Full_Path_Org, templ_type="updating-template", name="MGMT-A", descr="Management FI-A", stats_policy_name="default", admin_cdn_name="", switch_id="A", pin_to_group_name="", mtu="1500", policy_owner="local", qos_policy_name="", ident_pool_name="MGMT-A", cdn_source="vnic-name", nw_ctrl_policy_name="CDP_EN") mo_1 = VnicEtherIf(parent_mo_or_dn=mo, default_net="yes", name="Mgmt") mo_2 = VnicEtherIf(parent_mo_or_dn=mo, default_net="no", name="vMotion") handle.add_mo(mo) handle.commit() mo = VnicLanConnTempl(parent_mo_or_dn=my_Full_Path_Org, templ_type="updating-template", name="MGMT-B", descr="Management FI-B", stats_policy_name="default", admin_cdn_name="", switch_id="B", pin_to_group_name="", mtu="1500", policy_owner="local", qos_policy_name="", ident_pool_name="MGMT-B", cdn_source="vnic-name", nw_ctrl_policy_name="CDP_EN") mo_1 = VnicEtherIf(parent_mo_or_dn=mo, default_net="yes", name="Mgmt") mo_2 = VnicEtherIf(parent_mo_or_dn=mo, default_net="no", name="vMotion") handle.add_mo(mo) handle.commit() mo = VnicLanConnTempl(parent_mo_or_dn=my_Full_Path_Org, templ_type="updating-template", name="VM-A", descr="Production FI-A", stats_policy_name="default", admin_cdn_name="", switch_id="A", pin_to_group_name="", mtu="1500", policy_owner="local", qos_policy_name="", ident_pool_name="VM-A", cdn_source="vnic-name", nw_ctrl_policy_name="CDP_EN") #Depending on the VLANs that will pass wot the NIC: #mo_1 = VnicEtherIf(parent_mo_or_dn=mo, default_net="no", name=Production) #mo_2 = VnicEtherIf(parent_mo_or_dn=mo, default_net="no", name="VLAN_800") handle.add_mo(mo)
def vnic_template(input): name = input['name'] descr=input['descr'] switch_id=input['switch_id'] redundancy_pair_type="none" templ_type= input['templ_type'] vlan_name=input['vlan_name'] default_net=input['default_net'] cdn_source=input['cdn_source'] admin_cdn_name=input['admin_cdn_name'] mtu=input['mtu'] ident_pool_name= input['ident_pool_name'] state = input['state'] ip=input['ip'] username=input['username'] password=input['password'] mo="" mo_block="" results = {} ucs_handle = pickle.loads(str(ucs_login.main(ip,username,password))) ###-------CHECK IF MO EXISTS--------------------------------- try: mo = ucs_handle.query_dn("org-root/lan-conn-templ-"+name) mo_block=ucs_handle.query_dn("org-root/lan-conn-templ-"+name+"/if-"+vlan_name) mo_children=ucs_handle.query_children(in_dn="org-root/lan-conn-templ-"+name,class_id="vnicEtherIf") except: print("Could not query children of macpool") ###----if expected state is "present"------------------------ if state == "present": block_temp=False if(mo_block and mo_block.name ==vlan_name and mo_block.default_net ==default_net): block_temp=True elif (vlan_name == "" and not(mo_children)): block_temp=True elif (mo_children): block_temp=False else: block_temp=False if (mo): if ( mo.name == name and mo.descr == descr and mo.switch_id ==switch_id and mo.redundancy_pair_type == redundancy_pair_type and mo.templ_type ==templ_type and mo.cdn_source == cdn_source and mo.admin_cdn_name == admin_cdn_name and mo.mtu ==mtu and mo.ident_pool_name == ident_pool_name and block_temp): results['name']=name; results['expected'] = True; results['changed'] = False; results['present'] = True; else: try: modified_mo = VnicLanConnTempl(parent_mo_or_dn="org-root", name=name, descr=descr ,switch_id =switch_id , redundancy_pair_type = redundancy_pair_type ,templ_type = templ_type , cdn_source = cdn_source , admin_cdn_name = admin_cdn_name , mtu =mtu ,ident_pool_name = ident_pool_name ) if(vlan_name): mo_1= VnicEtherIf(parent_mo_or_dn=modified_mo,default_net=default_net, name=vlan_name) ucs_handle.add_mo(modified_mo,True) ucs_handle.commit() results['name']=name; results['present'] = True; results['removed'] = False; results['changed'] = True except Exception,e: print(e) ###----------if not, create boot policy with desired config ---------------- else: try: mo = VnicLanConnTempl(parent_mo_or_dn="org-root", name=name, descr=descr ,switch_id =switch_id , redundancy_pair_type = redundancy_pair_type ,templ_type = templ_type , cdn_source = cdn_source , admin_cdn_name = admin_cdn_name , mtu=mtu ,ident_pool_name = ident_pool_name ) if(vlan_name): mo_1= VnicEtherIf(parent_mo_or_dn=mo,default_net=default_net, name=vlan_name) ucs_handle.add_mo(mo) ucs_handle.commit() results['name']=name; results['present'] = False; results['created'] = True; results['changed'] = True; except: results['error'] = "Vnic template creation failed" return results
mo = VnicLanConnTempl(parent_mo_or_dn="org-root/org-Sub_Org_Test", templ_type="updating-template", name="MGMT-A", descr="Management FI-A", stats_policy_name="default", admin_cdn_name="", switch_id="A", pin_to_group_name="", mtu="1500", policy_owner="local", qos_policy_name="", ident_pool_name="MGMT-A", cdn_source="vnic-name", nw_ctrl_policy_name="CDP_EN") mo_1 = VnicEtherIf(parent_mo_or_dn=mo, default_net="yes", name="VLAN_200") mo_2 = VnicEtherIf(parent_mo_or_dn=mo, default_net="no", name="VLAN_800") handle.add_mo(mo) handle.commit() mo = VnicLanConnTempl(parent_mo_or_dn="org-root/org-Sub_Org_Test", templ_type="updating-template", name="MGMT-B", descr="Management FI-B", stats_policy_name="default", admin_cdn_name="", switch_id="B", pin_to_group_name="", mtu="1500", policy_owner="local", qos_policy_name="",
def vnic_template_create(handle, name, vlans=[], con_policy_type=None, con_policy_name=None, mtu=1500, qos_policy_name="", target="", ident_pool_name="", nw_ctrl_policy_name="", pin_to_group_name="", switch_id="A", stats_policy_name="default", templ_type="initial-template", descr="", parent_dn="org-root"): """ Creates vNIC Template Args: handle (UcsHandle) name (String) : vNIC Template name vlans (List) : List of tuples - [(vlan_name, native_vlan)] con_policy_type (String) : Connection Policy Type ["dynamic-vnic","usnic","vmq"] con_policy_name (String) : Connection Policy name mtu (String) qos_policy_name (String) : QoS Policy name target (String) : ((vm|adaptor|defaultValue),){0,2}(vm|adaptor|defaultValue){0,1} ident_pool_name (String) : MAC Address Pool name nw_ctrl_policy_name (String) : Network Control Policy name pin_to_group_name (String) : Pin Group name switch_id (String) : ["A", "A-B", "B", "B-A", "NONE"] stats_policy_name (String) : Stats Threshold Policy name templ_type (String) : ["initial-template", "updating-template"] descr (String) : parent_dn (String) : Returns: None Example: sample_vlans = [("my_vlan","yes"),("lab_vlan","no"),("sample_vlan","no")] vnic_template_create(handle, "sample_vnic_template", sample_vlans, "usnic", "sample_usnic_policy", "1500", "samp_qos_policy", "adaptor,vm", "samp_mac_pool") """ from ucsmsdk.mometa.vnic.VnicLanConnTempl import VnicLanConnTempl from ucsmsdk.mometa.vnic.VnicDynamicConPolicyRef import VnicDynamicConPolicyRef from ucsmsdk.mometa.vnic.VnicUsnicConPolicyRef import VnicUsnicConPolicyRef from ucsmsdk.mometa.vnic.VnicVmqConPolicyRef import VnicVmqConPolicyRef from ucsmsdk.mometa.vnic.VnicEtherIf import VnicEtherIf obj = handle.query_dn(parent_dn) if obj: mo = VnicLanConnTempl(parent_mo_or_dn=obj, templ_type=templ_type, name=name, descr=descr, stats_policy_name=stats_policy_name, switch_id=switch_id, pin_to_group_name=pin_to_group_name, mtu=mtu, policy_owner="local", qos_policy_name=qos_policy_name, target=target, ident_pool_name=ident_pool_name, nw_ctrl_policy_name=nw_ctrl_policy_name) handle.add_mo(mo, True) handle.commit() # TODO: Query to check if connection policy exists if con_policy_name: if con_policy_type.lower() == "dynamic-vnic": mo_1 = VnicDynamicConPolicyRef(parent_mo_or_dn=mo, con_policy_name=con_policy_name) elif con_policy_type.lower() == "usnic": mo_1 = VnicUsnicConPolicyRef(parent_mo_or_dn=mo, con_policy_name=con_policy_name) elif con_policy_type.lower() == "vmq": mo_1 = VnicVmqConPolicyRef(parent_mo_or_dn=mo, con_policy_name=con_policy_name) else: log.info(con_policy_type + " is not a valid Connection Policy type.") vlan_mo = [] if vlans != None: for vlan in vlans: # TODO: Query to check if VnicEtherIf exists if len(vlan) != 2: raise Exception( "Invalid number of VLAN properties. Expect 2 properties. Actual:%d" % (len(vlan))) vlan_name = vlan[0] is_native_vlan = vlan[1] # TODO: Query to check if VnicEtherIf exists vlan_mo.append( VnicEtherIf(parent_mo_or_dn=mo, name=vlan_name, default_net=is_native_vlan)) handle.add_mo(mo, modify_present=True) handle.commit() else: log.info(parent_dn + " MO is not available")
x = combgen(12, 101) p = OrderedDict(sorted(x.out3.items())) p1 = {} for (sp, eth), vlan in p.items(): try: p1['sp' + str(sp)].append((eth, vlan)) except: p1['sp' + str(sp)] = [(eth, vlan)] handle = UcsHandle('10.127.97.156', 'admin', 'nbv12345') handle.login() for sp, eth_vlan in p1.items(): mo = LsServer(parent_mo_or_dn="org-root", name="x{}".format(sp)) for ev in eth_vlan: mo_1 = VnicEther(parent_mo_or_dn=mo, switch_id="A-B", ident_pool_name="default", addr="derived", name="{}".format('eth' + str(ev[0])), mtu="9000") for _ in ev[1]: VnicEtherIf(parent_mo_or_dn=mo_1, name="x{}".format(_)) handle.add_mo(mo) handle.commit() handle.logout() print "loggedout"