Skip to content

Commit 30f62b5

Browse files
committed
Add is_subnet filter to collection
Sometimes it's useful to know if a subnet belongs to another. For that purpose we created `is_subnet` filter. It simply returns `true` if a subnet belongs to another and `false` if not or if both subnets are equal.
1 parent 55fc9a1 commit 30f62b5

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

plugins/filter/phpipam.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright (c) 2020 Christian Meißner
2+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3+
4+
5+
from __future__ import (absolute_import, division, print_function)
6+
__metaclass__ = type
7+
8+
9+
import ipaddress
10+
11+
12+
class FilterModule(object):
13+
"""Define useful filter in collection."""
14+
15+
def filters(self):
16+
"""Export filter for ansible.
17+
18+
:return: return a dictionary of filters
19+
:rtype: dict
20+
"""
21+
return {
22+
'is_subnet': self.is_subnet,
23+
}
24+
25+
def is_subnet(self, children, parent):
26+
"""Check if a subnet belongs to another.
27+
28+
First argument is a subnet second another. If the first subnet belongs to second
29+
30+
:param children: [description]
31+
:type children: [type]
32+
:param parent: [description]
33+
:type parent: [type]
34+
:return: [description]
35+
:rtype: [type]
36+
"""
37+
c = ipaddress.ip_network(children)
38+
p = ipaddress.ip_network(parent)
39+
40+
if not c.subnet_of(p) or c == p:
41+
return False
42+
else:
43+
return c.subnet_of(p)

0 commit comments

Comments
 (0)