Debian Codegraph @

ansible_collections/infoblox/nios_modules/plugins/lookup/nios_next_ip.py

raw symbols SCIP
1 # Copyright (c) 2018-2019 Red Hat, Inc.
2 # Copyright (c) 2020 Infoblox, Inc.
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5
6 from __future__ import (absolute_import, division, print_function)
7
8 __metaclass__ = type
9
10 DOCUMENTATION = '''
11 ---
12 name: nios_next_ip
13 short_description: Return the next available IP address for a network
14 version_added: "1.0.0"
15 description:
16 - Uses the Infoblox WAPI API to return the next available IP addresses
17 for a given network CIDR
18 requirements:
19 - infoblox-client
20
21 options:
22 _terms:
23 description: The CIDR network to retrieve the next address(es) from.
24 required: True
25 type: str
26 use_range:
27 description: Use DHCP range to retrieve the next available IP address(es). Requested number of IP Addresses must be between 1 and 20.
28 required: false
29 default: false
30 type: bool
31 num:
32 description: The number of IP address(es) to return.
33 required: false
34 default: 1
35 type: int
36 exclude:
37 description: List of IP's that need to be excluded from returned IP addresses.
38 required: false
39 type: list
40 elements: str
41 network_view:
42 description: The network view to retrieve the CIDR network from.
43 required: false
44 default: default
45 type: str
46 '''
47
48 EXAMPLES = """
49 - name: return next available IP address for network 192.168.10.0/24
50 ansible.builtin.set_fact:
51 ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24',
52 provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
53
54 - name: return next available IP address for network 192.168.10.0/24 from DHCP range
55 ansible.builtin.set_fact:
56 ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24',
57 use_range=true, provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
58
59 - name: return next available IP address for network 192.168.10.0/24 in a non-default network view
60 ansible.builtin.set_fact:
61 ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24', network_view='ansible',
62 provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
63
64 - name: return the next 3 available IP addresses for network 192.168.10.0/24
65 ansible.builtin.set_fact:
66 ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24', num=3,
67 provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
68
69 - name: return the next 3 available IP addresses for network 192.168.10.0/24
70 excluding ip addresses - ['192.168.10.1', '192.168.10.2']
71 ansible.builtin.set_fact:
72 ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24', num=3,
73 exclude=['192.168.10.1', '192.168.10.2'],
74 provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
75
76 - name: return next available IP address for network fd30:f52:2:12::/64
77 ansible.builtin.set_fact:
78 ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', 'fd30:f52:2:12::/64',
79 provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
80 """
81
82 RETURN = """
83 _list:
84 description:
85 - The list of next IP addresses available
86 returned: always
87 type: list
88 """
89
90 from ansible.plugins.lookup import LookupBase
91 from ansible.errors import AnsibleError
92 from ..module_utils.api import WapiLookup
93 import ipaddress
94
95
96 class LookupModule(LookupBase):
97
98 def run(self, terms, variables=None, **kwargs):
99 try:
100 network = terms[0]
101 except IndexError:
102 raise AnsibleError('missing argument in the form of A.B.C.D/E')
103
104 provider = kwargs.pop('provider', {})
105 wapi = WapiLookup(provider)
106 network_view = kwargs.get('network_view', 'default')
107
108 if isinstance(ipaddress.ip_network(network), ipaddress.IPv6Network):
109 object_type = 'ipv6range' if kwargs.get('use_range', False) else 'ipv6network'
110 else:
111 object_type = 'range' if kwargs.get('use_range', False) else 'network'
112
113 network_obj = wapi.get_object(object_type, {'network': network, 'network_view': network_view})
114
115 if network_obj is None:
116 raise AnsibleError('unable to find network object %s' % network)
117
118 num = kwargs.get('num', 1)
119 exclude_ip = kwargs.get('exclude', [])
120
121 ref_list = [network['_ref'] for network in network_obj if network['network_view'] == network_view]
122 if not ref_list:
123 raise AnsibleError('no records found')
124
125 for ref in ref_list:
126 try:
127 avail_ips = wapi.call_func('next_available_ip', ref, {'num': num, 'exclude': exclude_ip})
128 if len(avail_ips['ips']) >= num:
129 return [avail_ips['ips']]
130 except Exception:
131 continue
132
133 raise AnsibleError('unable to find the required number of IPs')