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)
Beispiel #2
0
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}"
                        )