From 8b4f2d050135610c0ecf87adf018843ef6f60996 Mon Sep 17 00:00:00 2001 From: Ondra Machacek Date: Wed, 29 May 2024 09:50:30 +0200 Subject: [PATCH] Add use_range parameter to nios_next_ip (#200) This parameter add use_range parameter to nios_next_ip lookup plugin. With this parameter set to true, the plugin will return first available IP from the network range, assigned to the network. Signed-off-by: Ondra Machacek --- plugins/lookup/nios_next_ip.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/plugins/lookup/nios_next_ip.py b/plugins/lookup/nios_next_ip.py index e2fa076d..9b5d243f 100644 --- a/plugins/lookup/nios_next_ip.py +++ b/plugins/lookup/nios_next_ip.py @@ -23,6 +23,11 @@ description: The CIDR network to retrieve the next address(es) from. required: True type: str + use_range: + description: Use DHCP range to retrieve the next available IP address(es). + required: false + default: no + type: bool num: description: The number of IP address(es) to return. required: false @@ -45,6 +50,10 @@ ansible.builtin.set_fact: 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'}) }}" + - name: return next available IP address for network 192.168.10.0/24 in a non-default network view ansible.builtin.set_fact: ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24', network_view='ansible', \ @@ -91,7 +100,9 @@ def run(self, terms, variables=None, **kwargs): provider = kwargs.pop('provider', {}) wapi = WapiLookup(provider) - if isinstance(ipaddress.ip_network(network), ipaddress.IPv6Network): + 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}) else: network_obj = wapi.get_object('network', {'network': network})