Skip to content

Conversation

@jeffkala
Copy link
Collaborator

@jeffkala jeffkala commented Jun 14, 2024

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

Copy link
Collaborator

@qduk qduk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@jeffkala jeffkala merged commit 33b26a4 into develop Sep 10, 2024
@jeffkala jeffkala deleted the jkala-extend-ipaddr-j2-filters branch September 10, 2024 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants