diff --git a/geant/gap_ansible/plugins/modules/geant_aggregate_prefixes.py b/geant/gap_ansible/plugins/modules/geant_aggregate_prefixes.py new file mode 100644 index 0000000000000000000000000000000000000000..8780a441c908c941702aced4712e08646719321b --- /dev/null +++ b/geant/gap_ansible/plugins/modules/geant_aggregate_prefixes.py @@ -0,0 +1,90 @@ +#!/usr/bin/python + +# (c) 2024, GEANT Orchestration and Automation Team +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +from ansible.module_utils.basic import AnsibleModule +from aggregate_prefixes import aggregate_prefixes + +DOCUMENTATION = """ +module: geant_aggregate_prefixes +author: +- GEANT Orchestration and Automation Team (@GEANT) +short_description: Aggregate IP prefixes into the minimal set of CIDR blocks +description: +- This module takes a list of IP prefixes and aggregates them into the minimal set of CIDR blocks. +- It is useful for summarizing routes or reducing the size of prefix lists. +version_added: 1.0.0 +options: + prefixes: + description: + - A list of IP prefixes to be aggregated. + - Each prefix should be a valid CIDR block (e.g., 192.0.2.0/32). + type: list + elements: str + required: true +notes: +- This module requires the Python `aggregate-prefixes` library to be installed on the control node. +requirements: +- aggregate-prefixes +""" + +EXAMPLES = """ +# Aggregate a list of prefixes +- name: Aggregate IP prefixes + hosts: localhost + tasks: + - name: Aggregate a list of prefixes + geant_aggregate_prefixes: + prefixes: + - "192.0.2.0/32" + - "192.0.2.1/32" + - "192.0.2.2/32" + register: result + + - name: Display aggregated prefixes + debug: + msg: "Aggregated prefixes: {{ result.aggregated_prefixes }}" +""" + +RETURN = """ +aggregated_prefixes: + description: The aggregated list of prefixes in CIDR format + type: list + returned: success + sample: ['192.0.2.0/31', '192.0.2.2/32'] +""" + + +def geant_aggregate_prefixes(prefixes): + """ + Aggregates a list of IP prefixes into the minimal set of CIDR blocks. + Returns a tuple (success, result). On success, result is a list of strings representing the aggregated prefixes. + On failure, result is an error message. + """ + try: + aggregated = list(aggregate_prefixes(prefixes)) + aggregated_str = [str(net) for net in aggregated] + return True, aggregated_str + except Exception as e: + return False, f"Error during aggregation: {str(e)}" + + +def main(): + module_args = { + "prefixes": {"type": "list", "required": True, "elements": "str"}, + } + + module = AnsibleModule(argument_spec=module_args, supports_check_mode=False) + prefixes = module.params["prefixes"] + success, result = geant_aggregate_prefixes(prefixes) + + if success: + module.exit_json(changed=False, aggregated_prefixes=result) + else: + module.fail_json(msg=result) + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt index 691676c82ba1dd79367447518b6ce463212a41de..3f34f476df2aaafc330989564f582b5cc5c34291 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ ncclient ansible -ansible-lint \ No newline at end of file +ansible-lint +aggregate-prefixes