Пример #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_vlan_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.module_utils.aoscx_pyaoscx import Session
        from pyaoscx.session import Session as Pyaoscx_Session
        from pyaoscx.device import Device

        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_vlan_list = ansible_module.params['acl_vlan_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 Device Object
        device = Device(s)

        for vlan_name in acl_vlan_list:
            if state == 'delete':
                # Create VLAN Object
                vlan = device.vlan(vlan_name)
                # Delete acl
                if acl_direction == 'in':
                    vlan.detach_acl_in(acl_name, acl_type)
                if acl_direction == 'out':
                    vlan.detach_acl_out(acl_name, acl_type)
                # Changed
                result['changed'] = True

            if state == 'create' or state == 'update':
                # Create VLAN Object
                vlan = device.vlan(vlan_name)
                # Verify if interface was create
                if vlan.was_modified():
                    # Changed
                    result['changed'] = True
                # Set variables
                modified_op1 = False
                modified_op2 = False
                # Update ACL inside VLAN
                if acl_direction == 'in':
                    modified_op1 = vlan.attach_acl_in(acl_name, acl_type)
                if acl_direction == 'out':
                    modified_op2 = vlan.attach_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_vlan_list = aruba_ansible_module.module.params['acl_vlan_list']
        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']

        acl_type_prefix = ""
        if acl_type == "ipv4":
            acl_type_prefix = "aclv4"
        elif acl_type == "ipv6":
            acl_type_prefix = "aclv6"
        elif acl_type == "mac":
            acl_type_prefix = "aclmac"

        vlan = VLAN()

        for vlan_id in acl_vlan_list:
            field1 = '{type}_{dir}_cfg'.format(type=acl_type_prefix,
                                               dir=acl_direction)
            value1 = '{name}/{type}'.format(name=acl_name, type=acl_type)
            field2 = '{type}_{dir}_cfg_version'.format(type=acl_type_prefix,
                                                       dir=acl_direction)
            value2 = randint(-900719925474099, 900719925474099)

            vlan_fields = {field1: value1, field2: value2}

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

                existing_values = vlan.get_vlan_fields_values(
                    aruba_ansible_module, vlan_id, [field1])

                if field1 in existing_values.keys():
                    if existing_values[field1] != vlan_fields[field1]:
                        aruba_ansible_module = vlan.update_vlan_fields(
                            aruba_ansible_module,
                            vlan_id,
                            vlan_fields,
                            update_type='insert')  # NOQA
                else:
                    aruba_ansible_module = vlan.update_vlan_fields(
                        aruba_ansible_module,
                        vlan_id,
                        vlan_fields,
                        update_type='insert')  # NOQA

                if state == 'create':
                    aruba_ansible_module.module.log(
                        msg=" Inserted ACL {name} of "
                        "type {type} to VLAN {id}"
                        "".format(name=acl_name, type=acl_type,
                                  id=vlan_id))  # NOQA

                if state == 'update':
                    aruba_ansible_module.module.log(
                        msg=" Updated  ACL {name} of "
                        "type {type} to VLAN {id}"
                        "".format(name=acl_name, type=acl_type,
                                  id=vlan_id))  # NOQA
            elif state == 'delete':
                aruba_ansible_module = vlan.update_vlan_fields(
                    aruba_ansible_module,
                    vlan_id,
                    vlan_fields,
                    update_type='delete')  # NOQA
                aruba_ansible_module.module.log(
                    msg="Deleted ACL {name} of type "
                    "{type} from VLAN {id}"
                    "".format(name=acl_name, type=acl_type,
                              id=vlan_id))  # NOQA

        aruba_ansible_module.update_switch_config()
Пример #2
0
def main():
    module_args = dict(vlan_id=dict(type='int', required=True),
                       name=dict(type='str', default=None),
                       description=dict(type='str', default=None),
                       admin_state=dict(type='str',
                                        default=None,
                                        choices=['up', 'down']),
                       state=dict(type='str',
                                  default='create',
                                  choices=['create', 'delete', 'update']))

    # Version management
    try:

        from ansible.module_utils.aoscx_pyaoscx import Session
        from pyaoscx.session import Session as Pyaoscx_Session
        from pyaoscx.device import Device

        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
        vlan_id = ansible_module.params['vlan_id']
        vlan_name = ansible_module.params['name']
        if vlan_name is None:
            vlan_name = "VLAN{vlan_id}".format(vlan_id=vlan_id)
        description = ansible_module.params['description']
        admin_state = ansible_module.params['admin_state']
        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 Device Object
        device = Device(s)

        if state == 'delete':
            # Create Vlan Object
            vlan = device.vlan(vlan_id)
            # Delete it
            vlan.delete()
            # Changed
            result['changed'] = True

        elif state == 'update' or state == 'create':
            # Create Vlan with incoming attributes, in case VLAN does not exist
            # inside device
            vlan = device.vlan(vlan_id, vlan_name, description, "static",
                               admin_state)

            if vlan.was_modified():
                # Changed
                result['changed'] = True

        # Exit
        ansible_module.exit_json(**result)

    # Use Older version
    else:

        aruba_ansible_module = ArubaAnsibleModule(module_args=module_args)

        vlan_id = aruba_ansible_module.module.params['vlan_id']
        vlan_name = aruba_ansible_module.module.params['name']
        description = aruba_ansible_module.module.params['description']
        admin_state = aruba_ansible_module.module.params['admin_state']
        state = aruba_ansible_module.module.params['state']

        vlan = VLAN()

        if state == 'delete':
            aruba_ansible_module = vlan.delete_vlan(aruba_ansible_module,
                                                    vlan_id)

        if state == 'create':
            aruba_ansible_module = vlan.create_vlan(aruba_ansible_module,
                                                    vlan_id)

            if vlan_name is not None:
                name = vlan_name
            else:
                name = "VLAN " + str(vlan_id)

            if admin_state is None:
                admin_state = 'up'

            vlan_fields = {
                "name": name,
                "admin": admin_state,
                "type": "static"
            }
            if description is not None:
                vlan_fields["description"] = description
            aruba_ansible_module = vlan.update_vlan_fields(
                aruba_ansible_module,
                vlan_id,
                vlan_fields,
                update_type='insert')

        if state == 'update':
            vlan_fields = {}
            if admin_state is not None:
                vlan_fields['admin'] = admin_state

            if description is not None:
                vlan_fields['description'] = description

            if state is not None:
                vlan_fields['state'] = state

            aruba_ansible_module = vlan.update_vlan_fields(
                aruba_ansible_module,
                vlan_id,
                vlan_fields,
                update_type='update')

        aruba_ansible_module.update_switch_config()