def advertised(ctx, device, json): """ Show OMP peer information """ vmanage_device = Device(ctx.auth, ctx.host) mn = MonitorNetwork(ctx.auth, ctx.host) # Check to see if we were passed in a device IP address or a device name try: ip = ipaddress.ip_address(device) system_ip = ip except ValueError: device_dict = vmanage_device.get_device_status(device, key='host-name') if 'system-ip' in device_dict: system_ip = device_dict['system-ip'] else: system_ip = None if not json: click.echo("VPN PREFIX PROTOCOL ") click.echo("-------------------------------------") # try: omp_peers = mn.get_omp_routes_advertised(system_ip) if json: pp = pprint.PrettyPrinter(indent=2) pp.pprint(omp_peers) else: for peer in omp_peers: if 'protocol' in peer: protocol = peer['protocol'] else: protocol = '' click.echo( f"{peer['vpn-id']:<6} {peer['prefix']:<18} {protocol:<10}")
def peers(ctx, device, json): """ Show OMP peer information """ vmanage_device = Device(ctx.auth, ctx.host) mn = MonitorNetwork(ctx.auth, ctx.host) # Check to see if we were passed in a device IP address or a device name try: ip = ipaddress.ip_address(device) system_ip = ip except ValueError: device_dict = vmanage_device.get_device_status(device, key='host-name') if 'system-ip' in device_dict: system_ip = device_dict['system-ip'] else: system_ip = None if not json: click.echo(" DOMAIN OVERLAY SITE") click.echo("PEER TYPE ID ID ID STATE UPTIME R/I/S") click.echo("---------------------------------------------------------------------------------------") # try: omp_peers = mn.get_omp_peers(system_ip) if json: pp = pprint.PrettyPrinter(indent=2) pp.pprint(omp_peers) else: for peer in omp_peers: click.echo( f"{peer['peer']:<16} {peer['type']:<7} {peer['domain-id']:<6} {'X':<7} {peer['site-id']:<6} {peer['state']:<8} {peer['up-time']:<16} X/X/X" )
def table(ctx, device, json): """ Show Interfaces """ vmanage_device = Device(ctx.auth, ctx.host) # Check to see if we were passed in a device IP address or a device name try: ip = ipaddress.ip_address(device) system_ip = ip except ValueError: device_dict = vmanage_device.get_device_status(device, key='host-name') if 'system-ip' in device_dict: system_ip = device_dict['system-ip'] else: system_ip = None if not json: click.echo( "VPNID PREFIX NEXT HOP PROTOCOL ") click.echo( "----------------------------------------------------------------") routes = vmanage_device.get_device_data('ip/routetable', system_ip) for rte in routes: if json: pp = pprint.PrettyPrinter(indent=2) pp.pprint(rte) else: if 'nexthop-addr' not in rte: rte['nexthop-addr'] = '' click.echo( f"{rte['vpn-id']:5} {rte['prefix']:<20} {rte['nexthop-addr']:<20} {rte['protocol']:8}" )
def received(ctx, device, json): """ Show OMP peer information """ vmanage_device = Device(ctx.auth, ctx.host, ctx.port) mn = MonitorNetwork(ctx.auth, ctx.host, ctx.port) # Check to see if we were passed in a device IP address or a device name try: ip = ipaddress.ip_address(device) system_ip = ip except ValueError: device_dict = vmanage_device.get_device_status(device, key='host-name') if 'system-ip' in device_dict: system_ip = device_dict['system-ip'] else: system_ip = None if not json: click.echo("VPN PREFIX PROTOCOL FROM-PEER Originator COLOR STATUS") click.echo("------------------------------------------------------------------------------------------------") # try: omp_peers = mn.get_omp_routes_received(system_ip) if json: pp = pprint.PrettyPrinter(indent=2) pp.pprint(omp_peers) else: for peer in omp_peers: click.echo( f"{peer['vpn-id']:<6} {peer['prefix']:<18} {peer['protocol']:<10} {peer['from-peer']:<15} {peer['originator']:<15} {peer['color']:<15} {peer['attribute-type']:<16}" )
def status(ctx, dev, device_type, json): #pylint: disable=unused-argument """ Show device status information """ vmanage_device = Device(ctx.auth, ctx.host) # output = mn.get_control_connections_history(sysip) # vmanage_session = ctx.obj pp = pprint.PrettyPrinter(indent=2) if dev: # Check to see if we were passed in a device IP address or a device name try: system_ip = ipaddress.ip_address(dev) device_dict = vmanage_device.get_device_status(system_ip) except ValueError: device_dict = vmanage_device.get_device_status(dev, key='host-name') if device_dict: pp.pprint(device_dict) else: click.secho(f"Could not find device {dev}", err=True, fg='red') else: device_list = vmanage_device.get_device_status_list() if json: pp.pprint(device_list) else: click.echo( f"{'Hostname':20} {'System IP':15} {'Model':15} {'Site':6} {'Status':9} {'BFD':>3} {'OMP':>3} {'CON':>3} {'Version':8} {'UUID':40} {'Serial'}" ) for device_entry in device_list: if 'bfdSessionsUp' in device_entry: bfd = device_entry['bfdSessionsUp'] else: bfd = '' if 'ompPeers' in device_entry: omp = device_entry['ompPeers'] else: omp = '' if 'controlConnections' in device_entry: control = device_entry['controlConnections'] else: control = '' click.echo( f"{device_entry['host-name']:20} {device_entry['system-ip']:15} {device_entry['device-model']:15} {device_entry['site-id']:6} {device_entry['reachability']:9} {bfd:>3} {omp:>3} {control:>3} {device_entry['version']:8} {device_entry['uuid']:40} {device_entry['board-serial']}" )
def device(ctx, device): """ Decommission device """ vmanage_device = Device(ctx.auth, ctx.host, ctx.port) status = vmanage_device.get_device_status(device, key='host-name') if 'uuid' in status: vmanage_device.put_device_decommission(status['uuid']) else: click.secho(f'Cannot find UUID for device {device}', fg="red")
def connections(ctx, device, json): """ Show control connections """ vmanage_device = Device(ctx.auth, ctx.host, ctx.port) mn = MonitorNetwork(ctx.auth, ctx.host, ctx.port) if device: # Check to see if we were passed in a device IP address or a device name try: ipaddress.ip_address(device) system_ip = device except ValueError: device_dict = vmanage_device.get_device_status(device, key='host-name') if 'system-ip' in device_dict: system_ip = device_dict['system-ip'] device_list = [system_ip] else: control_device_dict = vmanage_device.get_device_config_dict( device_type='controllers', key_name='deviceIP') device_list = list(control_device_dict.keys()) if not json: click.echo( "LOCAL PEER PEER PEER SITE DOMAIN PEER PEER " ) click.echo( "SYSTEM IP TYPE PROT SYSTEM IP ID ID PRIVATE IP PUBLIC IP LOCAL COLOR PROXY STATE UPTIME" ) click.echo( "-------------------------------------------------------------------------------------------------------------------" ) for dev in device_list: try: control_connections = mn.get_control_connections(dev) if json: pp = pprint.PrettyPrinter(indent=2) pp.pprint(control_connections) else: for connection in control_connections: click.echo( f"{dev:15} {connection['peer-type']:7} {connection['protocol']:4} {connection['system-ip']:15} {connection['site-id']:6} {connection['domain-id']:6} {connection['private-ip']:15} {connection['public-ip']:15} {connection['local-color']:15} {connection['state']:11} {connection['uptime']:11}" ) # TODO: figure out correct exception type to catch except Exception: pass
def list_interface(ctx, device, json): """ Show Interfaces """ vmanage_device = Device(ctx.auth, ctx.host) if device: # Check to see if we were passed in a device IP address or a device name try: ipaddress.ip_address(device) system_ip = device except ValueError: device_dict = vmanage_device.get_device_status(device, key='host-name') if 'system-ip' in device_dict: system_ip = device_dict['system-ip'] device_list = [system_ip] if not json: click.echo( "IFNAME VPNID IP ADDR MAC ADDR OPER STATE DESC" ) click.echo( "----------------------------------------------------------------------------------------------------------------------" ) for dev in device_list: interfaces = vmanage_device.get_device_data('interface', dev) for iface in interfaces: if json: pp = pprint.PrettyPrinter(indent=2) pp.pprint(iface) else: if 'hwaddr' not in iface: iface['hwaddr'] = '' if 'desc' not in iface: iface['desc'] = '' if 'ip-address' not in iface: iface['ip-address'] = '' click.echo( f"{iface['ifname']:17} {iface['vpn-id']:6} {iface['ip-address']:16} {iface['hwaddr']:25} {iface['if-oper-status']:17} {iface['desc']:17}" )
def connections_history(ctx, device, json): """ Show control connections history """ vmanage_device = Device(ctx.auth, ctx.host, ctx.port) mn = MonitorNetwork(ctx.auth, ctx.host, ctx.port) # Check to see if we were passed in a device IP address or a device name try: ipaddress.ip_address(device) system_ip = device except ValueError: device_dict = vmanage_device.get_device_status(device, key='host-name') if 'system-ip' in device_dict: system_ip = device_dict['system-ip'] if not json: click.echo( "PEER PEER PEER SITE DOMAIN PEER PRIVATE PEER PUBLIC LOCAL REMOTE" ) click.echo( "TYPE PROTOCOL SYSTEM IP ID ID PRIVATE IP PORT PUBLIC IP PORT LOCAL COLOR STATE ERROR ERROR" ) click.echo( "-------------------------------------------------------------------------------------------------------------------------------------------" ) try: control_connections_history = mn.get_control_connections_history( system_ip) if json: pp = pprint.PrettyPrinter(indent=2) pp.pprint(control_connections_history) else: for connection in control_connections_history: click.echo( f"{connection['peer-type']:8} {connection['protocol']:8} {connection['system-ip']:16} {connection['site-id']:5} {connection['domain-id']:6} {connection['private-ip']:15} {connection['private-port']:8} {connection['private-ip']:15} {connection['private-port']:7} {connection['local-color']:15} {connection['state']:11} {connection['local_enum']:7} {connection['local_enum-desc']}" ) except Exception: pass
def import_attachment_list(self, attachment_list, check_mode=False, update=False): """Import a list of device attachments to vManage. Args: attachment_list (list): List of attachments check_mode (bool): Only check to see if changes would be made update (bool): Update the template if it exists Returns: result (list): Returns the diffs of the updates. """ attachment_updates = {} attachment_failures = {} action_id_list = [] device_template_dict = self.device_templates.get_device_template_dict() vmanage_device = Device(self.session, self.host, self.port) for attachment in attachment_list: if attachment['template'] in device_template_dict: if attachment['device_type'] == 'vedge': # The UUID is fixes from the serial file/upload device_uuid = attachment['uuid'] else: # If this is not a vedge, we need to get the UUID from the vmanage since # it is generated by that vmanage device_status = vmanage_device.get_device_status( attachment['host_name'], key='host-name') if device_status: device_uuid = device_status['uuid'] else: raise Exception( f"Cannot find UUID for {attachment['host_name']}") template_id = device_template_dict[ attachment['template']]['templateId'] attached_uuid_list = self.device_templates.get_attachments( template_id, key='uuid') if device_uuid in attached_uuid_list: # The device is already attached to the template. We need to see if any of # the input changed, so we make an API call to get the input on last attach existing_template_input = self.device_templates.get_template_input( device_template_dict[attachment['template']] ['templateId'], [device_uuid]) current_variables = existing_template_input['data'][0] changed = False for property_name in attachment['variables']: # Check to see if any of the passed in varibles have changed from what is # already on the attachment. We are are not checking to see if the # correct variables are here. That will be done on attachment. if ((property_name in current_variables) and (str(attachment['variables'][property_name]) != str(current_variables[property_name]))): changed = True if changed: if not check_mode and update: action_id = self.device_templates.attach_to_template( template_id, device_uuid, attachment['system_ip'], attachment['host_name'], attachment['site_id'], attachment['variables']) action_id_list.append(action_id) else: if not check_mode: action_id = self.device_templates.attach_to_template( template_id, device_uuid, attachment['system_ip'], attachment['host_name'], attachment['site_id'], attachment['variables']) action_id_list.append(action_id) else: raise Exception(f"No template named {attachment['template']}") utilities = Utilities(self.session, self.host) # Batch the waits so that the peocessing of the attachments is in parallel for action_id in action_id_list: result = utilities.waitfor_action_completion(action_id) data = result['action_response']['data'][0] if result['action_status'] == 'failure': attachment_failures.update( {data['uuid']: data['currentActivity']}) else: attachment_updates.update( {data['uuid']: data['currentActivity']}) result = { 'updates': attachment_updates, 'failures': attachment_failures } return result
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = vmanage_argument_spec() argument_spec.update( device=dict(type='str', aliases=['device', 'host-name']), gather_subset=dict(type='list', options=subset_options), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, original_message='', message='') # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) vmanage = Vmanage(module) vmanage_device = Device(vmanage.auth, vmanage.host) vmanage_monitor = MonitorNetwork(vmanage.auth, vmanage.host) if vmanage.params['device']: # If we are passed in a device, we return specific facts about that device device_facts = {} # Check to see if we were passed in a device IP address or a device name try: system_ip = ipaddress.ip_address(vmanage.params['device']) device_status = vmanage_device.get_device_status(system_ip) except ValueError: device_status = vmanage_device.get_device_status( vmanage.params['device'], key='host-name') system_ip = device_status['system-ip'] if device_status: if vmanage.params['gather_subset']: requested_subsets = vmanage.params['gather_subset'] else: requested_subsets = subset_options device_facts['status'] = device_status if 'config' in requested_subsets: device_facts['config'] = vmanage_device.get_device_config( device_status['device-type'], system_ip) if 'omp' in requested_subsets: omp_routes_received = vmanage_monitor.get_omp_routes_received( system_ip) omp_routes_advertised = vmanage_monitor.get_omp_routes_advertised( system_ip) omp_routes = { 'received': omp_routes_received, 'advertised': omp_routes_advertised, } omp = {'routes': omp_routes} device_facts['omp'] = omp if 'control' in requested_subsets: control_connections = vmanage_monitor.get_control_connections( system_ip) control_connections_history = vmanage_monitor.get_control_connections_history( system_ip) control = { 'connections': control_connections, 'connections_history': control_connections_history } device_facts['control'] = control vmanage.result['device_facts'] = device_facts else: if vmanage.params['gather_subset']: vmanage.fail_json( msg= "gather_subset argument can only be secified with device argument", **vmanage.result) # Otherwise, we return facts for all devices sorted by device type vmanage.result['vedges'] = vmanage_device.get_device_config_list( 'vedges') vmanage.result['controllers'] = vmanage_device.get_device_config_list( 'controllers') vmanage.exit_json(**vmanage.result)
def config(ctx, dev, device_type, json): """ Show device config information """ vmanage_device = Device(ctx.auth, ctx.host) pp = pprint.PrettyPrinter(indent=2) #pylint: disable=too-many-nested-blocks if dev: # Check to see if we were passed in a device IP address or a device name try: system_ip = ipaddress.ip_address(dev) device_dict = vmanage_device.get_device_status(system_ip) except ValueError: device_dict = vmanage_device.get_device_status(dev, key='host-name') if device_dict: if device_dict['device-type'] in ['vmanage', 'vbond', 'vsmart']: device_type = 'controllers' else: device_type = 'vedges' device_config = vmanage_device.get_device_config( device_type, device_dict['system-ip']) pp.pprint(device_config) else: click.secho(f"Could not find device {dev}", err=True, fg='red') else: click.echo( f"{'Hostname':20} {'Device IP':15} {'Model':15} {'Site':6} {'State':9} {'Template':16} {'Status':7} {'Connection':10} {'Version':7}" ) if device_type in ['all', 'control']: device_list = vmanage_device.get_device_config_list('controllers') if json: pp.pprint(device_list) else: for device_entry in device_list: if 'template' in device_entry: template = device_entry['template'] else: template = '' device_name = device_entry[ 'host-name'] if 'host-name' in device_entry else 'Unknown' reachability = device_entry[ 'reachability'] if 'reachability' in device_entry else 'Unknown' site_id = device_entry[ 'site-id'] if 'site-id' in device_entry else 'Unknown' config_status_message = device_entry[ 'configStatusMessage'] if 'configStatusMessage' in device_entry else 'Unknown' vmanage_connection_state = device_entry[ 'vmanageConnectionState'] if 'vmanageConnectionState' in device_entry else 'Unknown' version = device_entry[ 'version'] if 'version' in device_entry else 'Unknown' click.echo( f"{device_name:20} {device_entry['deviceIP']:15} {device_entry['deviceModel']:15} {site_id:6} {reachability:9} {template:16} {config_status_message:7} {vmanage_connection_state:10} {version:7}" ) if type in ['all', 'edge']: device_list = vmanage_device.get_device_config_list('vedges') if json: pp.pprint(device_list) else: for device_entry in device_list: if 'host-name' in device_entry: if 'template' in device_entry: template = device_entry['template'] else: template = '' device_name = device_entry[ 'host-name'] if 'host-name' in device_entry else 'Unknown' reachability = device_entry[ 'reachability'] if 'reachability' in device_entry else 'Unknown' site_id = device_entry[ 'site-id'] if 'site-id' in device_entry else 'Unknown' config_status_message = device_entry[ 'configStatusMessage'] if 'configStatusMessage' in device_entry else 'Unknown' vmanage_connection_state = device_entry[ 'vmanageConnectionState'] if 'vmanageConnectionState' in device_entry else 'Unknown' version = device_entry[ 'version'] if 'version' in device_entry else 'Unknown' click.echo( f"{device_name:20} {device_entry['deviceIP']:15} {device_entry['deviceModel']:15} {site_id:6} {reachability:9} {template:16} {config_status_message:7} {vmanage_connection_state:10} {version:7}" )