示例#1
0
def main():
    module_args = dict(acl_name=dict(type='str', required=True),
                       acl_type=dict(type='str',
                                     required=True,
                                     choices=['ipv4', 'ipv6', 'mac']),
                       acl_interface_list=dict(type='list', required=True),
                       acl_direction=dict(type='str',
                                          default='in',
                                          choices=['in', 'out']),
                       state=dict(type='str',
                                  default='create',
                                  choices=['create', 'delete']))

    aruba_ansible_module = ArubaAnsibleModule(module_args)

    acl_name = aruba_ansible_module.module.params['acl_name']
    acl_interface_list = aruba_ansible_module.module.params[
        'acl_interface_list']  # NOQA
    acl_type = aruba_ansible_module.module.params['acl_type']
    acl_direction = aruba_ansible_module.module.params['acl_direction']
    state = aruba_ansible_module.module.params['state']

    interface = Interface()

    for interface_name in acl_interface_list:
        if not interface.check_interface_exists(aruba_ansible_module,
                                                interface_name):
            aruba_ansible_module.module.fail_json(msg="Interface {} is not "
                                                  "configured"
                                                  "".format(interface_name))

        if (state == 'create') or (state == 'update'):
            update_type = 'insert'
        elif (state == 'delete'):
            update_type = 'delete'
        aruba_ansible_module = interface.update_interface_acl_details(
            aruba_ansible_module, interface_name, acl_name, acl_type,
            acl_direction, update_type)

        if update_type == 'insert':
            aruba_ansible_module.module.log(
                msg="Attached ACL {} of type "
                "{} to interface {}"
                "".format(acl_name, acl_type, interface_name))

        if update_type == 'update':
            aruba_ansible_module.module.log(
                msg="Updated ACL {} of type {} attached to interface {}".
                format(acl_name, acl_type, interface_name))  # NOQA

        if (update_type == 'absent') or (update_type == 'delete'):
            aruba_ansible_module.module.log(
                msg="Removed ACL {} of type {} from interface {}".format(
                    acl_name, acl_type, interface_name))

    aruba_ansible_module.update_switch_config()
    def delete_vlan(self, aruba_ansible_module, vlan_id):

        port = Port()
        interface = Interface()

        interface_vlan_id = "vlan{id}".format(id=vlan_id)

        if not self.check_vlan_exist(aruba_ansible_module, vlan_id):
            aruba_ansible_module.warnings.append("VLAN ID {id} is not "
                                                 "configured"
                                                 "".format(id=vlan_id))
            return aruba_ansible_module

        if interface.check_interface_exists(aruba_ansible_module,
                                            interface_vlan_id):
            aruba_ansible_module.module.fail_json(msg="VLAN ID {id} is "
                                                  "configured as interface"
                                                  " VLAN"
                                                  "".format(id=vlan_id))
            return aruba_ansible_module

        port_list = port.get_configured_port_list(aruba_ansible_module)

        vlan_port_fields = [
            "vlan_tag", "vlan_mode", "vlans_per_protocol", "vlan_trunks"
        ]

        vlan_id_str = str(vlan_id)

        for port_name in port_list:

            vlan_field_values = port.get_port_field_values(
                aruba_ansible_module, port_name, vlan_port_fields)  # NOQA

            if vlan_field_values["vlan_tag"] == vlan_id_str:
                aruba_ansible_module = port.update_port_fields(
                    aruba_ansible_module, port_name, {"vlan_tag": "1"})  # NOQA

            if vlan_id_str in vlan_field_values["vlan_trunks"] and type(
                    vlan_field_values["vlan_trunks"]) is list:  # NOQA
                vlan_field_values["vlan_trunks"].remove(vlan_id_str)
                aruba_ansible_module = port.update_port_fields(
                    aruba_ansible_module, port_name,
                    {"vlan_trunks": vlan_field_values["vlan_trunks"]})  # NOQA

        aruba_ansible_module.running_config["VLAN"].pop(vlan_id_str)

        return aruba_ansible_module
示例#3
0
def main():
    module_args = dict(
        vlan_id=dict(type='str', required=True),
        admin_state=dict(type='str', required=False, choices=['up', 'down']),
        state=dict(default='create', choices=['create', 'delete', 'update']),
        ipv4=dict(type='list', default=None),
        description=dict(type='str', default=None),
        ipv6=dict(type='list', default=None),
        vrf=dict(type='str', default=None),
        ip_helper_address=dict(type='list', default=None),
        active_gateway_ip=dict(type='str', default=None),
        active_gateway_mac_v4=dict(type='str', default=None),
    )

    aruba_ansible_module = ArubaAnsibleModule(module_args)

    vlan_id = aruba_ansible_module.module.params['vlan_id']
    admin_state = aruba_ansible_module.module.params['admin_state']
    ipv4 = aruba_ansible_module.module.params['ipv4']
    ipv6 = aruba_ansible_module.module.params['ipv6']
    vrf = aruba_ansible_module.module.params['vrf']
    description = aruba_ansible_module.module.params['description']
    ip_helper_address = aruba_ansible_module.module.params['ip_helper_address']
    active_gateway_ip = aruba_ansible_module.module.params['active_gateway_ip']
    active_gateway_mac_v4 = aruba_ansible_module.module.params[
        'active_gateway_mac_v4']  # NOQA
    state = aruba_ansible_module.module.params['state']

    vlan = VLAN()
    port = Port()
    interface = Interface()
    vlan_interface_id = "vlan" + vlan_id
    if not vlan.check_vlan_exist(aruba_ansible_module, vlan_id):
        aruba_ansible_module.module.fail_json(msg="VLAN {id} does not exist. "
                                              "VLAN needs to be created "
                                              "before adding or deleting "
                                              "interfaces"
                                              "".format(id=vlan_id))

    if state == 'create':
        aruba_ansible_module = port.create_port(aruba_ansible_module,
                                                vlan_interface_id)
        aruba_ansible_module = interface.create_interface(aruba_ansible_module,
                                                          vlan_interface_id,
                                                          type='vlan')

        if admin_state is None:
            admin_state = 'up'

        user_config = {
            "admin": admin_state,
        }

        interface_fields = {
            "name": vlan_interface_id,
            "type": "vlan",
            "user_config": user_config
        }
        aruba_ansible_module = interface.update_interface_fields(
            aruba_ansible_module, vlan_interface_id, interface_fields)  # NOQA

        if vrf is not None:
            vrf_name = vrf
        else:
            vrf_name = "default"

        port_fields = {
            "interfaces": [vlan_interface_id],
            "vlan_tag": vlan_id,
            "vrf": vrf_name,
            "admin": admin_state
        }
        aruba_ansible_module = port.update_port_fields(aruba_ansible_module,
                                                       vlan_interface_id,
                                                       port_fields)

    if (state == 'create') or (state == 'update'):

        if not port.check_port_exists(aruba_ansible_module, vlan_interface_id):
            aruba_ansible_module.module.fail_json(msg="VLAN interface does not"
                                                  " exist")

        if admin_state is not None:
            port_fields = {"admin": admin_state}
            user_config = {"admin": admin_state}
            interface_fields = {"user_config": user_config}

        aruba_ansible_module = port.update_port_fields(aruba_ansible_module,
                                                       vlan_interface_id,
                                                       port_fields)
        aruba_ansible_module = interface.update_interface_fields(
            aruba_ansible_module, vlan_interface_id, interface_fields)  # NOQA

        if description is not None:
            port_fields = {"description": description}

            aruba_ansible_module = port.update_port_fields(
                aruba_ansible_module, vlan_interface_id, port_fields)  # NOQA

        if ipv4 is not None:
            l3_interface = L3_Interface()
            aruba_ansible_module = l3_interface.update_interface_ipv4_address(
                aruba_ansible_module, vlan_interface_id, ipv4)  # NOQA

        if ipv6 is not None:
            l3_interface = L3_Interface()
            aruba_ansible_module = l3_interface.update_interface_ipv6_address(
                aruba_ansible_module, vlan_interface_id, ipv6)  # NOQA

        if ip_helper_address is not None:
            l3_interface = L3_Interface()
            if vrf is None:
                vrf = "default"
            aruba_ansible_module = l3_interface.update_interface_ip_helper_address(
                aruba_ansible_module, vrf, vlan_interface_id,
                ip_helper_address)  # NOQA

        if vrf is not None:
            l3_interface = L3_Interface()
            aruba_ansible_module = l3_interface.update_interface_vrf_details_from_l3(
                aruba_ansible_module,
                vrf,
                vlan_interface_id,
                update_type="insert")  # NOQA

        if (active_gateway_ip is not None) and (active_gateway_mac_v4 is None):
            aruba_ansible_module.module.fail_json(
                msg=
                " Both active_gateway_ip and active_gateway_mac_v4 are required for configure active gateway."
            )  # NOQA
        elif (active_gateway_ip is None) and (active_gateway_mac_v4
                                              is not None):  # NOQA
            aruba_ansible_module.module.fail_json(
                msg=
                " Both active_gateway_ip and active_gateway_mac_v4 are required for configure active gateway."
            )  # NOQA
        elif (active_gateway_ip is not None) and (active_gateway_mac_v4
                                                  is not None):  # NOQA
            port_fields = {
                "vsx_virtual_ip4": active_gateway_ip,
                "vsx_virtual_gw_mac_v4": active_gateway_mac_v4
            }
            aruba_ansible_module = port.update_port_fields(
                aruba_ansible_module, vlan_interface_id, port_fields)  # NOQA

    if state == 'delete':
        aruba_ansible_module = port.delete_port(aruba_ansible_module,
                                                vlan_interface_id)
        aruba_ansible_module = interface.delete_interface(aruba_ansible_module,
                                                          vlan_interface_id,
                                                          type='vlan')

    aruba_ansible_module.update_switch_config()
示例#4
0
def main():
    module_args = dict(
        vlan_id=dict(type='str', required=True),
        admin_state=dict(type='str', required=False, choices=['up', 'down']),
        state=dict(default='create', choices=['create', 'delete', 'update']),
        ipv4=dict(type='list', default=None),
        description=dict(type='str', default=None),
        ipv6=dict(type='list', default=None),
        vrf=dict(type='str', default=None),
        ip_helper_address=dict(type='list', default=None),
        active_gateway_ip=dict(type='str', default=None),
        active_gateway_mac_v4=dict(type='str', default=None),
    )

    # 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)

        vlan_id = ansible_module.params['vlan_id']
        admin_state = ansible_module.params['admin_state']
        ipv4 = ansible_module.params['ipv4']
        ipv6 = ansible_module.params['ipv6']
        vrf = ansible_module.params['vrf']
        description = ansible_module.params['description']
        ip_helper_address = ansible_module.params['ip_helper_address']  # NOQA
        active_gateway_ip = ansible_module.params['active_gateway_ip']
        active_gateway_mac_v4 = ansible_module.params['active_gateway_mac_v4']
        state = ansible_module.params['state']

        # Set IP variable as empty arrays
        if ipv4 == ['']:
            ipv4 = []
        if ipv6 == ['']:
            ipv6 = []

        # Session
        session = Session(ansible_module)

        # Set variables
        vlan_interface_id = "vlan" + vlan_id
        if admin_state is None:
            admin_state = 'up'
        if vrf is not None:
            vrf_name = vrf
        else:
            vrf_name = "default"

        # 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 Interface Object
            vlan_interface = pyaoscx_factory.interface(vlan_interface_id)
            # Delete it
            vlan_interface.delete()
            # Changed
            result['changed'] = True

        if state == 'create' or state == 'update':
            # Create Interface with incoming attributes
            vlan_interface = pyaoscx_factory.interface(vlan_interface_id)
            # Verify if interface was create
            if vlan_interface.was_modified():
                # Changed
                result['changed'] = True

            # Configure SVI
            # Verify if object was changed
            modified_op = vlan_interface.configure_svi(vlan=int(vlan_id),
                                                       ipv4=ipv4,
                                                       ipv6=ipv6,
                                                       vrf=vrf,
                                                       description=description,
                                                       user_config=admin_state)

            if active_gateway_ip is not None and active_gateway_mac_v4 is not None:
                modified_op2 = vlan_interface.set_active_gateaway(
                    active_gateway_ip, active_gateway_mac_v4)
                modified_op = modified_op2 or modified_op

            if ip_helper_address is not None:
                # Create DHCP_Relay object
                dhcp_relay = pyaoscx_factory.dhcp_relay(vrf=vrf,
                                                        port=vlan_interface_id)
                # Add helper addresses
                modified_dhcp_relay = dhcp_relay.add_ipv4_addresses(
                    ip_helper_address)
                modified_op = modified_op or modified_dhcp_relay

            if modified_op:
                # Changed
                result['changed'] = True

        # Exit
        ansible_module.exit_json(**result)

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

        vlan_id = aruba_ansible_module.module.params['vlan_id']
        admin_state = aruba_ansible_module.module.params['admin_state']
        ipv4 = aruba_ansible_module.module.params['ipv4']
        ipv6 = aruba_ansible_module.module.params['ipv6']
        vrf = aruba_ansible_module.module.params['vrf']
        description = aruba_ansible_module.module.params['description']
        ip_helper_address = aruba_ansible_module.module.params[
            'ip_helper_address']
        active_gateway_ip = aruba_ansible_module.module.params[
            'active_gateway_ip']
        active_gateway_mac_v4 = aruba_ansible_module.module.params[
            'active_gateway_mac_v4']  # NOQA
        state = aruba_ansible_module.module.params['state']

        vlan = VLAN()
        port = Port()
        interface = Interface()
        vlan_interface_id = "vlan" + vlan_id
        if not vlan.check_vlan_exist(aruba_ansible_module, vlan_id):
            aruba_ansible_module.module.fail_json(
                msg="VLAN {id} does not exist. "
                "VLAN needs to be created "
                "before adding or deleting "
                "interfaces"
                "".format(id=vlan_id))

        if state == 'create':
            aruba_ansible_module = port.create_port(aruba_ansible_module,
                                                    vlan_interface_id)
            aruba_ansible_module = interface.create_interface(
                aruba_ansible_module, vlan_interface_id, type='vlan')

            if admin_state is None:
                admin_state = 'up'

            user_config = {
                "admin": admin_state,
            }

            interface_fields = {
                "name": vlan_interface_id,
                "type": "vlan",
                "user_config": user_config
            }
            aruba_ansible_module = interface.update_interface_fields(
                aruba_ansible_module, vlan_interface_id,
                interface_fields)  # NOQA

            if vrf is not None:
                vrf_name = vrf
            else:
                vrf_name = "default"

            port_fields = {
                "interfaces": [vlan_interface_id],
                "vlan_tag": vlan_id,
                "vrf": vrf_name,
                "admin": admin_state
            }
            aruba_ansible_module = port.update_port_fields(
                aruba_ansible_module, vlan_interface_id, port_fields)

        if (state == 'create') or (state == 'update'):

            if not port.check_port_exists(aruba_ansible_module,
                                          vlan_interface_id):
                aruba_ansible_module.module.fail_json(
                    msg="VLAN interface does not"
                    " exist")

            if admin_state is not None:
                port_fields = {"admin": admin_state}
                user_config = {"admin": admin_state}
                interface_fields = {"user_config": user_config}

            aruba_ansible_module = port.update_port_fields(
                aruba_ansible_module, vlan_interface_id, port_fields)
            aruba_ansible_module = interface.update_interface_fields(
                aruba_ansible_module, vlan_interface_id,
                interface_fields)  # NOQA

            if description is not None:
                port_fields = {"description": description}

                aruba_ansible_module = port.update_port_fields(
                    aruba_ansible_module, vlan_interface_id,
                    port_fields)  # NOQA

            if ipv4 is not None:
                l3_interface = L3_Interface()
                aruba_ansible_module = l3_interface.update_interface_ipv4_address(
                    aruba_ansible_module, vlan_interface_id, ipv4)  # NOQA

            if ipv6 is not None:
                l3_interface = L3_Interface()
                aruba_ansible_module = l3_interface.update_interface_ipv6_address(
                    aruba_ansible_module, vlan_interface_id, ipv6)  # NOQA

            if ip_helper_address is not None:
                l3_interface = L3_Interface()
                if vrf is None:
                    vrf = "default"
                aruba_ansible_module = l3_interface.update_interface_ip_helper_address(
                    aruba_ansible_module, vrf, vlan_interface_id,
                    ip_helper_address)  # NOQA

            if vrf is not None:
                l3_interface = L3_Interface()
                aruba_ansible_module = l3_interface.update_interface_vrf_details_from_l3(
                    aruba_ansible_module,
                    vrf,
                    vlan_interface_id,
                    update_type="insert")  # NOQA

            if (active_gateway_ip
                    is not None) and (active_gateway_mac_v4 is None):
                aruba_ansible_module.module.fail_json(
                    msg=
                    " Both active_gateway_ip and active_gateway_mac_v4 are required for configure active gateway."
                )  # NOQA
            elif (active_gateway_ip is None) and (active_gateway_mac_v4
                                                  is not None):  # NOQA
                aruba_ansible_module.module.fail_json(
                    msg=
                    " Both active_gateway_ip and active_gateway_mac_v4 are required for configure active gateway."
                )  # NOQA
            elif (active_gateway_ip is not None) and (active_gateway_mac_v4
                                                      is not None):  # NOQA
                port_fields = {
                    "vsx_virtual_ip4": active_gateway_ip,
                    "vsx_virtual_gw_mac_v4": active_gateway_mac_v4
                }
                aruba_ansible_module = port.update_port_fields(
                    aruba_ansible_module, vlan_interface_id,
                    port_fields)  # NOQA

        if state == 'delete':
            aruba_ansible_module = port.delete_port(aruba_ansible_module,
                                                    vlan_interface_id)
            aruba_ansible_module = interface.delete_interface(
                aruba_ansible_module, vlan_interface_id, type='vlan')

        aruba_ansible_module.update_switch_config()
示例#5
0
def main():
    module_args = dict(
        interface=dict(type='str', required=True),
        admin_state=dict(type='str', default='up', choices=['up', 'down']),
        description=dict(type='str', default=None),
        vlan_mode=dict(type='str', default=None, choices=['access', 'trunk']),
        vlan_access=dict(type='str', default=None),
        vlan_trunks=dict(type='list', default=None),
        trunk_allowed_all=dict(type='bool', default=None),
        native_vlan_id=dict(type='str', default=None),
        native_vlan_tag=dict(type='bool', default=None),
        interface_qos_schedule_profile=dict(type='dict', default=None),
        interface_qos_rate=dict(type='dict', default=None),
        state=dict(type='str', default='create',
                   choices=['create', 'delete', 'update'])
    )

    aruba_ansible_module = ArubaAnsibleModule(module_args)

    params = {}
    for param in aruba_ansible_module.module.params.keys():
        params[param] = aruba_ansible_module.module.params[param]

    state = aruba_ansible_module.module.params['state']
    admin_state = aruba_ansible_module.module.params['admin_state']
    interface_name = aruba_ansible_module.module.params['interface']
    description = aruba_ansible_module.module.params['description']
    interface_qos_rate = aruba_ansible_module.module.params[
        'interface_qos_rate']
    interface_qos_schedule_profile = aruba_ansible_module.module.params[
        'interface_qos_schedule_profile']

    l2_interface = L2_Interface()
    interface = Interface()
    vlan = VLAN()

    interface_vlan_dict = {}

    if params['state'] == 'create':
        aruba_ansible_module = l2_interface.create_l2_interface(
            aruba_ansible_module, interface_name)

        if params['vlan_mode'] == 'access':
            interface_vlan_dict['vlan_mode'] = 'access'

            if params['vlan_access'] is None:
                interface_vlan_dict['vlan_tag'] = 1

            elif vlan.check_vlan_exist(aruba_ansible_module,
                                       params['vlan_access']):
                interface_vlan_dict['vlan_tag'] = params['vlan_access']

            else:
                aruba_ansible_module.module.fail_json(msg="VLAN {id} is not "
                                                          "configured"
                                                          "".format(id=params['vlan_access']))  # NOQA

        elif params['vlan_mode'] == 'trunk':

            if params['native_vlan_id']:
                if params['native_vlan_id'] == '1':
                    interface_vlan_dict['vlan_tag'] = '1'
                    if params['native_vlan_tag']:
                        interface_vlan_dict['vlan_mode'] = 'native-tagged'
                    else:
                        interface_vlan_dict['vlan_mode'] = 'native-untagged'
                elif vlan.check_vlan_exist(aruba_ansible_module,
                                           params['native_vlan_id']):
                    if params['native_vlan_tag']:
                        interface_vlan_dict['vlan_mode'] = 'native-tagged'
                    else:
                        interface_vlan_dict['vlan_mode'] = 'native-untagged'
                    interface_vlan_dict['vlan_tag'] = params['native_vlan_id']
                else:
                    aruba_ansible_module.module.fail_json(
                        msg="VLAN {id} is not configured".format(
                            id=params['native_vlan_id']))

            elif params['native_vlan_tag']:
                interface_vlan_dict['vlan_mode'] = 'native-tagged'
                interface_vlan_dict['vlan_tag'] = '1'

            else:
                interface_vlan_dict['vlan_mode'] = 'native-untagged'
                interface_vlan_dict['vlan_tag'] = '1'

            if not params['trunk_allowed_all'] and params['vlan_trunks']:
                if 'vlan_mode' not in interface_vlan_dict.keys():
                    interface_vlan_dict['vlan_mode'] = 'native-untagged'
                interface_vlan_dict['vlan_trunks'] = []
                for id in params['vlan_trunks']:
                    if vlan.check_vlan_exist(aruba_ansible_module, id):
                        interface_vlan_dict['vlan_trunks'].append(str(id))
                    else:
                        aruba_ansible_module.module.fail_json(
                            msg="VLAN {id} is not configured".format(id=id))

            elif params['trunk_allowed_all']:
                if 'vlan_mode' not in interface_vlan_dict.keys():
                    interface_vlan_dict['vlan_mode'] = 'native-untagged'

        else:
            interface_vlan_dict['vlan_mode'] = 'access'
            interface_vlan_dict['vlan_tag'] = 1

        aruba_ansible_module = l2_interface.update_interface_vlan_details(
            aruba_ansible_module, interface_name, interface_vlan_dict)

    if state == 'delete':
        aruba_ansible_module = l2_interface.delete_l2_interface(
            aruba_ansible_module, interface_name)

    if (state == 'update') or (state == 'create'):

        if admin_state is not None:
            aruba_ansible_module = interface.update_interface_admin_state(
                aruba_ansible_module, interface_name, admin_state)

        if description is not None:
            aruba_ansible_module = interface.update_interface_description(
                aruba_ansible_module, interface_name, description)

        if interface_qos_rate is not None:
            aruba_ansible_module = l2_interface.update_interface_qos_rate(
                aruba_ansible_module, interface_name, interface_qos_rate)

        if interface_qos_schedule_profile is not None:
            aruba_ansible_module = l2_interface.update_interface_qos_profile(
                aruba_ansible_module, interface_name,
                interface_qos_schedule_profile)

    aruba_ansible_module.update_switch_config()
示例#6
0
def main():
    module_args = dict(interface=dict(type='str', required=True),
                       admin_state=dict(default='up', choices=['up', 'down']),
                       description=dict(type='str', default=None),
                       ipv4=dict(type='list', default=None),
                       ipv6=dict(type='list', default=None),
                       interface_qos_rate=dict(type='dict', default=None),
                       interface_qos_schedule_profile=dict(type='str',
                                                           default=None),
                       vrf=dict(type='str', default=None),
                       ip_helper_address=dict(type='list', default=None),
                       interface_acl_details=dict(type='dict', default=None),
                       state=dict(default='create',
                                  choices=['create', 'delete', 'update']))

    aruba_ansible_module = ArubaAnsibleModule(module_args)

    interface_name = aruba_ansible_module.module.params['interface']
    admin_state = aruba_ansible_module.module.params['admin_state']
    description = aruba_ansible_module.module.params['description']
    ipv4 = aruba_ansible_module.module.params['ipv4']
    ipv6 = aruba_ansible_module.module.params['ipv6']
    interface_qos_rate = aruba_ansible_module.module.params[
        'interface_qos_rate']  # NOQA
    interface_qos_schedule_profile = aruba_ansible_module.module.params[
        'interface_qos_schedule_profile']
    vrf = aruba_ansible_module.module.params['vrf']
    interface_acl_details = aruba_ansible_module.module.params[
        'interface_acl_details']
    ip_helper_address = aruba_ansible_module.module.params['ip_helper_address']

    state = aruba_ansible_module.module.params['state']

    l3_interface = L3_Interface()
    interface = Interface()
    if state == 'create':
        aruba_ansible_module = l3_interface.create_l3_interface(
            aruba_ansible_module, interface_name)  # NOQA
        if vrf is None:
            vrf = "default"
        aruba_ansible_module = l3_interface.update_interface_vrf_details_from_l3(
            aruba_ansible_module, vrf, interface_name)  # NOQA

    if state == 'delete':
        aruba_ansible_module = l3_interface.delete_l3_interface(
            aruba_ansible_module, interface_name)

    if (state == 'update') or (state == 'create'):

        if admin_state is not None:
            aruba_ansible_module = interface.update_interface_admin_state(
                aruba_ansible_module, interface_name, admin_state)

        if description is not None:
            aruba_ansible_module = interface.update_interface_description(
                aruba_ansible_module, interface_name, description)

        if interface_acl_details is not None:
            acl_name = interface_acl_details["acl_name"]
            acl_type = interface_acl_details["acl_type"]
            acl_direction = interface_acl_details["acl_direction"]
            aruba_ansible_module = l3_interface.update_interface_acl_details(
                aruba_ansible_module, interface_name, acl_name, acl_type,
                acl_direction)

        if vrf is not None and vrf != "default":
            aruba_ansible_module = l3_interface.update_interface_vrf_details_from_l3(
                aruba_ansible_module, vrf, interface_name)  # NOQA

        if interface_qos_rate is not None:
            aruba_ansible_module = l3_interface.update_interface_qos_rate(
                aruba_ansible_module, interface_name, interface_qos_rate)

        if interface_qos_schedule_profile is not None:
            aruba_ansible_module = l3_interface.update_interface_qos_profile(
                aruba_ansible_module, interface_name,
                interface_qos_schedule_profile)  # NOQA

        if ipv4 is not None:
            aruba_ansible_module = l3_interface.update_interface_ipv4_address(
                aruba_ansible_module, interface_name, ipv4)  # NOQA

        if ipv6 is not None:
            aruba_ansible_module = l3_interface.update_interface_ipv6_address(
                aruba_ansible_module, interface_name, ipv6)  # NOQA

        if ip_helper_address is not None:
            if vrf is not None:
                vrf = 'default'
            aruba_ansible_module = l3_interface.update_interface_ip_helper_address(
                aruba_ansible_module, vrf, interface_name,
                ip_helper_address)  # NOQA

    aruba_ansible_module.update_switch_config()
def main():
    module_args = dict(
        interface=dict(type='str', required=True),
        admin_state=dict(type='str', default='up', choices=['up', 'down']),
        description=dict(type='str', default=None),
        vlan_mode=dict(type='str', default=None, choices=['access', 'trunk']),
        vlan_access=dict(type='str', default=None),
        vlan_trunks=dict(type='list', default=None),
        trunk_allowed_all=dict(type='bool', default=None),
        native_vlan_id=dict(type='str', default=None),
        native_vlan_tag=dict(type='bool', default=None),
        interface_qos_schedule_profile=dict(type='dict', default=None),
        interface_qos_rate=dict(type='dict', default=None),
        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

    if USE_PYAOSCX_SDK:

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

        interface_name = ansible_module.params['interface']
        admin_state = ansible_module.params['admin_state']
        # Set all variables
        description = ansible_module.params['description']
        vlan_mode = ansible_module.params['vlan_mode']
        vlan_access = ansible_module.params['vlan_access']
        vlan_trunks = ansible_module.params['vlan_trunks']
        trunk_allowed_all = ansible_module.params['trunk_allowed_all']
        native_vlan_id = ansible_module.params['native_vlan_id']
        native_vlan_tag = ansible_module.params['native_vlan_tag']
        qos_profile_details = ansible_module.params['interface_qos_schedule_profile']
        qos_rate = ansible_module.params['interface_qos_rate']
        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 Interface Object
            interface = pyaoscx_factory.interface(interface_name)
            # Delete it
            interface.delete()

            # Changed
            result['changed'] = True

        if state == 'create' or state == 'update':
            # Set VLAN tag
            vlan_tag = None
            if vlan_access is not None:
                vlan_tag = vlan_access
            elif native_vlan_id is not None:
                vlan_tag = native_vlan_id

            if isinstance(vlan_tag, str):
                vlan_tag = int(vlan_tag)

            # Create Interface Object
            interface = pyaoscx_factory.interface(interface_name)
            # Verify if interface was create
            if interface.was_modified():
                # Changed
                result['changed'] = True
            # Configure L2
            # Verify if object was changed
            modified_op = interface.configure_l2(
                description=description,
                admin=admin_state,
                vlan_mode=vlan_mode,
                vlan_tag=vlan_tag,
                vlan_ids_list=vlan_trunks,
                trunk_allowed_all=trunk_allowed_all,
                native_vlan_tag=native_vlan_tag)

            if qos_profile_details is not None:
                modified_op2 = interface.update_interface_qos_profile(
                    qos_profile_details)
                modified_op = modified_op2 or modified_op

            if qos_rate is not None:
                modified_op3 = interface.update_interface_qos_rate(qos_rate)
                modified_op = modified_op3 or modified_op

            if modified_op:
                # Changed
                result['changed'] = True

        # Exit
        ansible_module.exit_json(**result)
    else:
        aruba_ansible_module = ArubaAnsibleModule(module_args)

        params = {}
        for param in aruba_ansible_module.module.params.keys():
            params[param] = aruba_ansible_module.module.params[param]

        state = aruba_ansible_module.module.params['state']
        admin_state = aruba_ansible_module.module.params['admin_state']
        interface_name = aruba_ansible_module.module.params['interface']
        description = aruba_ansible_module.module.params['description']
        interface_qos_rate = aruba_ansible_module.module.params[
            'interface_qos_rate']
        interface_qos_schedule_profile = aruba_ansible_module.module.params[
            'interface_qos_schedule_profile']

        l2_interface = L2_Interface()
        interface = Interface()
        vlan = VLAN()

        interface_vlan_dict = {}

        if params['state'] == 'create':
            aruba_ansible_module = l2_interface.create_l2_interface(
                aruba_ansible_module, interface_name)

            if params['vlan_mode'] == 'access':
                interface_vlan_dict['vlan_mode'] = 'access'

                if params['vlan_access'] is None:
                    interface_vlan_dict['vlan_tag'] = 1

                elif vlan.check_vlan_exist(aruba_ansible_module,
                                           params['vlan_access']):
                    interface_vlan_dict['vlan_tag'] = params['vlan_access']

                else:
                    aruba_ansible_module.module.fail_json(
    msg="VLAN {id} is not "
    "configured"
    "".format(
        id=params['vlan_access']))  # NOQA

            elif params['vlan_mode'] == 'trunk':

                if params['native_vlan_id']:
                    if params['native_vlan_id'] == '1':
                        interface_vlan_dict['vlan_tag'] = '1'
                        if params['native_vlan_tag']:
                            interface_vlan_dict['vlan_mode'] = 'native-tagged'
                        else:
                            interface_vlan_dict['vlan_mode'] = 'native-untagged'
                    elif vlan.check_vlan_exist(aruba_ansible_module,
                                               params['native_vlan_id']):
                        if params['native_vlan_tag']:
                            interface_vlan_dict['vlan_mode'] = 'native-tagged'
                        else:
                            interface_vlan_dict['vlan_mode'] = 'native-untagged'
                        interface_vlan_dict['vlan_tag'] = params['native_vlan_id']
                    else:
                        aruba_ansible_module.module.fail_json(
                            msg="VLAN {id} is not configured".format(
                                id=params['native_vlan_id']))

                elif params['native_vlan_tag']:
                    interface_vlan_dict['vlan_mode'] = 'native-tagged'
                    interface_vlan_dict['vlan_tag'] = '1'

                else:
                    interface_vlan_dict['vlan_mode'] = 'native-untagged'
                    interface_vlan_dict['vlan_tag'] = '1'

                if not params['trunk_allowed_all'] and params['vlan_trunks']:
                    if 'vlan_mode' not in interface_vlan_dict.keys():
                        interface_vlan_dict['vlan_mode'] = 'native-untagged'
                    interface_vlan_dict['vlan_trunks'] = []
                    for id in params['vlan_trunks']:
                        if vlan.check_vlan_exist(aruba_ansible_module, id):
                            interface_vlan_dict['vlan_trunks'].append(str(id))
                        else:
                            aruba_ansible_module.module.fail_json(
                                msg="VLAN {id} is not configured".format(id=id))

                elif params['trunk_allowed_all']:
                    if 'vlan_mode' not in interface_vlan_dict.keys():
                        interface_vlan_dict['vlan_mode'] = 'native-untagged'

            else:
                interface_vlan_dict['vlan_mode'] = 'access'
                interface_vlan_dict['vlan_tag'] = 1

            aruba_ansible_module = l2_interface.update_interface_vlan_details(
                aruba_ansible_module, interface_name, interface_vlan_dict)

        if state == 'delete':
            aruba_ansible_module = l2_interface.delete_l2_interface(
                aruba_ansible_module, interface_name)

        if (state == 'update') or (state == 'create'):

            if admin_state is not None:
                aruba_ansible_module = interface.update_interface_admin_state(
                    aruba_ansible_module, interface_name, admin_state)

            if description is not None:
                aruba_ansible_module = interface.update_interface_description(
                    aruba_ansible_module, interface_name, description)

            if interface_qos_rate is not None:
                aruba_ansible_module = l2_interface.update_interface_qos_rate(
                    aruba_ansible_module, interface_name, interface_qos_rate)

            if interface_qos_schedule_profile is not None:
                aruba_ansible_module = l2_interface.update_interface_qos_profile(
                    aruba_ansible_module, interface_name, interface_qos_schedule_profile)

        aruba_ansible_module.update_switch_config()
示例#8
0
def main():
    module_args = dict(
        interface=dict(type='str', required=True),
        admin_state=dict(default='up', choices=['up', 'down']),
        description=dict(type='str', default=None),
        ipv4=dict(type='list', default=None),
        ipv6=dict(type='list', default=None),
        interface_qos_rate=dict(type='dict', default=None),
        interface_qos_schedule_profile=dict(type='dict', default=None),
        vrf=dict(type='str', default=None),
        ip_helper_address=dict(type='list', default=None),
        state=dict(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

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

        interface_name = ansible_module.params['interface']
        admin_state = ansible_module.params['admin_state']
        description = ansible_module.params['description']
        ipv4 = ansible_module.params['ipv4']
        ipv6 = ansible_module.params['ipv6']
        qos_rate = ansible_module.params['interface_qos_rate']
        qos_profile_details = ansible_module.params['interface_qos_schedule_profile']
        vrf = ansible_module.params['vrf']
        ip_helper_addresses = ansible_module.params['ip_helper_address']
        state = ansible_module.params['state']
        
        # Set IP variable as empty arrays
        if ipv4 ==['']:
            ipv4 = []
        if ipv6 == ['']:
            ipv6 = []

        # Session
        session = Session(ansible_module)

        # Set Variables
        if vrf is None:
            vrf = 'default'

        # 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 Interface Object
            interface = pyaoscx_factory.interface(interface_name)
            # Delete it
            interface.delete()

            # Changed
            result['changed'] = True

        if state == 'create' or state == 'update':
            # Create Interface Object
            interface = pyaoscx_factory.interface(interface_name)
            # Verify if interface was create
            if interface.was_modified():
                # Changed
                result['changed'] = True
            # Configure L4
            # Verify if object was changed
            modified_op = interface.configure_l3(
                ipv4=ipv4,
                ipv6=ipv6,
                vrf=vrf,
                description=description,
                admin=admin_state)

            if qos_profile_details is not None:
                modified_op2 = interface.update_interface_qos_profile(
                    qos_profile_details)
                modified_op = modified_op2 or modified_op

            if qos_rate is not None:
                modified_op3 = interface.update_interface_qos_rate(
                    qos_rate)
                modified_op = modified_op3 or modified_op

            if ip_helper_addresses is not None:
                # Create DHCP_Relay object
                dhcp_relay = pyaoscx_factory.dhcp_relay(
                    vrf=vrf, port=interface_name)
                # Add helper addresses
                dhcp_relay.add_ipv4_addresses(ip_helper_addresses)

            if modified_op:
                # Changed
                result['changed'] = True

        # Exit
        ansible_module.exit_json(**result)

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

        interface_name = aruba_ansible_module.module.params['interface']
        admin_state = aruba_ansible_module.module.params['admin_state']
        description = aruba_ansible_module.module.params['description']
        ipv4 = aruba_ansible_module.module.params['ipv4']
        ipv6 = aruba_ansible_module.module.params['ipv6']
        interface_qos_rate = aruba_ansible_module.module.params['interface_qos_rate']  # NOQA
        interface_qos_schedule_profile = aruba_ansible_module.module.params[
            'interface_qos_schedule_profile']
        vrf = aruba_ansible_module.module.params['vrf']
        ip_helper_address = aruba_ansible_module.module.params['ip_helper_address']

        state = aruba_ansible_module.module.params['state']

        l3_interface = L3_Interface()
        interface = Interface()
        if state == 'create':
            aruba_ansible_module = l3_interface.create_l3_interface(aruba_ansible_module, interface_name)  # NOQA
            if vrf is None:
                vrf = "default"

            if vrf is not None:
                aruba_ansible_module = l3_interface.update_interface_vrf_details_from_l3(aruba_ansible_module, vrf, interface_name)  # NOQA

        if state == 'delete':
            aruba_ansible_module = l3_interface.delete_l3_interface(
                aruba_ansible_module, interface_name)

        if (state == 'update') or (state == 'create'):

            if admin_state is not None:
                aruba_ansible_module = interface.update_interface_admin_state(
                    aruba_ansible_module, interface_name, admin_state)

            if description is not None:
                aruba_ansible_module = interface.update_interface_description(
                    aruba_ansible_module, interface_name, description)

            if vrf is not None and vrf != "default":
                aruba_ansible_module = l3_interface.update_interface_vrf_details_from_l3(aruba_ansible_module, vrf, interface_name)  # NOQA

            if interface_qos_rate is not None:
                aruba_ansible_module = l3_interface.update_interface_qos_rate(
                    aruba_ansible_module, interface_name, interface_qos_rate)

            if interface_qos_schedule_profile is not None:
                aruba_ansible_module = l3_interface.update_interface_qos_profile(aruba_ansible_module, interface_name, interface_qos_schedule_profile)  # NOQA

            if ipv4 is not None:
                aruba_ansible_module = l3_interface.update_interface_ipv4_address(aruba_ansible_module, interface_name, ipv4)  # NOQA

            if ipv6 is not None:
                aruba_ansible_module = l3_interface.update_interface_ipv6_address(aruba_ansible_module, interface_name, ipv6)  # NOQA

            if ip_helper_address is not None:
                if vrf is not None:
                    vrf = 'default'
                aruba_ansible_module = l3_interface.update_interface_ip_helper_address(aruba_ansible_module, vrf, interface_name, ip_helper_address)  # NOQA

        aruba_ansible_module.update_switch_config()
def main():
    module_args = dict(acl_name=dict(type='str', required=True),
                       acl_type=dict(type='str',
                                     required=True,
                                     choices=['ipv4', 'ipv6', 'mac']),
                       acl_interface_list=dict(type='list', required=True),
                       acl_direction=dict(type='str',
                                          default='in',
                                          choices=['in', 'out']),
                       state=dict(type='str',
                                  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

    # 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
        acl_name = ansible_module.params['acl_name']
        acl_interface_list = ansible_module.params[
            'acl_interface_list']  # NOQA
        acl_type = ansible_module.params['acl_type']
        acl_direction = ansible_module.params['acl_direction']
        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)
        for interface_name in acl_interface_list:
            if state == 'delete':
                # Create ACL Object
                interface = pyaoscx_factory.interface(interface_name)
                # Delete it
                interface.clear_acl(acl_type)
                # Changed
                result['changed'] = True

            if state == 'create' or state == 'update':
                # Create ACL Object
                interface = pyaoscx_factory.interface(interface_name)
                # Verify if interface was create
                if interface.was_modified():
                    # Changed
                    result['changed'] = True

                # Modified variables
                modified_op1 = False
                modified_op2 = False
                # Update ACL inside Interface
                if acl_direction == 'in':
                    modified_op1 = interface.update_acl_in(acl_name, acl_type)
                if acl_direction == 'out':
                    modified_op2 = interface.update_acl_out(acl_name, acl_type)
                if modified_op1 or modified_op2:
                    # Changed
                    result['changed'] = True

        # Exit
        ansible_module.exit_json(**result)

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

        acl_name = aruba_ansible_module.module.params['acl_name']
        acl_interface_list = aruba_ansible_module.module.params[
            'acl_interface_list']  # NOQA
        acl_type = aruba_ansible_module.module.params['acl_type']
        acl_direction = aruba_ansible_module.module.params['acl_direction']
        state = aruba_ansible_module.module.params['state']

        interface = Interface()
        port = Port()

        for interface_name in acl_interface_list:
            if not port.check_port_exists(aruba_ansible_module,
                                          interface_name):
                aruba_ansible_module.module.fail_json(
                    msg="Interface {int} is not configured".format(
                        int=interface_name))

            if (state == 'create') or (state == 'update'):
                update_type = 'insert'
            elif (state == 'delete'):
                update_type = 'delete'
            aruba_ansible_module = interface.update_interface_acl_details(
                aruba_ansible_module, interface_name, acl_name, acl_type,
                acl_direction, update_type)

            if update_type == 'insert':
                aruba_ansible_module.module.log(
                    msg="Attached ACL {acl} of type "
                    "{type} to interface {int}"
                    "".format(acl=acl_name, type=acl_type, int=interface_name))

            if update_type == 'update':
                aruba_ansible_module.module.log(
                    msg="Updated ACL {acl} of type "
                    "{type} attached to interface"
                    " {int}"
                    "".format(acl=acl_name, type=acl_type,
                              int=interface_name))  # NOQA

            if (update_type == 'absent') or (update_type == 'delete'):
                aruba_ansible_module.module.log(
                    msg="Removed ACL {acl} of type"
                    " {type} from interface"
                    " {int}"
                    "".format(acl=acl_name, type=acl_type, int=interface_name))

        aruba_ansible_module.update_switch_config()