Skip to content

Commit 038bcb1

Browse files
andrewjrothpre-commit-ci[bot]Ruchip16
authored
fixed ipcut to support ipv4 (#358)
* fixed ipcut to support ipv4 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add changelog --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ruchi Pakhle <72685035+Ruchip16@users.noreply.github.com> Co-authored-by: Ruchi Pakhle <ruchipakhle@gmail.com>
1 parent 1df7af5 commit 038bcb1

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
minor_changes:
3+
- Previously, the ansible.utils.ipcut filter only supported IPv6 addresses, leading to confusing error messages when used with IPv4 addresses. This fix ensures that the filter now appropriately handles both IPv4 and IPv6 addresses.

plugins/filter/ipcut.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,15 @@ def _ipcut(*args, **kwargs):
105105

106106

107107
def ipcut(value, amount):
108-
ipv6_oct = []
109108
try:
110109
ip = netaddr.IPAddress(value)
111-
ipv6address = ip.bits().replace(":", "")
110+
if ip.version == 6:
111+
ip_bits = ip.bits().replace(":", "")
112+
elif ip.version == 4:
113+
ip_bits = ip.bits().replace(".", "")
114+
else:
115+
msg = "Unknown IP Address Version: {0}".format(ip.version)
116+
raise AnsibleFilterError(msg)
112117
except (netaddr.AddrFormatError, ValueError):
113118
msg = "You must pass a valid IP address; {0} is invalid".format(value)
114119
raise AnsibleFilterError(msg)
@@ -120,20 +125,27 @@ def ipcut(value, amount):
120125
raise AnsibleFilterError(msg)
121126
else:
122127
if amount < 0:
123-
ipsub = ipv6address[amount:]
128+
ipsub = ip_bits[amount:]
124129
else:
125-
ipsub = ipv6address[0:amount]
126-
127-
ipsubfinal = []
128-
129-
for i in range(0, len(ipsub), 16):
130-
oct_sub = i + 16
131-
ipsubfinal.append(ipsub[i:oct_sub])
132-
133-
for i in ipsubfinal:
134-
x = hex(int(i, 2))
135-
ipv6_oct.append(x.replace("0x", ""))
136-
return str(":".join(ipv6_oct))
130+
ipsub = ip_bits[0:amount]
131+
132+
if ip.version == 6:
133+
ipv4_oct = []
134+
for i in range(0, len(ipsub), 16):
135+
oct_sub = i + 16
136+
ipv4_oct.append(
137+
hex(int(ipsub[i:oct_sub], 2)).replace("0x", ""),
138+
)
139+
result = str(":".join(ipv4_oct))
140+
else: # ip.version == 4:
141+
ipv4_oct = []
142+
for i in range(0, len(ipsub), 8):
143+
oct_sub = i + 8
144+
ipv4_oct.append(
145+
str(int(ipsub[i:oct_sub], 2)),
146+
)
147+
result = str(".".join(ipv4_oct))
148+
return result
137149

138150

139151
class FilterModule(object):

tests/unit/plugins/filter/test_ipcut.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,30 @@ class TestIpCut(TestCase):
2121
def setUp(self):
2222
pass
2323

24-
def test_get_last_X_bits(self):
24+
def test_get_last_X_bits_ipv6(self):
2525
"""Get last X bits of Ipv6 address"""
2626

2727
args = ["", "1234:4321:abcd:dcba::17", -80]
2828
result = _ipcut(*args)
2929
self.assertEqual(result, "dcba:0:0:0:17")
3030

31-
def test_get_first_X_bits(self):
31+
def test_get_first_X_bits_ipv6(self):
3232
"""Get first X bits of Ipv6 address"""
3333

3434
args = ["", "1234:4321:abcd:dcba::17", 64]
3535
result = _ipcut(*args)
3636
self.assertEqual(result, "1234:4321:abcd:dcba")
37+
38+
def test_get_last_X_bits_ipv4(self):
39+
"""Get last X bits of Ipv4 address"""
40+
41+
args = ["", "10.2.3.0", -16]
42+
result = _ipcut(*args)
43+
self.assertEqual(result, "3.0")
44+
45+
def test_get_first_X_bits_ipv4(self):
46+
"""Get first X bits of Ipv4 address"""
47+
48+
args = ["", "10.2.3.0", 24]
49+
result = _ipcut(*args)
50+
self.assertEqual(result, "10.2.3")

0 commit comments

Comments
 (0)