From 51eb186b7c848439c7c25c263590fa88220c83a3 Mon Sep 17 00:00:00 2001 From: Philip Douglass Date: Wed, 12 Oct 2022 10:20:08 -0400 Subject: [PATCH] Enforce selecting ref from network_obj based on network view The 'default' view is used by default. Fixes: #156 --- plugins/lookup/nios_next_ip.py | 12 +++++++++++- plugins/lookup/nios_next_network.py | 13 ++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/plugins/lookup/nios_next_ip.py b/plugins/lookup/nios_next_ip.py index f10203a5..c99c232d 100644 --- a/plugins/lookup/nios_next_ip.py +++ b/plugins/lookup/nios_next_ip.py @@ -33,6 +33,11 @@ required: false type: list elements: str + network_view: + description: The network view to retrieve the CIDR network from. + required: false + default: default + type: str ''' EXAMPLES = """ @@ -40,6 +45,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 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', provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}" + - name: return the next 3 available IP addresses for network 192.168.10.0/24 ansible.builtin.set_fact: ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24', num=3, @@ -91,9 +100,10 @@ def run(self, terms, variables=None, **kwargs): num = kwargs.get('num', 1) exclude_ip = kwargs.get('exclude', []) + network_view = kwargs.get('network_view', 'default') try: - ref = network_obj[0]['_ref'] + ref = [network['_ref'] for network in network_obj if network['network_view'] == network_view][0] avail_ips = wapi.call_func('next_available_ip', ref, {'num': num, 'exclude': exclude_ip}) return [avail_ips['ips']] except Exception as exc: diff --git a/plugins/lookup/nios_next_network.py b/plugins/lookup/nios_next_network.py index 293dddb3..b4b012a5 100644 --- a/plugins/lookup/nios_next_network.py +++ b/plugins/lookup/nios_next_network.py @@ -41,6 +41,11 @@ default: '' type: list elements: str + network_view: + description: The network view to retrieve the CIDR network from. + required: false + default: default + type: str ''' EXAMPLES = """ @@ -49,6 +54,11 @@ networkaddr: "{{ lookup('infoblox.nios_modules.nios_next_network', '192.168.10.0/24', cidr=25, provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}" +- name: return next available network for network-container 192.168.10.0/24 in a non-default network view + ansible.builtin.set_fact: + networkaddr: "{{ lookup('infoblox.nios_modules.nios_next_network', '192.168.10.0/24', cidr=25, network_view='ansible' + provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}" + - name: return the next 2 available network addresses for network-container 192.168.10.0/24 ansible.builtin.set_fact: networkaddr: "{{ lookup('infoblox.nios_modules.nios_next_network', '192.168.10.0/24', cidr=25, num=2, @@ -94,9 +104,10 @@ def run(self, terms, variables=None, **kwargs): raise AnsibleError('unable to find network-container object %s' % network) num = kwargs.get('num', 1) exclude_ip = kwargs.get('exclude', []) + network_view = kwargs.get('network_view', 'default') try: - ref = network_obj[0]['_ref'] + ref = [network['_ref'] for network in network_obj if network['network_view'] == network_view][0] avail_nets = wapi.call_func('next_available_network', ref, {'cidr': cidr, 'num': num, 'exclude': exclude_ip}) return [avail_nets['networks']] except Exception as exc: