Name | j2ipaddr JSON |
Version |
1.0.6
JSON |
| download |
home_page | |
Summary | Jinja2 filters for IP addresses, the easy way |
upload_time | 2023-05-31 15:10:45 |
maintainer | |
docs_url | None |
author | |
requires_python | >=3.8 |
license | |
keywords |
jinja2
jinja
filters
netaddr
ipaddr
networking
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# j2ipaddr
Jinja2 filters for IP addresses, the easy way
## Why
On networking and network automation, we need to extract info about IP addresses as a combination of two values:
- a host address
- a subnet mask
For 10.10.10.5/24, the host address is 10.10.10.5 and the subnet mask is 255.255.255.0, and its prefix length is 24.
There is additional information we can infer from this single item, as its network address, broadcast address.
Useful data for network engineers are wildcards or hostmasks, network size, class, type, and so on.
Jinja2 provides several integrated filters to work with, however it can be complicated to use complex data types.
Ansible provides a way to work this on its [ansible.utils.ipaddr](https://docs.ansible.com/ansible/latest/collections/ansible/utils/docsite/filters_ipaddr.html) collection.
However, probably you won't need the entire Ansible package just to be able to use it.
This package intends to provide a set of filters and handler to the Python 3 [netaddr](https://netaddr.readthedocs.io/en/latest/) module, on a way that is hopefully easy and lightweight to use.
## What
Included filters are the following:
### ip_address(addr)
Returns an IP address for a combination of IP address and subnet mask
``` Python
ip_address('10.10.10.5/24')
> 10.10.10.5
```
``` Jinja2
{{ '10.10.10.5/24 | ip_address }}
> 10.10.10.5
```
### ip_prefixlen(addr)
Returns a prefix length for a combination of IP address and subnet mask
``` Python
ip_prefixlen('10.10.10.5/24')
> 24
```
``` Jinja2
{{ '10.10.10.5/24 | ip_prefixlen }}
> 24
```
### ip_netmask(addr)
Returns a subnet mask for a combination of IP address and subnet mask
``` Python
ip_netmask('10.10.10.5/24')
> 255.255.255.0
```
``` Jinja2
{{ '10.10.10.5/24 | ip_netmask }}
> 255.255.255.0
```
### ip_hostmask(addr)
Returns a wilcard or hostmask for a combination of IP address and subnet mask
``` Python
ip_hostmask('10.10.10.5/24')
> 0.0.0.255
```
``` Jinja2
{{ '10.10.10.5/24 | ip_hostmask }}
> 0.0.0.255
```
### ip_wildcard(addr)
Alias for ip_hostmask(addr)
``` Python
ip_wildcard('10.10.10.5/24')
> 0.0.0.255
```
``` Jinja2
{{ '10.10.10.5/24 | ip_wildcard }}
> 0.0.0.255
```
### ip_network(addr)
Returns a network address for a combination of IP address and subnet mask
``` Python
ip_network('10.10.10.5/24')
> 10.10.10.0
```
``` Jinja2
{{ '10.10.10.5/24 | ip_network_hosts_size }}
> 10.10.10.0
```
### ip_broadcast(addr)
Returns a broadcast address for a combination of IP address and subnet mask
``` Python
ip_broadcast('10.10.10.5/24')
> 10.10.10.255
```
``` Jinja2
{{ '10.10.10.5/24 | ip_broadcast }}
> 10.10.10.255
```
### ip_network_hosts_size(addr)
Returns the size of the subnet for a combination of IP address and subnet mask
``` Python
ip_network_hosts_size('10.10.10.5/24')
> 255
```
``` Jinja2
{{ '10.10.10.5/24 | ip_network_hosts_size }}
> 255
```
### ip_network_first(addr)
Returns the first usable address in network address for a combination of IP address and subnet mask
``` Python
ip_network('10.10.10.5/24')
> 10.10.10.1
```
``` Jinja2
{{ '10.10.10.5/24 | ip_network_hosts_size }}
> 10.10.10.1
```
### ip_network_last(addr)
Returns the last usable address in network address for a combination of IP address and subnet mask
``` Python
ip_network('10.10.10.5/24')
> 10.10.10.254
```
``` Jinja2
{{ '10.10.10.5/24 | ip_network_hosts_size }}
> 10.10.10.254
```
## How
Simply install with pip.
``` Console
pip install j2ipaddr
```
To insert the filters on your Jinja2 processor, simply use the following syntax.
The filter name can be changed by adjusting the dict key name.
``` Python
import jinja2
import j2ipaddr.filters
jinja2.filters.FILTERS['ip_prefixlen'] = filters.ip_prefixlen
```
Or, probably an easier way, use the following one-liner to load all the filters into your Jinja2 filters
``` Python
import jinja2
from j2ipaddr import filters
jinja2.filters.FILTERS.update(filters.load_all())
```
On your templates, you can do this as an example:
### Variables
``` YAML
host:
interfaces:
Te1/0/1:
ipv4_addresses:
- 10.10.10.5/24
```
### Template
``` YAML
router ospf 10
network {{host.interfaces.Te1/0/1.ipv4_addresses[0] | ip_network }} {{host.interfaces.Te1/0/1.ipv4_addresses[0] | ip_wildcard }} area 0.0.0.0
```
The output would looks like this:
``` Text
router ospf 10
network 10.0.0.0 0.0.0.255 area 0.0.0.0
```
Raw data
{
"_id": null,
"home_page": "",
"name": "j2ipaddr",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "",
"keywords": "jinja2,jinja,filters,netaddr,ipaddr,networking",
"author": "",
"author_email": "Arturo Baldo <baldoarturo@mail.com>",
"download_url": "https://files.pythonhosted.org/packages/92/66/5457e66edd890619e3fbd3fb52705be464693f0849585b9b7c359c283773/j2ipaddr-1.0.6.tar.gz",
"platform": null,
"description": "# j2ipaddr\n\nJinja2 filters for IP addresses, the easy way\n\n## Why\n\nOn networking and network automation, we need to extract info about IP addresses as a combination of two values:\n\n- a host address\n- a subnet mask\n\nFor 10.10.10.5/24, the host address is 10.10.10.5 and the subnet mask is 255.255.255.0, and its prefix length is 24.\n\nThere is additional information we can infer from this single item, as its network address, broadcast address.\n\nUseful data for network engineers are wildcards or hostmasks, network size, class, type, and so on.\n\nJinja2 provides several integrated filters to work with, however it can be complicated to use complex data types.\n\nAnsible provides a way to work this on its [ansible.utils.ipaddr](https://docs.ansible.com/ansible/latest/collections/ansible/utils/docsite/filters_ipaddr.html) collection.\n\nHowever, probably you won't need the entire Ansible package just to be able to use it.\n\nThis package intends to provide a set of filters and handler to the Python 3 [netaddr](https://netaddr.readthedocs.io/en/latest/) module, on a way that is hopefully easy and lightweight to use.\n\n## What\n\nIncluded filters are the following:\n\n### ip_address(addr)\n\nReturns an IP address for a combination of IP address and subnet mask\n\n``` Python\nip_address('10.10.10.5/24')\n> 10.10.10.5\n```\n\n``` Jinja2\n{{ '10.10.10.5/24 | ip_address }}\n> 10.10.10.5\n```\n\n### ip_prefixlen(addr)\n\nReturns a prefix length for a combination of IP address and subnet mask\n\n``` Python\nip_prefixlen('10.10.10.5/24')\n> 24\n```\n\n``` Jinja2\n{{ '10.10.10.5/24 | ip_prefixlen }}\n> 24\n```\n\n### ip_netmask(addr)\n\nReturns a subnet mask for a combination of IP address and subnet mask\n\n``` Python\nip_netmask('10.10.10.5/24')\n> 255.255.255.0\n```\n\n``` Jinja2\n{{ '10.10.10.5/24 | ip_netmask }}\n> 255.255.255.0\n```\n\n### ip_hostmask(addr)\n\nReturns a wilcard or hostmask for a combination of IP address and subnet mask\n\n``` Python\nip_hostmask('10.10.10.5/24')\n> 0.0.0.255\n```\n\n``` Jinja2\n{{ '10.10.10.5/24 | ip_hostmask }}\n> 0.0.0.255\n```\n\n### ip_wildcard(addr)\n\nAlias for ip_hostmask(addr)\n\n``` Python\nip_wildcard('10.10.10.5/24')\n> 0.0.0.255\n```\n\n``` Jinja2\n{{ '10.10.10.5/24 | ip_wildcard }}\n> 0.0.0.255\n```\n\n### ip_network(addr)\n\nReturns a network address for a combination of IP address and subnet mask\n\n``` Python\nip_network('10.10.10.5/24')\n> 10.10.10.0\n```\n\n``` Jinja2\n{{ '10.10.10.5/24 | ip_network_hosts_size }}\n> 10.10.10.0\n```\n\n### ip_broadcast(addr)\n\nReturns a broadcast address for a combination of IP address and subnet mask\n\n``` Python\nip_broadcast('10.10.10.5/24')\n> 10.10.10.255\n```\n\n``` Jinja2\n{{ '10.10.10.5/24 | ip_broadcast }}\n> 10.10.10.255\n```\n\n### ip_network_hosts_size(addr)\n\nReturns the size of the subnet for a combination of IP address and subnet mask\n\n``` Python\nip_network_hosts_size('10.10.10.5/24')\n> 255\n```\n\n``` Jinja2\n{{ '10.10.10.5/24 | ip_network_hosts_size }}\n> 255\n```\n\n### ip_network_first(addr)\n\nReturns the first usable address in network address for a combination of IP address and subnet mask\n\n``` Python\nip_network('10.10.10.5/24')\n> 10.10.10.1\n```\n\n``` Jinja2\n{{ '10.10.10.5/24 | ip_network_hosts_size }}\n> 10.10.10.1\n```\n\n### ip_network_last(addr)\n\nReturns the last usable address in network address for a combination of IP address and subnet mask\n\n``` Python\nip_network('10.10.10.5/24')\n> 10.10.10.254\n```\n\n``` Jinja2\n{{ '10.10.10.5/24 | ip_network_hosts_size }}\n> 10.10.10.254\n```\n\n## How\n\nSimply install with pip.\n\n``` Console\npip install j2ipaddr\n```\n\nTo insert the filters on your Jinja2 processor, simply use the following syntax.\nThe filter name can be changed by adjusting the dict key name.\n\n``` Python\nimport jinja2\nimport j2ipaddr.filters\njinja2.filters.FILTERS['ip_prefixlen'] = filters.ip_prefixlen\n```\n\nOr, probably an easier way, use the following one-liner to load all the filters into your Jinja2 filters\n\n``` Python\nimport jinja2\nfrom j2ipaddr import filters\njinja2.filters.FILTERS.update(filters.load_all())\n```\n\nOn your templates, you can do this as an example:\n\n### Variables\n\n``` YAML\nhost:\n interfaces:\n Te1/0/1:\n ipv4_addresses:\n - 10.10.10.5/24\n```\n\n### Template\n\n``` YAML\nrouter ospf 10\n network {{host.interfaces.Te1/0/1.ipv4_addresses[0] | ip_network }} {{host.interfaces.Te1/0/1.ipv4_addresses[0] | ip_wildcard }} area 0.0.0.0\n```\n\nThe output would looks like this:\n\n``` Text\nrouter ospf 10\n network 10.0.0.0 0.0.0.255 area 0.0.0.0\n```\n",
"bugtrack_url": null,
"license": "",
"summary": "Jinja2 filters for IP addresses, the easy way",
"version": "1.0.6",
"project_urls": {
"Homepage": "https://github.com/baldoarturo/j2ipaddr"
},
"split_keywords": [
"jinja2",
"jinja",
"filters",
"netaddr",
"ipaddr",
"networking"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "7468737c4b48dacd2e83b10f14dd5541475701c6adb8ee23a2b0137c3e48c2cc",
"md5": "9f6a405f44091e913f7724459260f7d4",
"sha256": "6a49bb3058c59f7d81b9faa68bbaf8c1376a2fa6da96fa94fbd9469e143cdca7"
},
"downloads": -1,
"filename": "j2ipaddr-1.0.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9f6a405f44091e913f7724459260f7d4",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 15657,
"upload_time": "2023-05-31T15:10:44",
"upload_time_iso_8601": "2023-05-31T15:10:44.147057Z",
"url": "https://files.pythonhosted.org/packages/74/68/737c4b48dacd2e83b10f14dd5541475701c6adb8ee23a2b0137c3e48c2cc/j2ipaddr-1.0.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "92665457e66edd890619e3fbd3fb52705be464693f0849585b9b7c359c283773",
"md5": "607db1de7378a3cfe3f2263f808a0dca",
"sha256": "fd826a56bf060fbb2c57ff6f178b08767a7a1e8a60f90f608b523a1fb61bf45c"
},
"downloads": -1,
"filename": "j2ipaddr-1.0.6.tar.gz",
"has_sig": false,
"md5_digest": "607db1de7378a3cfe3f2263f808a0dca",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 15331,
"upload_time": "2023-05-31T15:10:45",
"upload_time_iso_8601": "2023-05-31T15:10:45.977223Z",
"url": "https://files.pythonhosted.org/packages/92/66/5457e66edd890619e3fbd3fb52705be464693f0849585b9b7c359c283773/j2ipaddr-1.0.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-05-31 15:10:45",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "baldoarturo",
"github_project": "j2ipaddr",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "j2ipaddr"
}