コード例 #1
0
def main():
    module_args = dict(vrf_name=dict(type='str',
                                     required=False,
                                     default='default'),
                       destination_address_prefix=dict(type='str',
                                                       required=True),
                       type=dict(type='str',
                                 default='forward',
                                 choices=['forward', 'blackhole', 'reject']),
                       distance=dict(type='int', default=1),
                       next_hop_interface=dict(type='str', default=None),
                       next_hop_ip_address=dict(type='str', default=None),
                       state=dict(default='create',
                                  choices=['create', 'delete']))

    aruba_ansible_module = ArubaAnsibleModule(module_args)

    vrf_name = aruba_ansible_module.module.params['vrf_name']
    prefix = aruba_ansible_module.module.params['destination_address_prefix']
    route_type = aruba_ansible_module.module.params['type']
    distance = aruba_ansible_module.module.params['distance']
    next_hop_interface = aruba_ansible_module.module.params[
        'next_hop_interface']
    next_hop_ip_address = aruba_ansible_module.module.params[
        'next_hop_ip_address']
    state = aruba_ansible_module.module.params['state']

    vrf = VRF()

    if vrf_name is None:
        vrf_name = 'default'

    if not vrf.check_vrf_exists(aruba_ansible_module, vrf_name):

        if vrf_name == 'default' and state == 'create':
            aruba_ansible_module = vrf.create_vrf(aruba_ansible_module,
                                                  vrf_name)
        else:
            aruba_ansible_module.module.fail_json(msg="VRF {vrf} is not "
                                                  "configured"
                                                  "".format(vrf=vrf_name))

    encoded_prefix = prefix.replace("/", "%2F")
    encoded_prefix = encoded_prefix.replace(":", "%3A")
    index = vrf_name + '/' + encoded_prefix
    if (state == 'create') or (state == 'update'):
        address_family = 'ipv6' if ':' in prefix else 'ipv4'
        static_route = {}
        static_route[index] = {}
        if address_family is not None:
            static_route[index]["address_family"] = address_family
        if prefix is not None:
            static_route[index]["prefix"] = prefix
        if route_type is not None:
            static_route[index]['static_nexthops']["0"]["type"] = route_type
            if route_type == 'forward':
                static_route[index]['static_nexthops'] = {
                    "0": {
                        "bfd_enable": False,
                        "distance": distance
                    }
                }
            if next_hop_interface is not None:
                encoded_interface = next_hop_interface.replace('/', '%2F')
                static_route[index]['static_nexthops']["0"][
                    "port"] = encoded_interface
            if next_hop_ip_address is not None:
                static_route[index]['static_nexthops']["0"][
                    "ip_address"] = next_hop_ip_address

            aruba_ansible_module = vrf.update_vrf_fields(
                aruba_ansible_module, vrf_name, 'Static_Route', static_route)

    if (state == 'delete'):
        if not vrf.check_vrf_exists(aruba_ansible_module, vrf_name):
            aruba_ansible_module.module.fail_json(
                msg="VRF {vrf_name} does not exist".format(vrf_name=vrf_name))
        static_route = vrf.get_vrf_field_value(aruba_ansible_module, vrf_name,
                                               'Static_Route')
        if not static_route:
            aruba_ansible_module.warnings.append(
                "Static route for destination {dest} does not exist in VRF {vrf}"
                .format(dest=prefix, vrf=vrf_name))
        elif index not in static_route.keys():
            aruba_ansible_module.warnings.append(
                "Static route for destination {dest} does not exist in VRF {vrf}"
                .format(dest=prefix, vrf=vrf_name))
        else:
            static_route.pop(index)
            aruba_ansible_module = vrf.update_vrf_fields(
                aruba_ansible_module, vrf_name, 'Static_Route', static_route)

    aruba_ansible_module.update_switch_config()
コード例 #2
0
def main():
    '''
    Ansible module to configure DNS on AOS-CX switch
    '''
    module_args = dict(mgmt_nameservers=dict(type='dict', required=False),
                       dns_domain_list=dict(type='dict', required=False),
                       dns_domain_name=dict(type='str', required=False),
                       dns_name_servers=dict(type='dict', required=False),
                       vrf=dict(type='str', required=False),
                       dns_host_v4_address_mapping=dict(type='dict',
                                                        required=False),
                       state=dict(type='str',
                                  default='create',
                                  choices=['create', 'delete', 'update']))

    # Version management
    try:
        from ansible_collections.arubanetworks.aoscx.plugins.module_utils.aoscx_pyaoscx import Session
        from pyaoscx.session import Session as Pyaoscx_Session
        from pyaoscx.pyaoscx_factory import PyaoscxFactory

        USE_PYAOSCX_SDK = True

    except ImportError:
        USE_PYAOSCX_SDK = False

    # Use PYAOSCX SDK
    if USE_PYAOSCX_SDK:
        from ansible.module_utils.basic import AnsibleModule

        # ArubaModule
        ansible_module = AnsibleModule(argument_spec=module_args,
                                       supports_check_mode=True)

        # Session
        session = Session(ansible_module)
        # Set Variables
        mgmt_nameservers = ansible_module.params['mgmt_nameservers']
        dns_domain_name = ansible_module.params['dns_domain_name']
        dns_domain_list = ansible_module.params['dns_domain_list']
        vrf_name = ansible_module.params['vrf']
        dns_name_servers = ansible_module.params['dns_name_servers']
        dns_host_v4_address_mapping = ansible_module.params[
            'dns_host_v4_address_mapping']
        state = ansible_module.params['state']

        result = dict(changed=False)

        if ansible_module.check_mode:
            ansible_module.exit_json(**result)

        # Get session serialized information
        session_info = session.get_session()
        # Create pyaoscx.session object
        s = Pyaoscx_Session.from_session(session_info['s'],
                                         session_info['url'])

        # Create a Pyaoscx Factory Object
        pyaoscx_factory = PyaoscxFactory(s)

        if state == 'delete':
            # Modifed Variables
            modified_op = False
            modified_op2 = False

            # Create DNS object
            dns = pyaoscx_factory.dns(vrf=vrf_name)

            # Delete MGMT nameservers
            if mgmt_nameservers is not None:
                # Delete it
                modified_op = dns.delete_mgmt_nameservers()

            # Delete DNS
            dns.delete_dns(
                dns_domain_name,
                dns_domain_list,
                dns_name_servers,
                dns_host_v4_address_mapping,
            )
            # Check if dns was modified
            modified_op2 = dns.was_modified()

            # Changed
            result['changed'] = modified_op or modified_op2

        if state == 'create' or state == 'update':
            # Modifed Variables
            modified_op = False
            modified_op2 = False

            # Create DNS object
            dns = pyaoscx_factory.dns(
                vrf=vrf_name,
                domain_name=dns_domain_name,
                domain_list=dns_domain_list,
                domain_servers=dns_name_servers,
                host_v4_address_mapping=dns_host_v4_address_mapping)

            # Check if dns was modified
            modified_op = dns.was_modified()

            # Set MGMT name servers
            if mgmt_nameservers is not None:
                primary = None
                secondary = None
                # Get Primary and Secondary
                for key, value in mgmt_nameservers.items():
                    if key.lower() == 'primary':
                        primary = value
                    elif key.lower() == 'secondary':
                        secondary = value
                # Set up
                modified_op2 = dns.setup_mgmt_nameservers(primary=primary,
                                                          secondary=secondary)

            # Changed
            result['changed'] = modified_op or modified_op2

        # Exit
        ansible_module.exit_json(**result)

    # Use Older version
    else:
        aruba_ansible_module = ArubaAnsibleModule(module_args)

        mgmt_nameservers = aruba_ansible_module.module.params[
            'mgmt_nameservers']
        dns_domain_name = aruba_ansible_module.module.params['dns_domain_name']
        dns_domain_list = aruba_ansible_module.module.params['dns_domain_list']
        vrf_name = aruba_ansible_module.module.params['vrf']
        dns_name_servers = aruba_ansible_module.module.params[
            'dns_name_servers']
        dns_host_v4_address_mapping = aruba_ansible_module.module.params[
            'dns_host_v4_address_mapping']
        state = aruba_ansible_module.module.params['state']

        vrf = VRF()

        if state == 'create' or state == 'update':
            if mgmt_nameservers is not None:
                if 'mode' in aruba_ansible_module.running_config['System'][
                        'mgmt_intf']:
                    mgmt_if_mode = aruba_ansible_module.running_config[
                        'System']['mgmt_intf']['mode']
                else:
                    mgmt_if_mode = 'dhcp'

                if mgmt_if_mode != 'static':
                    message_part1 = "The management interface must have static"
                    message_part2 = "IP to configure management interface name servers"
                    aruba_ansible_module.module.fail_json(msg=message_part1 +
                                                          message_part2)

                for key, value in mgmt_nameservers.items():
                    if key.lower() == 'primary':
                        aruba_ansible_module.running_config['System'][
                            'mgmt_intf']['dns_server_1'] = value
                    elif key.lower() == 'secondary':
                        aruba_ansible_module.running_config['System'][
                            'mgmt_intf']['dns_server_2'] = value

            if vrf_name is None:
                vrf_name = 'default'

            if not vrf.check_vrf_exists(aruba_ansible_module, vrf_name):
                aruba_ansible_module.module.fail_json(
                    msg="VRF {vrf} is not configured".format(vrf=vrf_name))
                return aruba_ansible_module

            if dns_domain_name is not None:
                aruba_ansible_module = vrf.update_vrf_fields(
                    aruba_ansible_module, vrf_name, "dns_domain_name",
                    dns_domain_name)

            if dns_domain_list is not None:
                aruba_ansible_module = vrf.update_vrf_fields(
                    aruba_ansible_module, vrf_name, "dns_domain_list",
                    dns_domain_list)

            if dns_name_servers is not None:
                aruba_ansible_module = vrf.update_vrf_fields(
                    aruba_ansible_module, vrf_name, "dns_name_servers",
                    dns_name_servers)

            if dns_host_v4_address_mapping is not None:
                aruba_ansible_module = vrf.update_vrf_fields(
                    aruba_ansible_module, vrf_name,
                    "dns_host_v4_address_mapping", dns_host_v4_address_mapping)

        if state == 'delete':

            if vrf_name is None:
                vrf_name = 'default'

            if not vrf.check_vrf_exists(aruba_ansible_module, vrf_name):
                aruba_ansible_module.warnings.append(
                    "VRF {vrf} is not configured"
                    "".format(vrf=vrf_name))
                return aruba_ansible_module

            if mgmt_nameservers is not None:

                for k in mgmt_nameservers.keys():
                    if k.lower() == 'primary':
                        aruba_ansible_module.running_config['System'][
                            'mgmt_intf'].pop('dns_server_1')
                    elif k.lower() == 'secondary':
                        aruba_ansible_module.running_config['System'][
                            'mgmt_intf'].pop('dns_server_2')

            if dns_domain_name is not None:
                aruba_ansible_module = vrf.delete_vrf_field(
                    aruba_ansible_module, vrf_name, "dns_domain_name",
                    dns_domain_name)

            if dns_domain_list is not None:
                aruba_ansible_module = vrf.delete_vrf_field(
                    aruba_ansible_module, vrf_name, "dns_domain_list",
                    dns_domain_list)

            if dns_name_servers is not None:
                aruba_ansible_module = vrf.delete_vrf_field(
                    aruba_ansible_module, vrf_name, "dns_name_servers",
                    dns_name_servers)

            if dns_host_v4_address_mapping is not None:
                aruba_ansible_module = vrf.delete_vrf_field(
                    aruba_ansible_module, vrf_name,
                    "dns_host_v4_address_mapping", dns_host_v4_address_mapping)

        aruba_ansible_module.update_switch_config()
コード例 #3
0
def main():
    '''
    Ansible module to configure DNS on AOS-CX switch
    '''
    module_args = dict(mgmt_nameservers=dict(type='dict', required=False),
                       dns_domain_list=dict(type='dict', required=False),
                       dns_domain_name=dict(type='str', required=False),
                       dns_name_servers=dict(type='dict', required=False),
                       vrf=dict(type='str', required=False),
                       dns_host_v4_address_mapping=dict(type='dict',
                                                        required=False),
                       state=dict(type='str',
                                  default='create',
                                  choices=['create', 'delete', 'update']))

    aruba_ansible_module = ArubaAnsibleModule(module_args)

    mgmt_nameservers = aruba_ansible_module.module.params['mgmt_nameservers']
    dns_domain_name = aruba_ansible_module.module.params['dns_domain_name']
    dns_domain_list = aruba_ansible_module.module.params['dns_domain_list']
    vrf_name = aruba_ansible_module.module.params['vrf']
    dns_name_servers = aruba_ansible_module.module.params['dns_name_servers']
    dns_host_v4_address_mapping = aruba_ansible_module.module.params[
        'dns_host_v4_address_mapping']
    state = aruba_ansible_module.module.params['state']

    vrf = VRF()

    if state == 'create' or state == 'update':
        if mgmt_nameservers is not None:
            if 'mode' in aruba_ansible_module.running_config['System'][
                    'mgmt_intf']:
                mgmt_if_mode = aruba_ansible_module.running_config['System'][
                    'mgmt_intf']['mode']
            else:
                mgmt_if_mode = 'dhcp'

            if mgmt_if_mode != 'static':
                message_part1 = "The management interface must have static"
                message_part2 = "IP to configure management interface name servers"
                aruba_ansible_module.module.fail_json(msg=message_part1 +
                                                      message_part2)

            for key, value in mgmt_nameservers.items():
                if key.lower() == 'primary':
                    aruba_ansible_module.running_config['System']['mgmt_intf'][
                        'dns_server_1'] = value
                elif key.lower() == 'secondary':
                    aruba_ansible_module.running_config['System']['mgmt_intf'][
                        'dns_server_2'] = value

        if vrf_name is None:
            vrf_name = 'default'

        if not vrf.check_vrf_exists(aruba_ansible_module, vrf_name):
            aruba_ansible_module.module.fail_json(
                msg="VRF {vrf} is not configured".format(vrf=vrf_name))
            return aruba_ansible_module

        if dns_domain_name is not None:
            aruba_ansible_module = vrf.update_vrf_fields(
                aruba_ansible_module, vrf_name, "dns_domain_name",
                dns_domain_name)

        if dns_domain_list is not None:
            aruba_ansible_module = vrf.update_vrf_fields(
                aruba_ansible_module, vrf_name, "dns_domain_list",
                dns_domain_list)

        if dns_name_servers is not None:
            aruba_ansible_module = vrf.update_vrf_fields(
                aruba_ansible_module, vrf_name, "dns_name_servers",
                dns_name_servers)

        if dns_host_v4_address_mapping is not None:
            aruba_ansible_module = vrf.update_vrf_fields(
                aruba_ansible_module, vrf_name, "dns_host_v4_address_mapping",
                dns_host_v4_address_mapping)

    if state == 'delete':

        if vrf_name is None:
            vrf_name = 'default'

        if not vrf.check_vrf_exists(aruba_ansible_module, vrf_name):
            aruba_ansible_module.warnings.append("VRF {vrf} is not configured"
                                                 "".format(vrf=vrf_name))
            return aruba_ansible_module

        if mgmt_nameservers is not None:

            for k in mgmt_nameservers.keys():
                if k.lower() == 'primary':
                    aruba_ansible_module.running_config['System'][
                        'mgmt_intf'].pop('dns_server_1')
                elif k.lower() == 'secondary':
                    aruba_ansible_module.running_config['System'][
                        'mgmt_intf'].pop('dns_server_2')

        if dns_domain_name is not None:
            aruba_ansible_module = vrf.delete_vrf_field(
                aruba_ansible_module, vrf_name, "dns_domain_name",
                dns_domain_name)

        if dns_domain_list is not None:
            aruba_ansible_module = vrf.delete_vrf_field(
                aruba_ansible_module, vrf_name, "dns_domain_list",
                dns_domain_list)

        if dns_name_servers is not None:
            aruba_ansible_module = vrf.delete_vrf_field(
                aruba_ansible_module, vrf_name, "dns_name_servers",
                dns_name_servers)

        if dns_host_v4_address_mapping is not None:
            aruba_ansible_module = vrf.delete_vrf_field(
                aruba_ansible_module, vrf_name, "dns_host_v4_address_mapping",
                dns_host_v4_address_mapping)

    aruba_ansible_module.update_switch_config()
コード例 #4
0
def main():
    module_args = dict(vrf_name=dict(type='str',
                                     required=False,
                                     default='default'),
                       destination_address_prefix=dict(type='str',
                                                       required=True),
                       type=dict(type='str',
                                 default='forward',
                                 choices=['forward', 'blackhole', 'reject']),
                       distance=dict(type='int', default=1),
                       next_hop_interface=dict(type='str', default=None),
                       next_hop_ip_address=dict(type='str', default=None),
                       state=dict(default='create',
                                  choices=['create', 'delete']))

    # Version management
    try:
        from ansible_collections.arubanetworks.aoscx.plugins.module_utils.aoscx_pyaoscx import Session
        from pyaoscx.session import Session as Pyaoscx_Session
        from pyaoscx.pyaoscx_factory import PyaoscxFactory

        USE_PYAOSCX_SDK = True

    except ImportError:

        USE_PYAOSCX_SDK = False

    if USE_PYAOSCX_SDK:

        from ansible.module_utils.basic import AnsibleModule
        # ArubaModule
        ansible_module = AnsibleModule(argument_spec=module_args,
                                       supports_check_mode=True)

        vrf_name = ansible_module.params['vrf_name']
        prefix = ansible_module.params['destination_address_prefix']
        route_type = ansible_module.params['type']
        distance = ansible_module.params['distance']
        next_hop_interface = ansible_module.params['next_hop_interface']
        next_hop_ip_address = ansible_module.params['next_hop_ip_address']
        state = ansible_module.params['state']

        # Session
        session = Session(ansible_module)

        # Set result var
        result = dict(changed=False)

        if ansible_module.check_mode:
            ansible_module.exit_json(**result)

        # Get session serialized information
        session_info = session.get_session()
        # Create pyaoscx.session object
        s = Pyaoscx_Session.from_session(session_info['s'],
                                         session_info['url'])

        # Create a Pyaoscx Factory Object
        pyaoscx_factory = PyaoscxFactory(s)

        if state == 'delete':
            # Create Static Route Object
            static_route = pyaoscx_factory.static_route(vrf_name, prefix)
            # Delete it
            static_route.delete()
            # Changed
            result['changed'] = True

        if state == 'create' or state == 'update':
            # Create Static Route object
            static_route = pyaoscx_factory.static_route(vrf_name, prefix)

            # Add Static Nexthop
            static_route.add_static_nexthop(
                next_hop_ip_address=next_hop_ip_address,
                nexthop_type=route_type,
                distance=distance,
                next_hop_interface=next_hop_interface)

            # Verify if Static Route was created
            if static_route.was_modified():
                # Changed
                result['changed'] = True

        # Exit
        ansible_module.exit_json(**result)
    else:

        aruba_ansible_module = ArubaAnsibleModule(module_args)

        vrf_name = aruba_ansible_module.module.params['vrf_name']
        prefix = aruba_ansible_module.module.params[
            'destination_address_prefix']
        route_type = aruba_ansible_module.module.params['type']
        distance = aruba_ansible_module.module.params['distance']
        next_hop_interface = aruba_ansible_module.module.params[
            'next_hop_interface']
        next_hop_ip_address = aruba_ansible_module.module.params[
            'next_hop_ip_address']
        state = aruba_ansible_module.module.params['state']

        vrf = VRF()

        if vrf_name is None:
            vrf_name = 'default'

        if not vrf.check_vrf_exists(aruba_ansible_module, vrf_name):

            if vrf_name == 'default' and state == 'create':
                aruba_ansible_module = vrf.create_vrf(aruba_ansible_module,
                                                      vrf_name)
            else:
                aruba_ansible_module.module.fail_json(msg="VRF {vrf} is not "
                                                      "configured"
                                                      "".format(vrf=vrf_name))

        encoded_prefix = prefix.replace("/", "%2F")
        encoded_prefix = encoded_prefix.replace(":", "%3A")
        index = vrf_name + '/' + encoded_prefix
        if (state == 'create') or (state == 'update'):
            address_family = 'ipv6' if ':' in prefix else 'ipv4'
            static_route = {}
            static_route[index] = {}
            if address_family is not None:
                static_route[index]["address_family"] = address_family
            if prefix is not None:
                static_route[index]["prefix"] = prefix
            if route_type is not None:
                static_route[index]['static_nexthops']["0"][
                    "type"] = route_type
                if route_type == 'forward':
                    static_route[index]['static_nexthops'] = {
                        "0": {
                            "bfd_enable": False,
                            "distance": distance
                        }
                    }
                if next_hop_interface is not None:
                    encoded_interface = next_hop_interface.replace('/', '%2F')
                    static_route[index]['static_nexthops']["0"][
                        "port"] = encoded_interface
                if next_hop_ip_address is not None:
                    static_route[index]['static_nexthops']["0"][
                        "ip_address"] = next_hop_ip_address

                aruba_ansible_module = vrf.update_vrf_fields(
                    aruba_ansible_module, vrf_name, 'Static_Route',
                    static_route)

        if (state == 'delete'):
            if not vrf.check_vrf_exists(aruba_ansible_module, vrf_name):
                aruba_ansible_module.module.fail_json(
                    msg="VRF {vrf_name} does not exist".format(
                        vrf_name=vrf_name))
            static_route = vrf.get_vrf_field_value(aruba_ansible_module,
                                                   vrf_name, 'Static_Route')
            if not static_route:
                aruba_ansible_module.warnings.append(
                    "Static route for destination {dest} does not exist in VRF {vrf}"
                    .format(dest=prefix, vrf=vrf_name))
            elif index not in static_route.keys():
                aruba_ansible_module.warnings.append(
                    "Static route for destination {dest} does not exist in VRF {vrf}"
                    .format(dest=prefix, vrf=vrf_name))
            else:
                static_route.pop(index)
                aruba_ansible_module = vrf.update_vrf_fields(
                    aruba_ansible_module, vrf_name, 'Static_Route',
                    static_route)

        aruba_ansible_module.update_switch_config()