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

Added support for IPv6 next available IPs in network ranges. #225

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Changes from all 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
36 changes: 19 additions & 17 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 Down Expand Up @@ -86,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 @@ -103,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 @@ -117,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')
Loading