Bugfix ip4 hex (#292)
* Fix bug in ipv4_hex filter. * Fix issue in ipv4_hex * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update tests/integration/targets/utils_ipaddr_filter/tasks/ip4_hex.yaml Co-authored-by: Sagar Paul <sagpaul@redhat.com> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sagar Paul <sagpaul@redhat.com>pull/294/head
parent
1c31436512
commit
827e88dfbf
|
@ -0,0 +1,3 @@
|
||||||
|
---
|
||||||
|
bugfixes:
|
||||||
|
- Validate input for ipv4_hex(https://github.com/ansible-collections/ansible.utils/issues/281)
|
|
@ -110,6 +110,11 @@ def _ip4_hex(*args, **kwargs):
|
||||||
|
|
||||||
def ip4_hex(arg, delimiter=""):
|
def ip4_hex(arg, delimiter=""):
|
||||||
"""Convert an IPv4 address to Hexadecimal notation"""
|
"""Convert an IPv4 address to Hexadecimal notation"""
|
||||||
|
try:
|
||||||
|
ip = netaddr.IPAddress(arg)
|
||||||
|
except (netaddr.AddrFormatError, ValueError):
|
||||||
|
msg = "You must pass a valid IP address; {0} is invalid".format(arg)
|
||||||
|
raise AnsibleFilterError(msg)
|
||||||
numbers = list(map(int, arg.split(".")))
|
numbers = list(map(int, arg.split(".")))
|
||||||
return "{0:02x}{sep}{1:02x}{sep}{2:02x}{sep}{3:02x}".format(*numbers, sep=delimiter)
|
return "{0:02x}{sep}{1:02x}{sep}{2:02x}{sep}{3:02x}".format(*numbers, sep=delimiter)
|
||||||
|
|
||||||
|
|
|
@ -14,3 +14,13 @@
|
||||||
- name: Assert result for ip4_hex.
|
- name: Assert result for ip4_hex.
|
||||||
ansible.builtin.assert:
|
ansible.builtin.assert:
|
||||||
that: "{{ result1 == 'c0:a8:01:05' }}"
|
that: "{{ result1 == 'c0:a8:01:05' }}"
|
||||||
|
|
||||||
|
- name: Ip4_hex validate input
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
result1: "{{ '555.444.333.999' | ansible.utils.ip4_hex }}"
|
||||||
|
register: output
|
||||||
|
ignore_errors: true
|
||||||
|
|
||||||
|
- name: Assert result for ip4_hex.
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that: "{{ 'You must pass a valid IP address; 555.444.333.999 is invalid' in output.msg }}"
|
||||||
|
|
Loading…
Reference in New Issue