Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix ip4 hex #292

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/fragments/Bugfix_ipv4_hex.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- Validate input for ipv4_hex(https://github.com/ansible-collections/ansible.utils/issues/281)
5 changes: 5 additions & 0 deletions plugins/filter/ip4_hex.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ def _ip4_hex(*args, **kwargs):

def ip4_hex(arg, delimiter=""):
"""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(".")))
return "{0:02x}{sep}{1:02x}{sep}{2:02x}{sep}{3:02x}".format(*numbers, sep=delimiter)

Expand Down
10 changes: 10 additions & 0 deletions tests/integration/targets/utils_ipaddr_filter/tasks/ip4_hex.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@
- name: Assert result for ip4_hex.
ansible.builtin.assert:
that: "{{ result1 == 'c0:a8:01:05' }}"

- name: Ip4_hex validate input
ansible.builtin.set_fact:
result1: "{{ '555.444.333.999'|ansible.utils.ip4_hex }}"
ashwini-mhatre marked this conversation as resolved.
Show resolved Hide resolved
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