networktocode / netutils

Python library that is a collection of functions and objects for common network automation tasks.
https://netutils.readthedocs.io/
Other
214 stars 48 forks source link

Extend ip_network extension to support method calls with kwargs #534

Closed jeffkala closed 2 days ago

jeffkala commented 3 months ago

ip_network has additional method calls that support additional kwargs to be passed in. This adds that support. For clarity, ip_address and ip_interface don't have these, so the extension is only on ip_network.

Example

>>> list(ipaddress_network('192.168.1.0/28', 'subnets', new_prefix=30))
[IPv4Network('192.168.1.0/30'), IPv4Network('192.168.1.4/30'), IPv4Network('192.168.1.8/30'), IPv4Network('192.168.1.12/30')]

or jinja2

from jinja2 import Environment, select_autoescape
from netutils.utils import jinja2_convenience_function

template_string = """
Example:
{% for sub in "192.168.1.0/28" | ipaddress_network('subnets', new_prefix=30) | list %}
{{ sub.with_netmask }}
{% endfor %}
"""

env = Environment(
    autoescape=select_autoescape()
)
env.filters.update(jinja2_convenience_function())
template = env.from_string(template_string)
result = template.render()
print(result)
>>> print(result)

Example:

192.168.1.0/255.255.255.252

192.168.1.4/255.255.255.252

192.168.1.8/255.255.255.252

192.168.1.12/255.255.255.252