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

Fixed ansible.utils.ipaddr('host/prefix') function if size of subnet is 1 #373

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

ijajmulani
Copy link

SUMMARY

Fixes #372

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

ansible.utils.ipaddr

ADDITIONAL INFORMATION

If non CIDR IP is given to ansible.utils.ipaddr('host/prefix') function is not returning False value.
e.g

 "192.168.0.21" | ansible.utils.ipaddr('host/prefix')

Ideally above code should return False value. Hence corrected the behavior.

---
- name: Validate IP address in CIDR notation
  hosts: localhost
  gather_facts: no
  vars:
    # Replace this with your actual IP address in CIDR notation
    host_ip_cidr: "2620:128:f021:9028::112"  # Can be IPV4

  tasks:
    - name: Check if the IP address in CIDR notation is valid
      assert:
        that:
          - host_ip_cidr | ansible.utils.ipaddr('host/prefix')
        msg: "Invalid IP address in CIDR notation. Enter a valid IP address in CIDR notation. For example, 192.168.0.2/24 or 2001::1/64."

    - name: Output the validation result
      debug:
        msg: "The IP address {{ host_ip_cidr }} is valid in CIDR notation."

if we run above playbook before my change, then playbook passes

# Before my change
ansible-playbook a.yml
------------------------------------------------------------------------------------------------------------------------------
Log Path:                                                             /var/log/ansible/PLAYBOOK/2024-09-10T16-15-20.093922.log
Configuring:                                                                                                             100%

After my change playbook fails as expected.

ansible-playbook a.yml
------------------------------------------------------------------------------------------------------------------------------
Log Path:                                                             /var/log/ansible/PLAYBOOK/2024-09-10T16-16-00.069168.log
Configuring:                                                                                                               0%

Task Name: Check if the IP address in CIDR notation is valid
Error:
 "Invalid IP address in CIDR notation. Enter a valid IP address in CIDR notation. For example, 192.168.0.2/24 or 2001::1/64."

…is 1.

If non CIDR IP is given to  ansible.utils.ipaddr('host/prefix') function is not returning False value.
e.g
```
 "192.168.0.21" | ansible.utils.ipaddr('host/prefix')
```
Ideally above code should return False value.  Hence corrected the behaviour.
Fixed ansible.utils.ipaddr('host/prefix') function if size of subnet …
Copy link
Contributor

… subnet is 1.

Fixed ansible.utils.ipaddr('host/prefix') function if size of ip's in subnet is 1.
Copy link
Contributor

Update ipaddr _address_prefix_query testcases to test single ip.
Copy link
Contributor

…is 1.

Fixed ansible.utils.ipaddr('host/prefix') function if size of subnet is 1.
Copy link
Contributor

Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ansible.utils.ipaddr('host/prefix') should not parse non CIDR IP.
2 participants