Exemple #1
0
def main():
    module = AnsibleModule(argument_spec=dict())

    try:
        client = snmp.SnmpClient()

        var_binds = dict()
        var_binds[OID_RND_ACTION] = snmp.Integer32(1)
        client.set(var_binds)

        module.exit_json(changed=True)
    except snmp.SnmpError as e:
        module.fail_json(msg=str(e))
Exemple #2
0
def url_to_var_binds(url, copy_index, offset, var_binds):
    url_info = urlparse.urlparse(url)

    oid_copy_location = OID_RL_COPY_ENTRY + '.' + str(
        offset + OFFSET_LOCATION) + '.' + str(copy_index)
    oid_copy_ip_address = OID_RL_COPY_ENTRY + '.' + str(
        offset + OFFSET_IP_ADDRESS) + '.' + str(copy_index)
    oid_copy_unit_number = OID_RL_COPY_ENTRY + '.' + str(
        offset + OFFSET_UNIT_NUMBER) + '.' + str(copy_index)
    oid_copy_file_name = OID_RL_COPY_ENTRY + '.' + str(
        offset + OFFSET_FILE_NAME) + '.' + str(copy_index)
    oid_copy_file_type = OID_RL_COPY_ENTRY + '.' + str(
        offset + OFFSET_FILE_TYPE) + '.' + str(copy_index)

    scheme = url_info[0]
    if scheme in ('local', 'another-unit'):
        file_type = path_to_file_type(url_info[2])
        if not file_type:
            return False

        var_binds[oid_copy_location] = snmp.Integer32(LOCATION_LOCAL)
        if scheme == 'another-unit':
            var_binds[oid_copy_unit_number] = snmp.Integer32(int(url_info[1]))
        var_binds[oid_copy_file_type] = snmp.Integer32(file_type)
        return True

    if scheme in ('tftp', 'scp', 'http', 'https'):
        ip = url_info[1]
        path = url_info[2]

        if scheme == 'tftp':
            var_binds[oid_copy_location] = snmp.Integer32(LOCATION_TFTP)
        elif scheme == 'scp':
            var_binds[oid_copy_location] = snmp.Integer32(LOCATION_SCP)
        elif scheme == 'http':
            var_binds[oid_copy_location] = snmp.Integer32(LOCATION_HTTP)
        elif scheme == 'https':
            var_binds[oid_copy_location] = snmp.Integer32(LOCATION_HTTPS)

        var_binds[oid_copy_ip_address] = snmp.IpAddress(ip)
        var_binds[oid_copy_file_name] = snmp.OctetString(path)
        return True

    return False
Exemple #3
0
def main():
    module = AnsibleModule(
        argument_spec=dict(version=dict(required=False),
                           image=dict(required=False, choices=['1', '2']),
                           gather_facts=dict(required=False,
                                             type='bool',
                                             choices=BOOLEANS,
                                             default=False),
                           unit=dict(required=False, type='int', default=1)),
        mutually_exclusive=[['version', 'image']],
        required_one_of=[['version', 'image', 'gather_facts']],
        supports_check_mode=True)

    params = module.params

    version = params['version']
    image = params['image']
    gather_facts = params['gather_facts']
    unit = params['unit']

    try:
        client = snmp.SnmpClient()

        oid_rnd_active_software_file = OID_RND_ACTIVE_SOFTWARE_FILE + '.' + str(
            unit)
        oid_rnd_active_software_file_after_reset = OID_RND_ACTIVE_SOFTWARE_FILE_AFTER_RESET + '.' + str(
            unit)
        oid_rnd_image1_version = OID_RND_IMAGE1_VERSION + '.' + str(unit)
        oid_rnd_image2_version = OID_RND_IMAGE2_VERSION + '.' + str(unit)
        """ Gather facts """
        values = client.get(oid_rnd_active_software_file,
                            oid_rnd_active_software_file_after_reset,
                            oid_rnd_image1_version, oid_rnd_image2_version)

        active_image = int(values[oid_rnd_active_software_file])
        reset_image = int(values[oid_rnd_active_software_file_after_reset])
        version1 = str(values[oid_rnd_image1_version])
        version2 = str(values[oid_rnd_image2_version])
        """ Prepare to change image file after reset """
        var_binds = dict()
        if image:
            if reset_image != int(image):
                var_binds[
                    oid_rnd_active_software_file_after_reset] = snmp.Integer32(
                        image)
        elif version:
            if version1 == version:
                if reset_image != IMAGE1:
                    var_binds[
                        oid_rnd_active_software_file_after_reset] = snmp.Integer32(
                            IMAGE1)
            elif version2 == version:
                if reset_image != IMAGE2:
                    var_binds[
                        oid_rnd_active_software_file_after_reset] = snmp.Integer32(
                            IMAGE2)
            else:
                module.fail_json(msg="No firmware with that version found")
        """ Generate facts """
        if gather_facts:
            facts = dict(ciscosb_firmware_active_image=active_image,
                         ciscosb_firmware_reset_image=reset_image,
                         ciscosb_firmware_version1=version1,
                         ciscosb_firmware_version2=version2)

            if active_image == IMAGE1:
                facts['ciscosb_firmware_active_version'] = version1
            elif active_image == IMAGE2:
                facts['ciscosb_firmware_active_version'] = version2
            else:
                facts['ciscosb_firmware_active_version'] = 'unknown'

            if reset_image == IMAGE1:
                facts['ciscosb_firmware_reset_version'] = version1
            elif reset_image == IMAGE2:
                facts['ciscosb_firmware_reset_version'] = version2
            else:
                facts['ciscosb_firmware_reset_version'] = 'unknown'
        """ Finalize """
        if var_binds:
            changed = True
        else:
            changed = False

        if not module.check_mode:
            client.set(var_binds)

        if gather_facts:
            module.exit_json(changed=changed, ansible_facts=facts)
        else:
            module.exit_json(changed=changed)

    except snmp.SnmpError as e:
        module.fail_json(msg=str(e))
Exemple #4
0
def main():
    module = AnsibleModule(argument_spec=dict(
        ifname=dict(required=False),
        ifindex=dict(required=False),
        port=dict(required=False),
        gvrp=dict(required=False, choices=BOOLEANS),
        vlan=dict(required=False),
        state=dict(required=False, choices=['present', 'absent']),
        name=dict(required=False),
        pvid=dict(required=False)),
                           supports_check_mode=True)

    params = module.params

    ifname = params['ifname']
    ifindex = params['ifindex']
    port = params['port']
    gvrp = params['gvrp']
    vlan = params['vlan']
    state = params['state']
    name = params['name']
    pvid = params['pvid']

    has_selector = ifname or ifindex or port

    var_names = []

    client = snmp.SnmpClient()

    if has_selector:
        var_binds = dict()

        if not port:
            if ifindex:
                port = ifindex_to_port(client, ifindex)
            elif ifname:
                port = ifname_to_port(client, ifname)
            if not port:
                module.fail_json(msg="No such port/interface")

        oid_dot1q_port_gvrp_status = OID_DOT1Q_PORT_GVRP_STATUS + '.' + str(
            port)

        if gvrp:
            var_names.append(oid_dot1q_port_gvrp_status)

        values = client.get(*var_names)

        if gvrp:
            gvrp = module.boolean(gvrp)
            if gvrp and int(
                    values[oid_dot1q_port_gvrp_status]) != SNMP_ENABLED:
                var_binds[oid_dot1q_port_gvrp_status] = snmp.Integer32(
                    SNMP_ENABLED)
            elif not gvrp and int(
                    values[oid_dot1q_port_gvrp_status]) != SNMP_DISABLED:
                var_binds[oid_dot1q_port_gvrp_status] = snmp.Integer32(
                    SNMP_DISABLED)

    else:
        var_binds = dict()

        if gvrp:
            var_names.append(OID_DOT1Q_GVRP_STATUS)

        values = client.get(*var_names)

        if gvrp:
            gvrp = module.boolean(gvrp)
            if gvrp and int(values[OID_DOT1Q_GVRP_STATUS]) != SNMP_ENABLED:
                var_binds[OID_DOT1Q_GVRP_STATUS] = snmp.Integer32(SNMP_ENABLED)
            elif not gvrp and int(
                    values[OID_DOT1Q_GVRP_STATUS]) != SNMP_DISABLED:
                var_binds[OID_DOT1Q_GVRP_STATUS] = snmp.Integer32(
                    SNMP_DISABLED)

    if not var_binds:
        module.exit_json(changed=False)

    if module.check_mode:
        module.exit_json(changed=True)

    client.set(var_binds)
    module.exit_json(changed=True)
Exemple #5
0
def main():
    module = AnsibleModule(
        argument_spec = dict(
            ifname      = dict(required=False),
            ifindex     = dict(required=False),

            alias     = dict(required=False),
            status     = dict(required=False, choices=['up', 'down']),
            traps     = dict(required=False, choices=BOOLEANS),
            promisc   = dict(required=False, choices=BOOLEANS)
        ),
        mutually_exclusive=[['ifname', 'ifindex']],
        required_one_of=[['ifname', 'ifindex'],
                         ['alias', 'status', 'traps', 'promisc']],
        supports_check_mode=True
    )

    params = module.params

    ifname = params['ifname']
    ifindex = params['ifindex']
    alias = params['alias']
    status = params['status']
    traps = params['traps']
    promisc = params['promisc']

    try:
        client = snmp.SnmpClient()

        if not ifindex:
            ifindex = get_ifindex(client, ifname)
            if not ifindex:
                module.fail_json(msg="No such interface")

        oid_if_alias = OID_IF_ALIAS + '.' + str(ifindex)
        oid_if_admin_status = OID_IF_ADMIN_STATUS + '.' + str(ifindex)
        oid_if_link_up_down_trap_enable = OID_IF_LINK_UP_DOWN_TRAP_ENABLE + '.' + str(ifindex)
        oid_if_promiscuous_mode = OID_IF_PROMISCUOUS_MODE + '.' + str(ifindex)

        var_names = []
        if alias is not None:
            var_names.append(oid_if_alias)
        if status is not None:
            var_names.append(oid_if_admin_status)
        if traps is not None:
            var_names.append(oid_if_link_up_down_trap_enable)
        if promisc is not None:
            var_names.append(oid_if_promiscuous_mode)

        values = client.get(*var_names)

        var_binds = dict()

        if alias:
            value = str(values[oid_if_alias])
            if value != alias:
                var_binds[oid_if_alias] = snmp.OctetString(alias)

        if status:
            if_status = int(values[oid_if_admin_status])
            if status == 'up' and if_status != IF_ADMIN_STATUS_UP:
                var_binds[oid_if_admin_status] = snmp.Integer32(IF_ADMIN_STATUS_UP)
            elif status == 'down' and if_status != IF_ADMIN_STATUS_DOWN:
                var_binds[oid_if_admin_status] = snmp.Integer32(IF_ADMIN_STATUS_DOWN)

        if traps:
            traps = module.boolean(traps)
            if_link_up_down_trap_enable = int(values[oid_if_link_up_down_trap_enable])
            if traps and if_link_up_down_trap_enable != IF_LINK_UP_DOWN_TRAP_ENABLE_ENABLED:
                var_binds[oid_if_link_up_down_trap_enable] = snmp.Integer32(IF_LINK_UP_DOWN_TRAP_ENABLE_ENABLED)
            elif not traps and if_link_up_down_trap_enable != IF_LINK_UP_DOWN_TRAP_ENABLE_DISABLED:
                var_binds[oid_if_link_up_down_trap_enable] = snmp.Integer32(IF_LINK_UP_DOWN_TRAP_ENABLE_DISABLED)

        if promisc:
            promisc = module.boolean(promisc)
            if_promiscuous_mode = int(values[oid_if_promiscuous_mode])
            if promisc and if_promiscuous_mode != SNMP_TRUE:
                var_binds[oid_if_promiscuous_mode] = snmp.Integer32(SNMP_TRUE)
            elif not promisc and if_promiscuous_mode != SNMP_FALSE:
                var_binds[oid_if_promiscuous_mode] = snmp.Integer32(SNMP_FALSE)

        if not var_binds:
            module.exit_json(changed=False)

        if module.check_mode:
            module.exit_json(changed=True)

        client.set(var_binds)
        module.exit_json(changed=True)
    except snmp.SnmpError as e:
        module.fail_json(msg=str(e))
Exemple #6
0
def main():
    module = AnsibleModule(
        argument_spec=dict(src=dict(required=True),
                           dest=dict(required=True),
                           secure_data=dict(required=False,
                                            choices=[
                                                'exclude', 'include-encrypted',
                                                'include-decrypted', 'default'
                                            ],
                                            default='default')))

    params = module.params

    src = params['src']
    dest = params['dest']
    secure_data = params['secure_data']

    try:
        client = snmp.SnmpClient()

        copy_index = random.randint(1, 2147483647)
        copy_history_index = copy_index

        oid_rl_copy_row_status = OID_RL_COPY_ROW_STATUS + '.' + str(copy_index)
        oid_rl_copy_history_index = OID_RL_COPY_HISTORY_INDEX + '.' + str(
            copy_index)

        oid_rl_copy_history_operation_state = OID_RL_COPY_HISTORY_OPERATION_STATE + '.' + str(
            copy_history_index)
        oid_rl_copy_history_row_status = OID_RL_COPY_HISTORY_ROW_STATUS + '.' + str(
            copy_history_index)
        oid_rl_copy_history_error_message = OID_RL_COPY_HISTORY_ERROR_MESSAGE + '.' + str(
            copy_history_index)

        var_binds = dict()
        if not url_to_var_binds(src, copy_index, 3, var_binds):
            module.fail_json(msg='Invalid source url')
        if not url_to_var_binds(dest, copy_index, 8, var_binds):
            module.fail_json(msg='Invalid destination url')
        var_binds[oid_rl_copy_row_status] = snmp.Integer32(
            ROW_STATUS_CREATE_AND_GO)
        var_binds[oid_rl_copy_history_index] = snmp.Integer32(
            copy_history_index)

        client.set(var_binds)

        while True:
            values = client.get(oid_rl_copy_history_operation_state)
            state = int(values[oid_rl_copy_history_operation_state])
            if state != STATE_UPLOAD_IN_PROGRESS and state != STATE_DOWNLOAD_IN_PROGRESS:
                break
            time.sleep(5)

        if state != STATE_COPY_FINISHED:
            try:
                values = client.get(oid_rl_copy_history_error_message)
                error_message = str(values[oid_rl_copy_history_error_message])
            except snmp.SnmpError as e:
                error_message = None

        var_binds = dict()
        var_binds[oid_rl_copy_history_row_status] = snmp.Integer32(
            ROW_STATUS_DESTROY)
        client.set(var_binds)

        if state == STATE_COPY_FINISHED:
            module.exit_json(changed=True)
        elif error_message:
            module.fail_json(msg=error_message)
        elif state == STATE_COPY_TIMEDOUT:
            module.fail_json(msg='Copy timed out')
        else:
            module.fail_json(msg='Copy failed')

    except snmp.SnmpError as e:
        module.fail_json(msg=str(e))