Skip to content

Commit

Permalink
Enforce selecting ref from network_obj based on network view (#157)
Browse files Browse the repository at this point in the history
The 'default' view is used by default.

Fixes: #156
  • Loading branch information
philipsd6 authored Nov 9, 2022
1 parent 953aca5 commit 5976756
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
12 changes: 11 additions & 1 deletion plugins/lookup/nios_next_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,22 @@
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 = """
- 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'}) }}"
- 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,
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 12 additions & 1 deletion plugins/lookup/nios_next_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand All @@ -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,
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 5976756

Please sign in to comment.