Skip to content
Snippets Groups Projects
Commit 0e513b15 authored by Mohammad Torkashvand's avatar Mohammad Torkashvand
Browse files

add aggregate_prefixes writen by Hakan Salim with a tiny changes

parent f5bdb07b
No related branches found
No related tags found
No related merge requests found
Pipeline #91025 passed
#!/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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment