Debian Codegraph @

ansible_collections/infoblox/nios_modules/plugins/lookup/nios_next_network.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_network
13 short_description: Return the next available network range for a network-container
14 version_added: "1.0.0"
15 description:
16 - Uses the Infoblox WAPI API to return the next available network addresses for
17 a given network CIDR
18 requirements:
19 - infoblox_client
20
21 options:
22 _terms:
23 description: The CIDR network to retrieve the next network from next available network within the specified
24 container.
25 required: True
26 type: str
27 cidr:
28 description:
29 - The CIDR of the network to retrieve the next network from next available network within the
30 specified container. Also, Requested CIDR must be specified and greater than the parent CIDR.
31 required: True
32 type: str
33 num:
34 description: The number of network addresses to return from network-container.
35 required: false
36 default: 1
37 type: int
38 exclude:
39 description: Network addresses returned from network-container excluding list of user's input network range.
40 required: false
41 default: ''
42 type: list
43 elements: str
44 network_view:
45 description: The network view to retrieve the CIDR network from.
46 required: false
47 default: default
48 type: str
49 '''
50
51 EXAMPLES = """
52 - name: return next available network for network-container 192.168.10.0/24
53 ansible.builtin.set_fact:
54 networkaddr: "{{ lookup('infoblox.nios_modules.nios_next_network', '192.168.10.0/24', cidr=25,
55 provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
56
57 - name: return next available network for network-container 192.168.10.0/24 in a non-default network view
58 ansible.builtin.set_fact:
59 networkaddr: "{{ lookup('infoblox.nios_modules.nios_next_network', '192.168.10.0/24', cidr=25, network_view='ansible'
60 provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
61
62 - name: return the next 2 available network addresses for network-container 192.168.10.0/24
63 ansible.builtin.set_fact:
64 networkaddr: "{{ lookup('infoblox.nios_modules.nios_next_network', '192.168.10.0/24', cidr=25, num=2,
65 provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
66
67 - name: return the available network addresses for network-container 192.168.10.0/24 excluding network range '192.168.10.0/25'
68 ansible.builtin.set_fact:
69 networkaddr: "{{ lookup('infoblox.nios_modules.nios_next_network', '192.168.10.0/24', cidr=25, exclude=['192.168.10.0/25'],
70 provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
71
72 - name: return the available ipv6 network addresses for network-container 2001:1:111:1::0/64
73 set_fact:
74 networkaddr: "{{ lookup('infoblox.nios_modules.nios_next_network', '2001:1:111:1::0/64', cidr=126,
75 provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
76 """
77
78 RETURN = """
79 _list:
80 description:
81 - The list of next network addresses available
82 returned: always
83 type: list
84 """
85
86 from ansible.plugins.lookup import LookupBase
87 from ansible.module_utils._text import to_text
88 from ansible.errors import AnsibleError
89 from ..module_utils.api import WapiLookup
90 from ..module_utils.api import NIOS_IPV4_NETWORK_CONTAINER, NIOS_IPV6_NETWORK_CONTAINER
91 import ipaddress
92
93
94 class LookupModule(LookupBase):
95
96 def run(self, terms, variables=None, **kwargs):
97 try:
98 network = ipaddress.ip_network(terms[0], strict=True)
99 except IndexError:
100 raise AnsibleError('network argument is missing')
101 except (ValueError, TypeError) as error:
102 raise AnsibleError('network argument is invalid %s' % error)
103 try:
104 cidr = kwargs.get('cidr', 24)
105 # maybe using network.prefixlen+1 as default
106 except IndexError:
107 raise AnsibleError('missing CIDR argument in the form of xx')
108
109 if network.prefixlen >= cidr:
110 raise AnsibleError('cidr %s must be greater than parent network cidr %s' % (cidr, network.prefixlen))
111
112 container_type = None
113 network_objects = None
114
115 # check for ip version 4 or 6 else die
116 if network.version == 4:
117 container_type = NIOS_IPV4_NETWORK_CONTAINER
118 if cidr not in range(1, 32):
119 raise AnsibleError('cidr %s must be in range 1 to 32' % cidr)
120 elif network.version == 6:
121 container_type = NIOS_IPV6_NETWORK_CONTAINER
122 if cidr not in range(1, 128):
123 raise AnsibleError('cidr %s must be in range 1 to 128' % cidr)
124 else:
125 raise AnsibleError('not a valid ipv4 or ipv6 network definition %s' % terms[0])
126
127 # check for valid subnetting cidr
128 if network.prefixlen >= cidr:
129 raise AnsibleError('cidr %s must be greater than parent network cidr %s' % (cidr, network.prefixlen))
130
131 provider = kwargs.pop('provider', {})
132 wapi = WapiLookup(provider)
133
134 if container_type is None:
135 raise AnsibleError('unable to identify network-container type')
136
137 network_objects = wapi.get_object(container_type, {'network': network.with_prefixlen})
138
139 if network_objects is None:
140 raise AnsibleError('unable to find network-container object %s' % network.with_prefixlen)
141
142 num = kwargs.get('num', 1)
143 exclude_ip = kwargs.get('exclude', [])
144 network_view = kwargs.get('network_view', 'default')
145
146 try:
147 ref_list = [network_obj['_ref'] for network_obj in network_objects if network_obj['network_view'] == network_view]
148 if not ref_list:
149 raise AnsibleError('no records found')
150 else:
151 ref = ref_list[0]
152 avail_nets = wapi.call_func('next_available_network', ref, {'cidr': cidr, 'num': num, 'exclude': exclude_ip})
153 return [avail_nets['networks']]
154 except Exception as exc:
155 raise AnsibleError(to_text(exc))