ansible.utils.cidr_merge (4.0.0) — filter

This filter can be used to merge subnets or individual addresses.

| "added in version" 2.5.0 of ansible.utils"

Authors: Ashwini Mhatre (@amhatre)

Install collection

Install with ansible-galaxy collection install ansible.utils:==4.0.0


Add to requirements.yml

  collections:
    - name: ansible.utils
      version: 4.0.0

Description

This filter can be used to merge subnets or individual addresses into their minimal representation, collapsing

overlapping subnets and merging adjacent ones wherever possible.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
#### examples
- name: cidr_merge with merge action
  ansible.builtin.set_fact:
    value:
      - 192.168.0.0/17
      - 192.168.128.0/17
      - 192.168.128.1
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- debug:
    msg: '{{ value|ansible.utils.cidr_merge }}'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# TASK [cidr_merge with merge action] **********************************************************************************
# ok: [localhost] => {
#     "ansible_facts": {
#         "value": [
#             "192.168.0.0/17",
#             "192.168.128.0/17",
#             "192.168.128.1"
#         ]
#     },
#     "changed": false
# }
# TASK [debug] *********************************************************************************************************
# ok: [loalhost] => {
#     "msg": [
#         "192.168.0.0/16"
#     ]
# }

- name: Cidr_merge with span.
  ansible.builtin.set_fact:
    value:
      - 192.168.1.1
      - 192.168.1.2
      - 192.168.1.3
      - 192.168.1.4
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- debug:
    msg: '{{ value|ansible.utils.cidr_merge(''span'') }}'

Inputs

    
value:
    description:
    - list of subnets or individual address to be merged
    elements: str
    required: true
    type: list

action:
    default: merge
    description:
    - Action to be performed.example merge,span
    type: str

Outputs

data:
  description:
  - Returns a minified list of subnets or a single subnet that spans all of the inputs.
  type: raw