def main(): module = AnsibleModule(argument_spec=dict( display_name=dict(required=True, type='str'), description=dict(required=False, type='str', default=None), admin_state=dict(required=False, type='str', default='UP', choices=['UP', 'DOWN']), ip_pool_id=dict(required=False, type='str', default=None), mac_pool_id=dict(required=False, type='str', default=None), replication_mode=dict(required=False, type='str', default='MTEP', choices=['MTEP', 'SOURCE']), switching_profile_ids=dict(required=False, type='list', default=None), transport_zone_id=dict(required=True, type='str'), vlan=dict(required=False, type='int', default=None), tags=dict(required=False, type='dict', default=None), state=dict(required=False, type='str', default="present", choices=['present', 'absent']), nsx_manager=dict(required=True, type='str'), nsx_username=dict(required=True, type='str'), nsx_passwd=dict(required=True, type='str', no_log=True)), supports_check_mode=True) if not HAS_PYNSXT: module.fail_json(msg='pynsxt is required for this module') session = requests.session() session.verify = False nsx_url = 'https://%s:%s' % (module.params['nsx_manager'], 443) connector = connect.get_requests_connector(session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_std_configuration(connector) security_context = create_user_password_security_context( module.params["nsx_username"], module.params["nsx_passwd"]) connector.set_security_context(security_context) requests.packages.urllib3.disable_warnings() tags = None if module.params['tags'] is not None: tags = [] for key, value in module.params['tags'].items(): tag = Tag(scope=key, tag=value) tags.append(tag) ls_svc = LogicalSwitches(stub_config) ls = getLogicalSwitchByName(module, stub_config) if module.params['state'] == 'present': if ls is None: new_ls = LogicalSwitch( display_name=module.params['display_name'], description=module.params['description'], address_bindings=None, admin_state=module.params['admin_state'], ip_pool_id=module.params['ip_pool_id'], mac_pool_id=module.params['mac_pool_id'], replication_mode=module.params['replication_mode'], switching_profile_ids=None, transport_zone_id=module.params['transport_zone_id'], vlan=module.params['vlan'], tags=tags) if module.check_mode: module.exit_json(changed=True, debug_out=str(new_ls), id="1111") new_ls = ls_svc.create(new_ls) # # TODO: Check the realisation before exiting !!!! # module.exit_json(changed=True, object_name=module.params['display_name'], id=new_ls.id, message="Logical Switch with name %s created!" % (module.params['display_name'])) elif ls: changed = False if tags != ls.tags: changed = True ls.tags = tags if module.check_mode: module.exit_json(changed=True, debug_out=str(ls), id=ls.id) new_ls = ls_svc.update(ls.id, ls) if changed: module.exit_json( changed=True, object_name=module.params['display_name'], id=new_ls.id, message="Logical Switch with name %s has changed tags!" % (module.params['display_name'])) module.exit_json( changed=False, object_name=module.params['display_name'], id=ls.id, message="Logical Switch with name %s already exists!" % (module.params['display_name'])) elif module.params['state'] == "absent": if ls: if module.check_mode: module.exit_json(changed=True, debug_out=str(ls), id=ls.id) ls_svc.delete(ls.id) module.exit_json(changed=True, object_name=module.params['display_name'], message="Logical Switch with name %s deleted!" % (module.params['display_name'])) module.exit_json( changed=False, object_name=module.params['display_name'], message="Logical Switch with name %s does not exist!" % (module.params['display_name']))
def main(): module = AnsibleModule(argument_spec=dict( display_name=dict(required=True, type='str'), description=dict(required=False, type='str', default=None), subnets=dict(required=True, type='list'), tags=dict(required=False, type='dict', default=None), state=dict(required=False, type='str', default="present", choices=['present', 'absent']), nsx_manager=dict(required=True, type='str'), nsx_username=dict(required=True, type='str'), nsx_passwd=dict(required=True, type='str', no_log=True)), supports_check_mode=True) if not HAS_PYNSXT: module.fail_json(msg='pynsxt is required for this module') session = requests.session() session.verify = False nsx_url = 'https://%s:%s' % (module.params['nsx_manager'], 443) connector = connect.get_requests_connector(session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_std_configuration(connector) security_context = create_user_password_security_context( module.params["nsx_username"], module.params["nsx_passwd"]) connector.set_security_context(security_context) requests.packages.urllib3.disable_warnings() tags = None if module.params['tags'] is not None: tags = [] for key, value in module.params['tags'].items(): tag = Tag(scope=key, tag=value) tags.append(tag) subnet_list = [] for subnet in module.params['subnets']: ip_range_list = [] for iprange in subnet['allocation_ranges']: ipr = iprange.split('-') ip_pool_range = IpPoolRange(start=ipr[0], end=ipr[1]) ip_range_list.append(ip_pool_range) ip_pool_subnet = IpPoolSubnet( allocation_ranges=ip_range_list, cidr=subnet['cidr'], dns_nameservers=subnet['dns_nameservers'], dns_suffix=subnet['dns_suffix'], gateway_ip=subnet['gateway_ip']) subnet_list.append(ip_pool_subnet) ippool_svc = IpPools(stub_config) ippool = getIpPoolByName(module, stub_config) if module.params['state'] == 'present': if ippool is None: if module.params['state'] == "present": new_ippool = IpPool(display_name=module.params['display_name'], description=module.params['description'], subnets=subnet_list, tags=tags) if module.check_mode: module.exit_json(changed=True, debug_out=str(new_ippool), id="1111") new_ippool = ippool_svc.create(new_ippool) module.exit_json(changed=True, object_name=module.params['display_name'], id=new_ippool.id, message="IP POOL with name %s created!" % (module.params['display_name'])) elif ippool: changed = False if tags != ippool.tags: changed = True ippool.tags = tags if ippool.subnets != subnet_list: ippool.subnets = subnet_list changed = True if changed: if module.check_mode: module.exit_json(changed=True, debug_out=str(ippool), id=ippool.id) new_ippool = ippool_svc.update(ippool.id, ippool) module.exit_json(changed=True, object_name=module.params['display_name'], id=ippool.id, msg="IP Pool has been changed") module.exit_json(changed=False, object_name=module.params['display_name'], id=ippool.id, message="IP POOL with name %s already exists!" % (module.params['display_name'])) elif module.params['state'] == "absent": if ippool: if module.check_mode: module.exit_json(changed=True, debug_out=str(ippool), id=ippool.id) ippool_svc.delete(ippool.id) module.exit_json(changed=True, object_name=module.params['display_name'], message="IP POOL with name %s deleted!" % (module.params['display_name'])) module.exit_json(changed=False, object_name=module.params['display_name'], message="IP POOL with name %s does not exist!" % (module.params['display_name']))
def main(): module = AnsibleModule(argument_spec=dict( display_name=dict(required=True, type='str'), description=dict(required=False, type='str', default=None), logical_router_id=dict(required=True, type='str'), service_bindings=dict(required=False, type='str', default=None), linked_logical_switch_port_id=dict(required=True, type='str'), subnets=dict(required=True, type='list'), urpf_mode=dict(required=False, type='str', default='NONE', choices=['NONE', 'STRICT']), tags=dict(required=False, type='dict', default=None), state=dict(required=False, type='str', default="present", choices=['present', 'absent']), nsx_manager=dict(required=True, type='str'), nsx_username=dict(required=True, type='str'), nsx_passwd=dict(required=True, type='str', no_log=True)), supports_check_mode=True) if not HAS_PYNSXT: module.fail_json(msg='pynsxt is required for this module') session = requests.session() session.verify = False nsx_url = 'https://%s:%s' % (module.params['nsx_manager'], 443) connector = connect.get_requests_connector(session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_std_configuration(connector) security_context = create_user_password_security_context( module.params["nsx_username"], module.params["nsx_passwd"]) connector.set_security_context(security_context) requests.packages.urllib3.disable_warnings() tags = None if module.params['tags'] is not None: tags = [] for key, value in module.params['tags'].items(): tag = Tag(scope=key, tag=value) tags.append(tag) subnet_list = [] for subnet in module.params['subnets']: new_subnet = IPSubnet(ip_addresses=subnet['ip_addresses'], prefix_length=int(subnet['prefix_length'])) subnet_list.append(new_subnet) lrp_svc = LogicalRouterPorts(stub_config) lrp = getLogicalRouterPortByName(module, stub_config) if module.params['state'] == 'present': if lrp is None: new_lrp = LogicalRouterDownLinkPort( display_name=module.params['display_name'], description=module.params['description'], subnets=subnet_list, linked_logical_switch_port_id=ResourceReference( target_id=module.params['linked_logical_switch_port_id']), logical_router_id=module.params['logical_router_id'], service_bindings=None, urpf_mode=module.params['urpf_mode'], tags=tags) if module.check_mode: module.exit_json(changed=True, debug_out=str(new_lrp), id="1111") new_lrp_temp = lrp_svc.create(new_lrp) new_lrp = new_lrp_temp.convert_to(LogicalRouterDownLinkPort) module.exit_json( changed=True, object_name=module.params['display_name'], id=new_lrp.id, message="Logical Router Port with name %s created!" % (module.params['display_name'])) elif lrp: changed = False if tags != lrp.tags: changed = True lrp.tags = tags if subnet_list != lrp.subnets: changed = True lrp.subnets = subnet_list if changed: if module.check_mode: module.exit_json(changed=True, debug_out=str(lrp), id=lrp.id) new_lrp = lrp_svc.update(lrp.id, lrp) module.exit_json( changed=True, object_name=module.params['display_name'], id=lrp.id, message="Logical Router Port with name %s has changed tags!" % (module.params['display_name'])) module.exit_json( changed=False, object_name=module.params['display_name'], id=lrp.id, message="Logical Router Port with name %s already exists!" % (module.params['display_name'])) elif module.params['state'] == "absent": if lrp: if module.check_mode: module.exit_json(changed=True, debug_out=str(lrp), id=lrp.id) lrp_svc.delete(lrp.id) module.exit_json( changed=True, object_name=module.params['display_name'], message="Logical Router Port with name %s deleted!" % (module.params['display_name'])) module.exit_json( changed=False, object_name=module.params['display_name'], message="Logical Router Port with name %s does not exist!" % (module.params['display_name']))
def main(): module = AnsibleModule(argument_spec=dict( display_name=dict(required=True, type='str'), description=dict(required=False, type='str', default=None), members=dict(required=False, type='list', default=None), tags=dict(required=False, type='dict', default=None), state=dict(required=False, type='str', default="present", choices=['present', 'absent']), nsx_manager=dict(required=True, type='str'), nsx_username=dict(required=True, type='str'), nsx_passwd=dict(required=True, type='str', no_log=True)), supports_check_mode=True) if not HAS_PYNSXT: module.fail_json(msg='pynsxt is required for this module') session = requests.session() session.verify = False nsx_url = 'https://%s:%s' % (module.params['nsx_manager'], 443) connector = connect.get_requests_connector(session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_std_configuration(connector) security_context = create_user_password_security_context( module.params["nsx_username"], module.params["nsx_passwd"]) connector.set_security_context(security_context) requests.packages.urllib3.disable_warnings() tags = None if module.params['tags'] is not None: tags = [] for key, value in module.params['tags'].items(): tag = Tag(scope=key, tag=value) tags.append(tag) member_list = [] if module.params['members']: for tnode_name in module.params['members']: member = getTransportNodeByName(tnode_name, module, stub_config) edgeClusterMember = EdgeClusterMember(transport_node_id=member.id) member_list.append(edgeClusterMember) elif module.params['members'] is None: member_list = None ec_svc = EdgeClusters(stub_config) ec = getEdgeClusterByName(module, stub_config) if module.params['state'] == 'present': if ec is None: new_ec = EdgeCluster(display_name=module.params['display_name'], description=module.params['description'], members=member_list, tags=tags) if module.check_mode: module.exit_json(changed=True, debug_out=str(new_ec), id="1111") new_ec = ec_svc.create(new_ec) module.exit_json(changed=True, object_name=module.params['display_name'], id=new_ec.id, message="Edge Cluster with name %s created!" % (module.params['display_name'])) elif ec: changed = False if tags != ec.tags: ec.tags = tags changed = True desiredList = [] realisedList = [] if module.params['members']: desiredList = simplifyClusterMembersList(member_list) if ec.members: realisedList = simplifyClusterMembersList(ec.members) if desiredList != realisedList: ec.members = member_list changed = True if changed: if module.check_mode: module.exit_json(changed=True, debug_out=str(ec), id=ec.id) new_ec = ec_svc.update(ec.id, ec) module.exit_json( changed=True, object_name=module.params['display_name'], id=new_ec.id, message="Edge Cluster with name %s has changed tags!" % (module.params['display_name'])) module.exit_json( changed=False, object_name=module.params['display_name'], id=ec.id, message="Edge Cluster with name %s already exists!" % (module.params['display_name'])) elif module.params['state'] == "absent": if ec: if module.check_mode: module.exit_json(changed=True, debug_out=str(ec)) ec_svc.delete(ec.id) module.exit_json(changed=True, object_name=module.params['display_name'], message="Edge Cluster with name %s deleted!" % (module.params['display_name'])) if module.check_mode: module.exit_json(changed=False, debug_out="no Edge Cluster with name %s" % (module.params['display_name'])) module.exit_json(changed=False, object_name=module.params['display_name'], message="Edge Cluster with name %s does not exist!" % (module.params['display_name']))
def main(): module = AnsibleModule(argument_spec=dict( network=dict(required=True, type='str'), description=dict(required=False, type='str', default=None), next_hops=dict(required=True, type='list', default=None), admin_distance=dict(required=False, type='int', default=1), router_name=dict(required=False, type='str', default=None), router_id=dict(required=False, type='str', default=None), tags=dict(required=False, type='dict', default=None), state=dict(required=False, type='str', default="present", choices=['present', 'absent']), nsx_manager=dict(required=True, type='str'), nsx_username=dict(required=True, type='str'), nsx_passwd=dict(required=True, type='str', no_log=True)), supports_check_mode=True) if not HAS_PYNSXT: module.fail_json(msg='pynsxt is required for this module') session = requests.session() session.verify = False nsx_url = 'https://%s:%s' % (module.params['nsx_manager'], 443) connector = connect.get_requests_connector(session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_std_configuration(connector) security_context = create_user_password_security_context( module.params["nsx_username"], module.params["nsx_passwd"]) connector.set_security_context(security_context) requests.packages.urllib3.disable_warnings() tags = None if module.params['tags'] is not None: tags = [] for key, value in module.params['tags'].items(): tag = Tag(scope=key, tag=value) tags.append(tag) lrid = "" if module.params['router_id']: lrid = module.params['router_id'] elif module.params['router_name']: lr_svc = LogicalRouters(stub_config) lr = getLogicalRouterByName(module, stub_config) lrid = lr.id sroute = getStaticRouteByNetwork(module, stub_config, lrid) next_hop_list = [] for next_hop in module.params['next_hops']: staticRouteNextHop = StaticRouteNextHop( administrative_distance=module.params['admin_distance'], ip_address=next_hop, logical_router_port_id=None) next_hop_list.append(staticRouteNextHop) sr_svc = StaticRoutes(stub_config) if module.params['state'] == 'present': if sroute is None: new_static_route = StaticRoute( display_name=None, network=module.params['network'], next_hops=next_hop_list, description=module.params['description'], tags=tags) if module.check_mode: module.exit_json(changed=True, debug_out=str(new_static_route), id="1111") try: new_static_route = sr_svc.create(lrid, new_static_route) module.exit_json( changed=True, object_name=module.params['network'], id=new_static_route.id, message= "Static Route with for %s with id %s was created on router with id %s!" % (module.params['network'], new_static_route.id, lrid)) except Error as ex: module.fail_json(msg='API Error creating Static Route: %s' % (str(ex))) elif sroute: changed = False if tags != sroute.tags: sroute.tags = tags changed = True nhopList1 = simplifyNextHopList(sroute.next_hops) nhopList2 = simplifyNextHopList(next_hop_list) if nhopList1 != nhopList2: sroute.next_hops = next_hop_list changed = True if changed: if module.check_mode: module.exit_json(changed=True, debug_out=str(sroute), id=lrid) new_static_route = sr_svc.update(lrid, sroute.id, sroute) module.exit_json( changed=True, object_name=module.params['network'], id=new_static_route.id, message="Static Route for %s has changed tags!" % (module.params['network'])) module.exit_json(changed=False, object_name=module.params['network'], id=sroute.id, router_id=lrid, message="Static Route for %s already exists!" % (module.params['network'])) elif module.params['state'] == "absent": if sroute: if module.check_mode: module.exit_json(changed=True, debug_out=str(sroute), id=lrid) try: sr_svc.delete(lrid, sroute.id) module.exit_json(changed=True, object_name=module.params['network'], message="Static Route for %s deleted!" % (module.params['network'])) except Error as ex: api_error = ex.date.convert_to(ApiError) module.fail_json(msg='API Error deleting Logical Routers: %s' % (api_error.error_message)) module.exit_json(changed=False, object_name=module.params['network'], message="Static Route for %s does not exist!" % (module.params['network']))
def main(): module = AnsibleModule(argument_spec=dict( display_name=dict(required=True, type='str'), description=dict(required=False, type='str', default=None), host_switch_mode=dict(required=False, type='str', default='STANDARD', choices=['STANDARD', 'ENS']), host_switch_name=dict(required=True, type='str'), nested_nsx=dict(required=False, type='bool', default=False), transport_type=dict(required=False, type='str', default='OVERLAY', choices=['OVERLAY', 'VLAN']), tags=dict(required=False, type='dict', default=None), state=dict(required=False, type='str', default="present", choices=['present', 'absent']), nsx_manager=dict(required=True, type='str'), nsx_username=dict(required=True, type='str'), nsx_passwd=dict(required=True, type='str', no_log=True)), supports_check_mode=True) if not HAS_PYNSXT: module.fail_json(msg='pynsxt is required for this module') session = requests.session() session.verify = False nsx_url = 'https://%s:%s' % (module.params['nsx_manager'], 443) connector = connect.get_requests_connector(session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_std_configuration(connector) security_context = create_user_password_security_context( module.params["nsx_username"], module.params["nsx_passwd"]) connector.set_security_context(security_context) requests.packages.urllib3.disable_warnings() #tags=None tags = [] tags.append( Tag(scope='created-by', tag=os.getenv("NSX_T_INSTALLER", "nsx-t-gen"))) if module.params['tags'] is not None: #tags = [] for key, value in module.params['tags'].items(): tag = Tag(scope=key, tag=value) tags.append(tag) transportzones_svc = TransportZones(stub_config) tz = getTransportZoneByName(module, stub_config) if module.params['state'] == 'present': if tz is None: tags.append( Tag(scope='generated', tag=time.strftime("%Y-%m-%d %H:%M:%S %z"))) new_tz = TransportZone( transport_type=module.params['transport_type'], display_name=module.params['display_name'], description=module.params['description'], host_switch_name=module.params['host_switch_name'], host_switch_mode=module.params['host_switch_mode'], nested_nsx=module.params['nested_nsx'], tags=tags) if module.check_mode: module.exit_json(changed=True, debug_out=str(new_tz), id="1111") new_tz = transportzones_svc.create(new_tz) module.exit_json(changed=True, object_name=module.params['display_name'], id=new_tz.id, message="Transport Zone with name %s created!" % (module.params['display_name'])) elif tz: #if tags != tz.tags: if not compareTags(tz.tags, tags): tags.append(findTag(tz.tags, 'generated')) tags.append( Tag(scope='modified', tag=time.strftime("%Y-%m-%d %H:%M:%S %z"))) tz.tags = tags if module.check_mode: module.exit_json(changed=True, debug_out=str(tz), id=tz.id) new_tz = transportzones_svc.update(tz.id, tz) module.exit_json( changed=True, object_name=module.params['display_name'], id=new_tz.id, message="Transport Zone with name %s has changed tags!" % (module.params['display_name'])) module.exit_json( changed=False, object_name=module.params['display_name'], id=tz.id, message="Transport Zone with name %s already exists!" % (module.params['display_name'])) elif module.params['state'] == "absent": if tz: if module.check_mode: module.exit_json(changed=True, debug_out=str(tz), id=tz.id) transportzones_svc.delete(tz.id) module.exit_json(changed=True, object_name=module.params['display_name'], message="Transport Zone with name %s deleted!" % (module.params['display_name'])) module.exit_json(changed=False, object_name=module.params['display_name'], message="Transport Zone with name %s doe not exist!" % (module.params['display_name']))
def main(): module = AnsibleModule(argument_spec=dict( display_name=dict(required=True, type='str'), description=dict(required=False, type='str', default=None), failover_mode=dict(required=False, type='str', default=None, choices=['NON_PREEMPTIVE', 'PREEMPTIVE']), edge_cluster_id=dict(required=False, type='str', default=None), high_availability_mode=dict( required=False, type='str', default='ACTIVE_STANDBY', choices=['ACTIVE_STANDBY', 'ACTIVE_ACTIVE']), tags=dict(required=False, type='dict', default=None), state=dict(required=False, type='str', default="present", choices=['present', 'absent']), nsx_manager=dict(required=True, type='str'), nsx_username=dict(required=True, type='str'), nsx_passwd=dict(required=True, type='str', no_log=True)), supports_check_mode=True) if not HAS_PYNSXT: module.fail_json(msg='pynsxt is required for this module') session = requests.session() session.verify = False nsx_url = 'https://%s:%s' % (module.params['nsx_manager'], 443) connector = connect.get_requests_connector(session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_std_configuration(connector) security_context = create_user_password_security_context( module.params["nsx_username"], module.params["nsx_passwd"]) connector.set_security_context(security_context) requests.packages.urllib3.disable_warnings() #tags=None tags = [] tags.append( Tag(scope='created-by', tag=os.getenv("NSX_T_INSTALLER", "nsx-t-gen"))) if module.params['tags'] is not None: #tags = [] for key, value in module.params['tags'].items(): tag = Tag(scope=key, tag=value) tags.append(tag) lr_svc = LogicalRouters(stub_config) lr = getLogicalRouterByName(module, stub_config) if module.params['state'] == 'present': if lr is None: tags.append( Tag(scope='generated', tag=time.strftime("%Y-%m-%d %H:%M:%S %z"))) new_lr = LogicalRouter( display_name=module.params['display_name'], description=module.params['description'], failover_mode=module.params['failover_mode'], edge_cluster_id=module.params['edge_cluster_id'], router_type='TIER0', high_availability_mode=module.params['high_availability_mode'], tags=tags) if module.check_mode: module.exit_json(changed=True, debug_out=str(new_lr), id="1111") try: new_lr = lr_svc.create(new_lr) module.exit_json( changed=True, object_name=module.params['display_name'], id=new_lr.id, message="Logical Router with name %s created!" % (module.params['display_name'])) except Error as ex: module.fail_json(msg='API Error listing Logical Routers: %s' % (str(ex))) elif lr: #if tags != lr.tags: if not compareTags(lr.tags, tags): tags.append(findTag(lr.tags, 'generated')) tags.append( Tag(scope='modified', tag=time.strftime("%Y-%m-%d %H:%M:%S %z"))) lr.tags = tags if module.check_mode: module.exit_json(changed=True, debug_out=str(lr), id=lr.id) new_lr = lr_svc.update(lr.id, lr) module.exit_json( changed=True, object_name=module.params['display_name'], id=new_lr.id, message="Logical Router with name %s has changed tags!" % (module.params['display_name'])) module.exit_json( changed=False, object_name=module.params['display_name'], id=lr.id, message="Logical Router with name %s already exists!" % (module.params['display_name'])) elif module.params['state'] == "absent": if lr: if module.check_mode: module.exit_json(changed=True, debug_out=str(lr), id=lr.id) try: deleteAllPortsOnRouter(lr, module, stub_config) lr_svc.delete(lr.id) except Error as ex: api_error = ex.date.convert_to(ApiError) module.fail_json(msg='API Error deleting Logical Routers: %s' % (api_error.error_message)) module.exit_json(changed=True, object_name=module.params['display_name'], message="Logical Router with name %s deleted!" % (module.params['display_name'])) module.exit_json( changed=False, object_name=module.params['display_name'], message="Logical Router with name %s does not exist!" % (module.params['display_name']))
def main(): module = AnsibleModule(argument_spec=dict( display_name=dict(required=True, type='str'), description=dict(required=False, type='str', default=None), edge_cluster_member=dict(required=True, type='str'), ip_address=dict(required=True, type='str'), t0_router=dict(required=True, type='str'), logical_switch_port_id=dict(required=True, type='str'), urpf=dict(required=False, type='str', default='NONE', choices=['NONE', 'STRICT']), tags=dict(required=False, type='dict', default=None), state=dict(required=False, type='str', default="present", choices=['present', 'absent']), nsx_manager=dict(required=True, type='str'), nsx_username=dict(required=True, type='str'), nsx_passwd=dict(required=True, type='str', no_log=True)), supports_check_mode=True) if not HAS_PYNSXT: module.fail_json(msg='pynsxt is required for this module') session = requests.session() session.verify = False nsx_url = 'https://%s:%s' % (module.params['nsx_manager'], 443) connector = connect.get_requests_connector(session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_std_configuration(connector) security_context = create_user_password_security_context( module.params["nsx_username"], module.params["nsx_passwd"]) connector.set_security_context(security_context) requests.packages.urllib3.disable_warnings() tags = None if module.params['tags'] is not None: tags = [] for key, value in module.params['tags'].items(): tag = Tag(scope=key, tag=value) tags.append(tag) lr_svc = LogicalRouters(stub_config) lr = getLogicalRouterByName(module, stub_config) ec_svc = EdgeClusters(stub_config) edgeCluster = ec_svc.get(lr.edge_cluster_id) tn = getTransportNodeByName(module, stub_config) member_index = [] for member in edgeCluster.members: if member.transport_node_id == tn.id: member_index.append(int(member.member_index)) subnet_list = [] subnet = module.params['ip_address'].split('/') new_subnet = IPSubnet(ip_addresses=[subnet[0]], prefix_length=int(subnet[1])) subnet_list.append(new_subnet) lrp_svc = LogicalRouterPorts(stub_config) lrp = getLogicalRouterPortByName(module, stub_config, lr.id) if module.params['state'] == 'present': if lrp is None: new_lrp = LogicalRouterUpLinkPort( display_name=module.params['display_name'], description=module.params['description'], edge_cluster_member_index=member_index, linked_logical_switch_port_id=ResourceReference( target_id=module.params['logical_switch_port_id']), subnets=subnet_list, urpf_mode=module.params['urpf'], logical_router_id=lr.id, tags=tags) if module.check_mode: module.exit_json(changed=True, debug_out=str(new_lrp), id="1111") try: lrp_temp = lrp_svc.create(new_lrp) new_lrp = lrp_temp.convert_to(LogicalRouterUpLinkPort) module.exit_json( changed=True, object_name=module.params['display_name'], id=new_lrp.id, message="Logical Router Port with name %s created!" % (module.params['display_name'])) except Error as ex: api_error = ex.data.convert_to(ApiError) module.fail_json( msg='API Error creating Logical Router Uplink: %s' % (str(api_error.error_message))) elif lrp: changed = False if lrp.linked_logical_switch_port_id.target_id != module.params[ 'logical_switch_port_id']: changed = True lrp.linked_logical_switch_port_id = ResourceReference( target_id=module.params['logical_switch_port_id']) if tags != lrp.tags: changed = True lrp.tags = tags if subnet_list != lrp.subnets: changed = True lrp.subnets = subnet_list if member_index != lrp.edge_cluster_member_index: changed = True lrp.edge_cluster_member_index = member_index if changed: if module.check_mode: module.exit_json(changed=True, debug_out=str(lrp), id=lrp.id) new_lr = lrp_svc.update(lrp.id, lrp) module.exit_json( changed=True, object_name=module.params['display_name'], id=lrp.id, message= "Logical Router Uplink with name %s has been changed!" % (module.params['display_name'])) module.exit_json( changed=False, object_name=module.params['display_name'], id=lrp.id, message="Logical Router Uplink with name %s already exists!" % (module.params['display_name'])) elif module.params['state'] == "absent": if lrp: if module.check_mode: module.exit_json(changed=True, debug_out=str(lrp), id=lrp.id) try: lrp_svc.delete(lrp.id, force=True) except Error as ex: api_error = ex.date.convert_to(ApiError) module.fail_json( msg='API Error deleting Logical Router Ports: %s' % (api_error.error_message)) module.exit_json( changed=True, object_name=module.params['display_name'], message="Logical Router Port with name %s deleted!" % (module.params['display_name'])) module.exit_json( changed=False, object_name=module.params['display_name'], message="Logical Router Port with name %s does not exist!" % (module.params['display_name']))
def main(): module = AnsibleModule(argument_spec=dict( display_name=dict(required=True, type='str'), description=dict(required=False, type='str', default=None), cidr=dict(required=True, type='str'), tags=dict(required=False, type='dict', default=None), state=dict(required=False, type='str', default="present", choices=['present', 'absent']), nsx_manager=dict(required=True, type='str'), nsx_username=dict(required=True, type='str'), nsx_passwd=dict(required=True, type='str', no_log=True)), supports_check_mode=True) if not HAS_PYNSXT: module.fail_json(msg='pynsxt is required for this module') session = requests.session() session.verify = False nsx_url = 'https://%s:%s' % (module.params['nsx_manager'], 443) connector = connect.get_requests_connector(session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_std_configuration(connector) security_context = create_user_password_security_context( module.params["nsx_username"], module.params["nsx_passwd"]) connector.set_security_context(security_context) requests.packages.urllib3.disable_warnings() tags = None if module.params['tags'] is not None: tags = [] for key, value in module.params['tags'].items(): tag = Tag(scope=key, tag=value) tags.append(tag) ipblock_svc = IpBlocks(stub_config) ipblock = getIpBlockByName(module, stub_config) if module.params['state'] == 'present': if ipblock is None: if module.params['state'] == "present": new_ipblock = IpBlock( display_name=module.params['display_name'], description=module.params['description'], cidr=module.params['cidr'], tags=tags) if module.check_mode: module.exit_json(changed=True, debug_out=str(new_ipblock), id="1111") new_ipblock = ipblock_svc.create(new_ipblock) module.exit_json(changed=True, object_name=module.params['display_name'], id=new_ipblock.id, message="IP BLOCK with name %s created!" % (module.params['display_name'])) elif ipblock: changed = False if tags != ipblock.tags: changed = True ipblock.tags = tags if ipblock.cidr != module.params['cidr']: ipblock.cidr = module.params['cidr'] changed = True if changed: if module.check_mode: module.exit_json(changed=True, debug_out=str(ipblock), id=ipblock.id) new_ipblock = ipblock_svc.update(ipblock.id, ipblock) module.exit_json(changed=True, object_name=module.params['display_name'], id=ipblock.id, msg="IP Block has been changed") module.exit_json(changed=False, object_name=module.params['display_name'], id=ipblock.id, message="IP Block with name %s already exists!" % (module.params['display_name'])) elif module.params['state'] == "absent": if ipblock: if module.check_mode: module.exit_json(changed=True, debug_out=str(ipblock), id=ipblock.id) ipblock_svc.delete(ipblock.id) module.exit_json(changed=True, object_name=module.params['display_name'], message="IP Block with name %s deleted!" % (module.params['display_name'])) module.exit_json(changed=False, object_name=module.params['display_name'], message="IP Block with name %s does not exist!" % (module.params['display_name']))
def main(): module = AnsibleModule(argument_spec=dict( display_name=dict(required=True, type='str'), description=dict(required=False, type='str', default=None), mtu=dict(required=False, type='int', default=1600), lags=dict(required=False, type='list', default=None), active_list=dict(required=True, type='list'), standby_list=dict(required=False, type='list', default=None), policy=dict(required=True, type='str', choices=['FAILOVER_ORDER', 'LOADBALANCE_SRCID']), transport_vlan=dict(required=True, type='int'), tags=dict(required=False, type='dict', default=None), state=dict(required=False, type='str', default="present", choices=['present', 'absent']), nsx_manager=dict(required=True, type='str'), nsx_username=dict(required=True, type='str'), nsx_passwd=dict(required=True, type='str', no_log=True)), supports_check_mode=True) if not HAS_PYNSXT: module.fail_json(msg='pynsxt is required for this module') session = requests.session() session.verify = False nsx_url = 'https://%s:%s' % (module.params['nsx_manager'], 443) connector = connect.get_requests_connector(session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_std_configuration(connector) security_context = create_user_password_security_context( module.params["nsx_username"], module.params["nsx_passwd"]) connector.set_security_context(security_context) requests.packages.urllib3.disable_warnings() tags = None if module.params['tags'] is not None: tags = [] for key, value in module.params['tags'].items(): tag = Tag(scope=key, tag=value) tags.append(tag) if module.params['policy'] == 'LOADBALANCE_SRCID' and module.params[ 'standby_list']: module.fail_json( msg= 'With LOADBALANCE_SRCID teaming policy the StandBy List must NOT be defined!!!' ) uplink_type = Uplink.UPLINK_TYPE_PNIC # if module.params['lags']: # uplink_type=Uplink.UPLINK_TYPE_LAG active_list = [] for active_uplink in module.params['active_list']: uplink = Uplink(active_uplink, uplink_type) active_list.append(uplink) lag_list = None # if module.params['lags']: # lag_list = createListOfLags(module, stub_config, active_list) standby_list = None if module.params['standby_list']: standby_list = [] for standby_uplink in module.params['standby_list']: uplink = Uplink(standby_uplink, uplink_type) standby_list.append(uplink) teaming = TeamingPolicy(active_list, module.params['policy'], standby_list) hs_profile_svc = HostSwitchProfiles(stub_config) prof = getProfileByName(module, stub_config) if module.params['state'] == 'present': if prof is None: new_prof = UplinkHostSwitchProfile( display_name=module.params['display_name'], description=module.params['description'], lags=lag_list, mtu=module.params['mtu'], teaming=teaming, transport_vlan=module.params['transport_vlan'], tags=tags) if module.check_mode: module.exit_json(changed=True, debug_out=str(new_prof), id="1111") try: new_prof = hs_profile_svc.create(new_prof) except Error as ex: module.fail_json( msg='API Error listing Hostswitch Profiles: %s' % (str(ex))) created_prof = getProfileByName(module, stub_config) module.exit_json(changed=True, object_name=module.params['display_name'], id=created_prof.id, message="Uplink Profile with name %s created!" % (module.params['display_name'])) elif prof: changed = False if tags != prof.tags: changed = True prof.tags = tags if prof.teaming != teaming: prof.teaming = teaming changed = True if prof.mtu != module.params['mtu']: prof.mtu = module.params['mtu'] changed = True if prof.transport_vlan != module.params['transport_vlan']: prof.transport_vlan = module.params['transport_vlan'] changed = True # if prof.lags != lag_list: # prof.lags = lag_list # changed = True if changed: if module.check_mode: module.exit_json(changed=True, debug_out=str(prof), id=prof.id) new_prof = hs_profile_svc.update(prof.id, prof) module.exit_json(changed=True, object_name=module.params['display_name'], id=prof.id, msg="Uplink Profile has been changed") module.exit_json( changed=False, object_name=module.params['display_name'], id=prof.id, message="Uplink Profile with name %s already exists!" % (module.params['display_name'])) elif module.params['state'] == "absent": if prof: if module.check_mode: module.exit_json(changed=True, debug_out=str(prof), id=prof.id) hs_profile_svc.delete(prof.id) module.exit_json(changed=True, object_name=module.params['display_name'], message="Uplink Profile with name %s deleted!" % (module.params['display_name'])) module.exit_json( changed=False, object_name=module.params['display_name'], message="Uplink Profile with name %s does not exist!" % (module.params['display_name']))
def main(): module = AnsibleModule(argument_spec=dict( display_name=dict(required=True, type='str'), description=dict(required=False, type='str', default=None), admin_state=dict(required=False, type='str', default='UP', choices=['UP', 'DOWN']), logical_switch_id=dict(required=True, type='str'), switching_profile_ids=dict(required=False, type='list', default=None), tags=dict(required=False, type='dict', default=None), state=dict(required=False, type='str', default="present", choices=['present', 'absent']), nsx_manager=dict(required=True, type='str'), nsx_username=dict(required=True, type='str'), nsx_passwd=dict(required=True, type='str', no_log=True)), supports_check_mode=True) if not HAS_PYNSXT: module.fail_json(msg='pynsxt is required for this module') session = requests.session() session.verify = False nsx_url = 'https://%s:%s' % (module.params['nsx_manager'], 443) connector = connect.get_requests_connector(session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_std_configuration(connector) security_context = create_user_password_security_context( module.params["nsx_username"], module.params["nsx_passwd"]) connector.set_security_context(security_context) requests.packages.urllib3.disable_warnings() #tags=None tags = [] tags.append( Tag(scope='created-by', tag=os.getenv("NSX_T_INSTALLER", "nsx-t-gen"))) if module.params['tags'] is not None: #tags = [] for key, value in module.params['tags'].items(): tag = Tag(scope=key, tag=value) tags.append(tag) lsp_svc = LogicalPorts(stub_config) lsp = getLogicalSwitchPortByName(module, stub_config) if module.params['state'] == 'present': if lsp is None: tags.append( Tag(scope='generated', tag=time.strftime("%Y-%m-%d %H:%M:%S %z"))) new_lsp = LogicalPort( display_name=module.params['display_name'], description=module.params['description'], address_bindings=None, admin_state=module.params['admin_state'], attachment=None, logical_switch_id=module.params['logical_switch_id'], switching_profile_ids=None, tags=tags) if module.check_mode: module.exit_json(changed=True, debug_out=str(new_lsp), id="1111") new_lsp = lsp_svc.create(new_lsp) module.exit_json( changed=True, object_name=module.params['display_name'], id=new_lsp.id, message="Logical Switch Port with name %s created!" % (module.params['display_name'])) elif lsp: changed = False #if tags != lsp.tags: if not compareTags(lsp.tags, tags): changed = True tags.append(findTag(lsp.tags, 'generated')) tags.append( Tag(scope='modified', tag=time.strftime("%Y-%m-%d %H:%M:%S %z"))) lsp.tags = tags if module.check_mode: module.exit_json(changed=True, debug_out=str(lsp), id=lsp.id) new_lsp = lsp_svc.update(lsp.id, lsp) if changed: module.exit_json( changed=True, object_name=module.params['display_name'], id=new_lsp.id, message="Logical Switch Port with name %s has changed tags!" % (module.params['display_name'])) module.exit_json( changed=False, object_name=module.params['display_name'], id=lsp.id, message="Logical Switch Port with name %s already exists!" % (module.params['display_name'])) elif module.params['state'] == "absent": if lsp: if module.check_mode: module.exit_json(changed=True, debug_out=str(lsp), id=lsp.id) lsp_svc.delete(lsp.id) module.exit_json( changed=True, object_name=module.params['display_name'], message="Logical Switch Port with name %s deleted!" % (module.params['display_name'])) module.exit_json( changed=False, object_name=module.params['display_name'], message="Logical Switch Port with name %s does not exist!" % (module.params['display_name']))
def main(): module = AnsibleModule(argument_spec=dict( display_name=dict(required=True, type='str'), cm_name=dict(required=False, type='str', default=None), auto_install_nsx=dict(required=False, type='bool', default=True), create_transport_node=dict(required=False, type='bool', default=True), host_switch=dict(required=True, type='list'), transport_zone_endpoints=dict(required=False, type='list'), tags=dict(required=False, type='dict', default=None), state=dict(required=False, type='str', default="present", choices=['present', 'absent']), nsx_manager=dict(required=True, type='str'), nsx_username=dict(required=True, type='str'), nsx_passwd=dict(required=True, type='str', no_log=True)), supports_check_mode=True) if not HAS_PYNSXT: module.fail_json(msg='pynsxt is required for this module') session = requests.session() session.verify = False nsx_url = 'https://%s:%s' % (module.params['nsx_manager'], 443) connector = connect.get_requests_connector(session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_std_configuration(connector) security_context = create_user_password_security_context( module.params["nsx_username"], module.params["nsx_passwd"]) connector.set_security_context(security_context) requests.packages.urllib3.disable_warnings() tags = None if module.params['tags'] is not None: tags = [] for key, value in module.params['tags'].items(): tag = Tag(scope=key, tag=value) tags.append(tag) ccft_svc = ComputeCollectionFabricTemplates(stub_config) cc = getClusterByName(module, stub_config) ft_list = getFabricTemplates(module, stub_config, cc) cctnt_svc = ComputeCollectionTransportNodeTemplates(stub_config) if module.params['state'] == 'present': if ft_list.result_count == 1: changed = False if ft_list.results[0].auto_install_nsx != module.params[ 'auto_install_nsx']: ft_list.results[0].auto_install_nsx = module.params[ 'auto_install_nsx'] if module.check_mode: module.exit_json(changed=True, msg="Cluster auto config will be updated", id="1111") ccft_svc.update(ft_list.results[0].id, ft_list.results[0]) changed = True if not module.params['create_transport_node']: try: cctnt_list = cctnt_svc.list( compute_collection_id=cc.external_id) if module.check_mode: module.exit_json( changed=True, msg="Cluster auto config will be updated", id="1111") cctnt_svc.delete(cctnt_list.results[0].id) module.exit_json( changed=True, msg="Automatic Transport Node Creation is disabled") except AttributeError: module.exit_json(changed=False, msg="TN Template is already disabling") if module.params['create_transport_node']: changed = createTnTemplate(module, stub_config, cc) if changed: module.exit_json( changed=True, msg='Template for cluster %s has been updated!' % (module.params['display_name'])) module.exit_json(changed=False, msg='Template for cluster %s already exists!' % (module.params['display_name'])) elif ft_list.result_count == 0: ccft = ComputeCollectionFabricTemplate( display_name='ccFabTempl-%s' % (module.params['display_name']), description=None, auto_install_nsx=module.params['auto_install_nsx'], compute_collection_id=cc.external_id, tags=tags) if module.check_mode: module.exit_json(changed=True, msg="Cluster auto config will be crated", id="1111") new_ccft = ccft_svc.create(ccft) if module.params['create_transport_node']: changed = createTnTemplate(module, stub_config, cc) module.exit_json(changed=True, object_name=str(new_ccft), id=new_ccft.id, msg='Template for cluster %s created!' % (module.params['display_name'])) elif module.params['state'] == "absent": if ft_list.result_count == 1: if module.check_mode: module.exit_json(changed=True, msg="Cluster auto config will be deleted", id="1111") try: cctnt_list = cctnt_svc.list( compute_collection_id=cc.external_id) cctnt_svc.delete(cctnt_list.results[0].id) except AttributeError: pass ccft_svc.delete(ft_list.results[0].id) module.exit_json(changed=True, object_out=str(ft_list.results[0]), msg='Template for cluster %s deleted!' % (module.params['display_name'])) elif ft_list.result_count == 0: module.exit_json( changed=False, msg='No Template Configuration for Cluster %s found!' % (module.params['display_name']))
def main(): args = getargs.getargs() stub_config = auth.get_session_auth_stub_config(args.user, args.password, args.nsx_host, args.tcp_port) # Create a pretty printer to make the output look nice. pp = PrettyPrinter() # Create the services we'll need. transportzones_svc = TransportZones(stub_config) logicalswitches_svc = LogicalSwitches(stub_config) logicalports_svc = LogicalPorts(stub_config) nsgroups_svc = NsGroups(stub_config) efflports_svc = EffectiveLogicalPortMembers(stub_config) # Create a transport zone and logical switch. We only # need these so we can create logical ports. They aren't # part of the demo. tz = TransportZone(transport_type=TransportZone.TRANSPORT_TYPE_OVERLAY, display_name="Tagging Demo Transport Zone", description="Transport zone for tagging demo", host_switch_name="hostswitch") tz = transportzones_svc.create(tz) ls = LogicalSwitch( transport_zone_id=tz.id, admin_state=LogicalSwitch.ADMIN_STATE_UP, replication_mode=LogicalSwitch.REPLICATION_MODE_MTEP, display_name="ls-tag-demo", ) ls = logicalswitches_svc.create(ls) # First, create a new group whose members are any logical # ports with a tag scope of "color" and tag value of "green" group = NSGroup( display_name="Green Logical Ports", description="All logical ports with scope=color and tag=green", membership_criteria=[ NSGroupTagExpression(resource_type="NSGroupTagExpression", target_type="LogicalPort", scope_op="EQUALS", scope="color", tag_op="EQUALS", tag="green") ]) green_group = nsgroups_svc.create(group) # Now create another group for color:yellow logical ports. group = NSGroup( display_name="Yellow Logical Ports", description="All logical ports with scope=color and tag=yellow", membership_criteria=[ NSGroupTagExpression(resource_type="NSGroupTagExpression", target_type="LogicalPort", scope_op="EQUALS", scope="color", tag_op="EQUALS", tag="yellow") ]) yellow_group = nsgroups_svc.create(group) # Now get the list of effective members (that is, logical ports # with color:green). There should be no effective members yet. print("Before creating any lports:") print_group_effective_members("green group", efflports_svc.list(green_group.id)) print_group_effective_members("yellow group", efflports_svc.list(yellow_group.id)) print("") # Create a logical port with color:green lport = LogicalPort(display_name="tagging-demo-lp1", admin_state=LogicalPort.ADMIN_STATE_UP, logical_switch_id=ls.id, tags=[Tag(scope="color", tag="green")]) lport = logicalports_svc.create(lport) print("Logical port for this test has id %s" % lport.id) print("") # Group membership is computed asynchronously in the NSX # manager. Wait a bit. time.sleep(2.0) # Find the effective members of the green and yellow groups. # Notice that the logical port is in the green group. print("After creating green lport:") print_group_effective_members("green group", efflports_svc.list(green_group.id)) print_group_effective_members("yellow group", efflports_svc.list(yellow_group.id)) print("") # Now modify the logical port's color to yellow. lport = logicalports_svc.get(lport.id) lport.tags = [Tag(scope="color", tag="yellow")] logicalports_svc.update(lport.id, lport) # Wait for group recalculation time.sleep(2.0) # Find the effective members of the green and yellow groups. # Notice that the logical port is now in the yellow group # and no longer in the green group. print("After changing lport color to yellow:") print_group_effective_members("green group", efflports_svc.list(green_group.id)) print_group_effective_members("yellow group", efflports_svc.list(yellow_group.id)) print("") # Now clear the tag lport = logicalports_svc.get(lport.id) lport.tags = [] logicalports_svc.update(lport.id, lport) # Wait for group recalculation time.sleep(2.0) # The port should be in neither group print("After removing lport color tag:") print_group_effective_members("green group", efflports_svc.list(green_group.id)) print_group_effective_members("yellow group", efflports_svc.list(yellow_group.id)) print("") print("Press enter to delete all resources created for this example.") sys.stdin.readline() # Delete all resources we created. logicalports_svc.delete(lport.id) nsgroups_svc.delete(green_group.id) nsgroups_svc.delete(yellow_group.id) logicalswitches_svc.delete(ls.id) transportzones_svc.delete(tz.id)
def main(): module = AnsibleModule( argument_spec=dict( display_name=dict(required=True, type='str'), description=dict(required=False, type='str', default=None), failover_mode=dict(required=False, type='str', default=None, choices=['NON_PREEMPTIVE', 'PREEMPTIVE']), edge_cluster_id=dict(required=False, type='str', default=None), connected_t0_id=dict(required=False, type='str', default=None), high_availability_mode=dict(required=False, type='str', default='ACTIVE_STANDBY', choices=['ACTIVE_STANDBY', 'ACTIVE_ACTIVE']), advertise=dict(required=False, type='dict', default=None), tags=dict(required=False, type='dict', default=None), state=dict(required=False, type='str', default="present", choices=['present', 'absent']), nsx_manager=dict(required=True, type='str'), nsx_username=dict(required=True, type='str'), nsx_passwd=dict(required=True, type='str', no_log=True) ), supports_check_mode=True ) if not HAS_PYNSXT: module.fail_json(msg='pynsxt is required for this module') session = requests.session() session.verify = False nsx_url = 'https://%s:%s' % (module.params['nsx_manager'], 443) connector = connect.get_requests_connector( session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_std_configuration(connector) security_context = create_user_password_security_context(module.params["nsx_username"], module.params["nsx_passwd"]) connector.set_security_context(security_context) requests.packages.urllib3.disable_warnings() desired_adv_config = AdvertisementConfig() if module.params['advertise']: if 'enabled' in module.params['advertise']: desired_adv_config.enabled = module.params['advertise']['enabled'] else: desired_adv_config.enabled = True if 'advertise_lb_snat_ip' in module.params['advertise'] and module.params['advertise']['advertise_lb_snat_ip']: desired_adv_config.advertise_lb_snat_ip = module.params['advertise']['advertise_lb_snat_ip'] if 'advertise_lb_vip' in module.params['advertise'] and module.params['advertise']['advertise_lb_vip']: desired_adv_config.advertise_lb_vip = module.params['advertise']['advertise_lb_vip'] if 'advertise_nat_routes' in module.params['advertise'] and module.params['advertise']['advertise_nat_routes']: desired_adv_config.advertise_nat_routes = module.params['advertise']['advertise_nat_routes'] if 'advertise_nsx_connected_routes' in module.params['advertise'] and module.params['advertise']['advertise_nsx_connected_routes']: desired_adv_config.advertise_nsx_connected_routes = module.params['advertise']['advertise_nsx_connected_routes'] if 'advertise_static_routes' in module.params['advertise'] and module.params['advertise']['advertise_static_routes']: desired_adv_config.advertise_static_routes = module.params['advertise']['advertise_static_routes'] tags=None if module.params['tags'] is not None: tags = [] for key, value in module.params['tags'].items(): tag=Tag(scope=key, tag=value) tags.append(tag) lr_svc = LogicalRouters(stub_config) lr = getLogicalRouterByName(module, stub_config) if module.params['state'] == 'present': if lr is None: new_lr = LogicalRouter( display_name=module.params['display_name'], description=module.params['description'], failover_mode=module.params['failover_mode'], edge_cluster_id=module.params['edge_cluster_id'], router_type='TIER1', high_availability_mode=module.params['high_availability_mode'], tags=tags ) if module.check_mode: module.exit_json(changed=True, debug_out=str(new_lr), id="1111") try: new_lr = lr_svc.create(new_lr) mylr = getLogicalRouterByName(module, stub_config) if module.params['connected_t0_id']: connectT0(mylr, module, stub_config) if module.params['advertise']: adv_svc = Advertisement(stub_config) adv_config = adv_svc.get(mylr.id) adv_config.enabled = desired_adv_config.enabled adv_config.advertise_lb_snat_ip = desired_adv_config.advertise_lb_snat_ip adv_config.advertise_lb_vip = desired_adv_config.advertise_lb_vip adv_config.advertise_nat_routes = desired_adv_config.advertise_nat_routes adv_config.advertise_nsx_connected_routes = desired_adv_config.advertise_nsx_connected_routes adv_config.advertise_static_routes = desired_adv_config.advertise_static_routes adv_svc.update(mylr.id, adv_config) module.exit_json(changed=True, object_name=module.params['display_name'], id=mylr.id, message="Logical Router with name %s created!"%(module.params['display_name'])) except Error as ex: module.fail_json(msg='API Error listing Logical Routers: %s'%(str(ex))) elif lr: adv_svc = Advertisement(stub_config) adv_config = adv_svc.get(lr.id) changed = False if tags != lr.tags: changed = True lr.tags=tags if module.params['edge_cluster_id'] != lr.edge_cluster_id: lr.edge_cluster_id=module.params['edge_cluster_id'] changed = True if changed: if module.check_mode: module.exit_json(changed=True, debug_out=str(lr), id=lr.id) new_lr = lr_svc.update(lr.id, lr) module.exit_json(changed=True, object_name=module.params['display_name'], id=new_lr.id, message="Logical Router with name %s has been modified!"%(module.params['display_name'])) if compareLrpT0T1(lr, module, stub_config): module.exit_json(changed=True, object_name=module.params['display_name'], id=lr.id, message="Logical Router uplink on T1 with name %s has been modified!"%(module.params['display_name'])) if module.params['advertise']: changed = False if ('enabled' not in module.params['advertise']): changed = False elif module.params['advertise']['enabled'] != adv_config.enabled: adv_config.enabled = desired_adv_config.enabled changed = True if ('advertise_lb_snat_ip' not in module.params['advertise']): if adv_config.advertise_lb_snat_ip: adv_config.advertise_lb_snat_ip = None changed = True elif module.params['advertise']['advertise_lb_snat_ip'] != adv_config.advertise_lb_snat_ip: adv_config.advertise_lb_snat_ip = desired_adv_config.advertise_lb_snat_ip changed = True if ('advertise_lb_vip' not in module.params['advertise']): if adv_config.advertise_lb_vip: adv_config.advertise_lb_vip = None changed = True elif module.params['advertise']['advertise_lb_vip'] != adv_config.advertise_lb_vip: adv_config.advertise_lb_vip = desired_adv_config.advertise_lb_vip changed = True if ('advertise_nat_routes' not in module.params['advertise']): if adv_config.advertise_nat_routes: adv_config.advertise_nat_routes = None changed = True elif module.params['advertise']['advertise_nat_routes'] != adv_config.advertise_nat_routes: adv_config.advertise_nat_routes = desired_adv_config.advertise_nat_routes changed = True if ('advertise_nsx_connected_routes' not in module.params['advertise']): if adv_config.advertise_nsx_connected_routes: adv_config.advertise_nsx_connected_routes = None changed = True elif module.params['advertise']['advertise_nsx_connected_routes'] != adv_config.advertise_nsx_connected_routes: adv_config.advertise_nsx_connected_routes = desired_adv_config.advertise_nsx_connected_routes changed = True if ('advertise_static_routes' not in module.params['advertise']): if adv_config.advertise_static_routes: adv_config.advertise_static_routes = None changed = True elif module.params['advertise']['advertise_static_routes'] != adv_config.advertise_static_routes: adv_config.advertise_static_routes = desired_adv_config.advertise_static_routes changed = True if changed: if module.check_mode: module.exit_json(changed=True, debug_out=str(adv_config), id=lr.id) adv_svc.update(lr.id, adv_config) module.exit_json(changed=True, object_name=module.params['display_name'], id=lr.id, message="Logical Router advertisement config on T1 with name %s has been modified!"%(module.params['display_name'])) module.exit_json(changed=False, object_name=module.params['display_name'], id=lr.id, message="Logical Router with name %s already exists!"%(module.params['display_name'])) elif module.params['state'] == "absent": if lr: if module.check_mode: module.exit_json(changed=True, debug_out=str(lr), id=lr.id) try: deleteAllPortsOnRouter(lr, module, stub_config) lr_svc.delete(lr.id) except Error as ex: api_error = ex.date.convert_to(ApiError) module.fail_json(msg='API Error deleting Logical Routers: %s'%(api_error.error_message)) module.exit_json(changed=True, object_name=module.params['display_name'], message="Logical Router with name %s deleted!"%(module.params['display_name'])) module.exit_json(changed=False, object_name=module.params['display_name'], message="Logical Router with name %s does not exist!"%(module.params['display_name']))