HEX
Server: LiteSpeed
System: Linux kapuas.iixcp.rumahweb.net 5.14.0-427.42.1.el9_4.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Nov 1 14:58:02 EDT 2024 x86_64
User: mirz4654 (1666)
PHP: 8.1.33
Disabled: system,exec,escapeshellarg,escapeshellcmd,passthru,proc_close,proc_get_status,proc_nice,proc_open,proc_terminate,shell_exec,popen,pclose,dl,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setsid,posix_setuid,posix_setpgid,ini_alter,show_source,define_syslog_variables,symlink,syslog,openlog,openlog,closelog,ocinumcols,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dll,ftp,myshellexec,socket_bind,mail,posix_getwpuid
Upload Files
File: //lib/python3.9/site-packages/ansible_collections/cisco/aci/plugins/lookup/interface_range.py
# -*- coding: utf-8 -*-

# Copyright: (c) 2022, Akini Ross (@akinross) <[email protected]>
# 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))