Skip to content

Commit

Permalink
- Added support for IPv6 next available ips in network ranges.
Browse files Browse the repository at this point in the history
  • Loading branch information
JchhatbarInfoblox committed May 29, 2024
1 parent 8b4f2d0 commit ebb85ec
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions plugins/lookup/nios_next_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
required: True
type: str
use_range:
description: Use DHCP range to retrieve the next available IP address(es).
description: Use DHCP range to retrieve the next available IP address(es). Requested number of IP Addresses must be between 1 and 20.
required: false
default: no
type: bool
Expand All @@ -48,11 +48,13 @@
EXAMPLES = """
- name: return next available IP address for network 192.168.10.0/24
ansible.builtin.set_fact:
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24', provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24',
provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
- name: return next available IP address for network 192.168.10.0/24 from DHCP range
ansible.builtin.set_fact:
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24', use_range=true, provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24', use_range=true,
provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
- name: return next available IP address for network 192.168.10.0/24 in a non-default network view
ansible.builtin.set_fact:
Expand All @@ -71,7 +73,8 @@
- name: return next available IP address for network fd30:f52:2:12::/64
ansible.builtin.set_fact:
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', 'fd30:f52:2:12::/64', provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', 'fd30:f52:2:12::/64',
provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
"""

RETURN = """
Expand All @@ -83,7 +86,6 @@
"""

from ansible.plugins.lookup import LookupBase
from ansible.module_utils._text import to_text
from ansible.errors import AnsibleError
from ..module_utils.api import WapiLookup
import ipaddress
Expand All @@ -100,12 +102,12 @@ def run(self, terms, variables=None, **kwargs):
provider = kwargs.pop('provider', {})
wapi = WapiLookup(provider)

if kwargs.get('use_range', False):
network_obj = wapi.get_object('range', {'network': network})
elif isinstance(ipaddress.ip_network(network), ipaddress.IPv6Network):
network_obj = wapi.get_object('ipv6network', {'network': network})
if isinstance(ipaddress.ip_network(network), ipaddress.IPv6Network):
object_type = 'ipv6range' if kwargs.get('use_range', False) else 'ipv6network'
else:
network_obj = wapi.get_object('network', {'network': network})
object_type = 'range' if kwargs.get('use_range', False) else 'network'

network_obj = wapi.get_object(object_type, {'network': network})

if network_obj is None:
raise AnsibleError('unable to find network object %s' % network)
Expand All @@ -114,13 +116,16 @@ def run(self, terms, variables=None, **kwargs):
exclude_ip = kwargs.get('exclude', [])
network_view = kwargs.get('network_view', 'default')

try:
ref_list = [network['_ref'] for network in network_obj if network['network_view'] == network_view]
if not ref_list:
raise AnsibleError('no records found')
else:
ref = ref_list[0]
avail_ips = wapi.call_func('next_available_ip', ref, {'num': num, 'exclude': exclude_ip})
return [avail_ips['ips']]
except Exception as exc:
raise AnsibleError(to_text(exc))
ref_list = [network['_ref'] for network in network_obj if network['network_view'] == network_view]
if not ref_list:
raise AnsibleError('no records found')

for ref in ref_list:
try:
avail_ips = wapi.call_func('next_available_ip', ref, {'num': num, 'exclude': exclude_ip})
if len(avail_ips['ips']) >= num:
return [avail_ips['ips']]
except Exception:
continue

raise AnsibleError('unable to find the required number of IPs')

0 comments on commit ebb85ec

Please sign in to comment.