Debian Codegraph @

ansible_collections/infoblox/nios_modules/plugins/modules/nios_range.py

raw symbols SCIP
1 #!/usr/bin/python
2 # Copyright (c) 2018-2019 Red Hat, Inc.
3 # Copyright (c) 2020 Infoblox, Inc.
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8
9 DOCUMENTATION = '''
10 ---
11 module: nios_range
12 author: "Matthew Dennett (@matthewdennett)"
13 short_description: Configure Infoblox NIOS network range object
14 version_added: "1.5.0"
15 description:
16 - Adds and/or removes instances of range objects from
17 Infoblox NIOS servers. This module manages NIOS DHCP range objects
18 using the Infoblox WAPI interface over REST.
19 - Supports both IPV4 and IPV6 internet protocols.
20 requirements:
21 - infoblox-client
22 extends_documentation_fragment: infoblox.nios_modules.nios
23 notes:
24 - This module supports C(check_mode).
25 options:
26 network:
27 description:
28 - Specifies the network to add or remove DHCP range to. The value
29 should use CIDR notation.
30 type: str
31 required: true
32 aliases:
33 - cidr
34 network_view:
35 description:
36 - Configures the name of the network view to associate with this
37 configured instance.
38 type: str
39 default: default
40 options:
41 description:
42 - Configures the set of DHCP options to be included as part of
43 the configured network instance. This argument accepts a list
44 of values (see suboptions). When configuring suboptions at
45 least one of C(name) or C(num) must be specified.
46 type: list
47 default: []
48 elements: dict
49 suboptions:
50 name:
51 description:
52 - The name of the DHCP option to configure. The standard options are
53 C(router), C(router-templates), C(domain-name-servers), C(domain-name),
54 C(broadcast-address), C(broadcast-address-offset), C(dhcp-lease-time),
55 and C(dhcp6.name-servers).
56 type: str
57 num:
58 description:
59 - The number of the DHCP option to configure
60 type: int
61 value:
62 description:
63 - The value of the DHCP option specified by C(name)
64 type: str
65 required: true
66 use_option:
67 description:
68 - Only applies to a subset of options (see NIOS API documentation)
69 type: bool
70 default: 'yes'
71 vendor_class:
72 description:
73 - The name of the space this DHCP option is associated to
74 type: str
75 default: DHCP
76 extattrs:
77 description:
78 - Allows for the configuration of Extensible Attributes on the
79 instance of the object. This argument accepts a set of key / value
80 pairs for configuration.
81 type: dict
82 comment:
83 description:
84 - Configures a text string comment to be associated with the instance
85 of this object. The provided text string will be configured on the
86 object instance.
87 type: str
88 start_addr:
89 description:
90 - Configures IP address this object instance is to begin from. If
91 'new_start_addr' is defined during a create operation this value is
92 overridden with the value of 'new_start_addr'
93 type: str
94 required: true
95 aliases:
96 - start
97 - first_addr
98 - first
99 new_start_addr:
100 description:
101 - Configures IP address to update this object instance to begin from.
102 type: str
103 required: false
104 aliases:
105 - new_start
106 - new_first_addr
107 - new_first
108 end_addr:
109 description:
110 - Configures IP address this object instance is to end at. If
111 'new_end_addr' is defined during a create operation this value is
112 overridden with the value of 'new_end_addr'
113 type: str
114 required: true
115 aliases:
116 - end
117 - last_addr
118 - last
119 new_end_addr:
120 description:
121 - Configures IP address to update this object instance to end at.
122 type: str
123 required: false
124 aliases:
125 - new_end
126 - new_last_addr
127 - new_last
128 member:
129 description:
130 - The hostname of the Nios member which will be configured to server
131 this object instance. Can not be configured when 'ms_server' or
132 'failover_association' are configured.
133 type: str
134 required: false
135 failover_association:
136 description:
137 - The name of the DHCP failover association which will be configured
138 to server this object instance. A failover of MS or Nios members
139 can be configured. Can not be configured when 'ms_server' or
140 'member' are configured.
141 type: str
142 required: false
143 ms_server:
144 description:
145 - The hostname of the MS member which will be configured to server
146 this object instance. Can not be configured when 'member' or
147 'failover_association' are configured.
148 type: str
149 required: false
150 server_association_type:
151 description:
152 - Configured the type of server association that will be assigned to
153 serve this object instance. This value is not required and will be
154 set as needed automatically during module execution.
155 type: str
156 required: false
157 choices:
158 - NONE
159 - FAILOVER
160 - MEMBER
161 - FAILOVER_MS
162 - MS_SERVER
163 state:
164 description:
165 - Configures the intended state of the instance of the object on
166 the NIOS server. When this value is set to C(present), the object
167 is configured on the device and when this value is set to C(absent)
168 the value is removed (if necessary) from the device.
169 type: str
170 default: present
171 choices:
172 - present
173 - absent
174 disable:
175 description:
176 - Determines whether a range is disabled or not. When this is set
177 to False, the range is enabled.
178 type: bool
179 default: false
180 name:
181 description:
182 - Congifured the name of the Microsoft scope for the instance of the
183 object on the NIOS server.
184 type: str
185 '''
186
187 EXAMPLES = '''
188
189 - name: Configure a ipv4 reserved range
190 infoblox.nios_modules.nios_range:
191 network: 192.168.10.0/24
192 start: 192.168.10.10
193 end: 192.168.10.20
194 name: Test Range 1
195 comment: this is a test comment
196 state: present
197 provider:
198 host: "{{ inventory_hostname_short }}"
199 username: admin
200 password: admin
201 connection: local
202
203 - name: Upadtes a ipv4 reserved range
204 infoblox.nios_modules.nios_range:
205 network: 192.168.10.0/24
206 start: 192.168.10.10
207 new_start: 192.168.10.5
208 end: 192.168.10.20
209 new_end: 192.168.10.50
210 name: Test Range 1
211 comment: this is a test comment
212 state: present
213 provider:
214 host: "{{ inventory_hostname_short }}"
215 username: admin
216 password: admin
217 connection: local
218
219 - name: Configure a ipv4 range served by a member
220 infoblox.nios_modules.nios_range:
221 network: 192.168.10.0/24
222 start: 192.168.10.10
223 end: 192.168.10.20
224 name: Test Range 1
225 member: infoblox1.localdomain
226 comment: this is a test comment
227 state: present
228 provider:
229 host: "{{ inventory_hostname_short }}"
230 username: admin
231 password: admin
232 connection: local
233
234 - name: Configure a ipv4 range served by a failover association
235 infoblox.nios_modules.nios_range:
236 network: 192.168.10.0/24
237 start: 192.168.10.10
238 end: 192.168.10.20
239 name: Test Range 1
240 failover_association: fo_association_01
241 comment: this is a test comment
242 state: present
243 provider:
244 host: "{{ inventory_hostname_short }}"
245 username: admin
246 password: admin
247 connection: local
248
249 - name: Configure a ipv4 range with options
250 infoblox.nios_modules.nios_range:
251 network: 18.10.0.0/24
252 network_view: custom
253 start_addr: 18.10.0.12
254 end_addr: 18.10.0.14
255 options:
256 - name: domain-name
257 value: zone1.com
258 comment: Created with Ansible
259 state: present
260 provider:
261 host: "{{ inventory_hostname_short }}"
262 username: admin
263 password: admin
264 connection: local
265
266 - name: Configure a ipv4 range served by a MS Server
267 infoblox.nios_modules.nios_range:
268 network: 192.168.10.0/24
269 start: 192.168.10.10
270 end: 192.168.10.20
271 name: Test Range 1
272 ms_server: dc01.ad.localdomain
273 comment: this is a test comment
274 state: present
275 provider:
276 host: "{{ inventory_hostname_short }}"
277 username: admin
278 password: admin
279 connection: local
280 '''
281
282 RETURN = ''' # '''
283
284 from ansible.module_utils.basic import AnsibleModule
285 from ..module_utils.api import WapiModule
286 from ..module_utils.api import NIOS_RANGE
287 from ..module_utils.api import normalize_ib_spec
288
289
290 def options(module):
291 ''' Transforms the module argument into a valid WAPI struct
292 This function will transform the options argument into a structure that
293 is a valid WAPI structure in the format of:
294 {
295 name: <value>,
296 num: <value>,
297 value: <value>,
298 use_option: <value>,
299 vendor_class: <value>
300 }
301 It will remove any options that are set to None since WAPI will error on
302 that condition. It will also verify that either `name` or `num` is
303 set in the structure but does not validate the values are equal.
304 The remainder of the value validation is performed by WAPI
305 '''
306 options = list()
307 for item in module.params['options']:
308 opt = dict([(k, v) for k, v in item.items() if v is not None])
309 if 'name' not in opt and 'num' not in opt:
310 module.fail_json(msg='one of `name` or `num` is required for option value')
311 options.append(opt)
312 return options
313
314
315 def check_vendor_specific_dhcp_option(module, ib_spec):
316 '''This function will check if the argument dhcp option belongs to vendor-specific and if yes then will remove
317 use_options flag which is not supported with vendor-specific dhcp options.
318 '''
319 for key, value in ib_spec.items():
320 if isinstance(module.params[key], list):
321 for temp_dict in module.params[key]:
322 if 'num' in temp_dict:
323 if temp_dict['num'] in (43, 124, 125, 67, 60):
324 del temp_dict['use_option']
325 return ib_spec
326
327
328 def convert_range_member_to_struct(module):
329 '''This function will check the module input to ensure that only one member assignment type is specified at once.
330 Member passed in is converted to the correct struct for the API to understand bassed on the member type.
331 '''
332 # Error checking that only one member type was defined
333 params = [k for k in module.params.keys() if module.params[k] is not None]
334 opts = list(set(params).intersection(['member', 'failover_association', 'ms_server']))
335 if len(opts) > 1:
336 raise AttributeError("'%s' can not be defined when '%s' is defined!" % (opts[0], opts[1]))
337
338 # A member node was passed in. Ehsure the correct type and struct
339 if 'member' in opts:
340 module.params['member'] = {'_struct': 'dhcpmember', 'name': module.params['member']}
341 module.params['server_association_type'] = 'MEMBER'
342 # A FO association was passed in. Ensure the correct type is set
343 elif 'failover_association' in opts:
344 module.params['server_association_type'] = 'FAILOVER'
345 # MS server was passed in. Ensure the correct type and struct
346 elif 'ms_server' in opts:
347 module.params['ms_server'] = {'_struct': 'msdhcpserver', 'ipv4addr': module.params['ms_server']}
348 module.params['server_association_type'] = 'MS_SERVER'
349 else:
350 module.params['server_association_type'] = 'NONE'
351 return module
352
353
354 def main():
355 ''' Main entry point for module execution
356 '''
357 option_spec = dict(
358 # one of name or num is required; enforced by the function options()
359 name=dict(),
360 num=dict(type='int'),
361
362 value=dict(required=True),
363
364 use_option=dict(type='bool', default=True),
365 vendor_class=dict(default='DHCP')
366 )
367
368 # This is what gets posted to the WAPI API
369 ib_spec = dict(
370 network=dict(required=True, aliases=['cidr']),
371 network_view=dict(default='default', ib_req=True),
372 start_addr=dict(required=True, aliases=['start', 'first_addr', 'first'], type='str', ib_req=True),
373 new_start_addr=dict(aliases=['new_start', 'new_first_addr', 'new_first'], type='str'),
374 end_addr=dict(required=True, aliases=['end', 'last_addr', 'last'], type='str', ib_req=True),
375 new_end_addr=dict(aliases=['new_end', 'new_last_addr', 'new_last'], type='str'),
376 name=dict(type='str'),
377 disable=dict(type='bool', default='false',),
378 options=dict(type='list', elements='dict', options=option_spec, transform=options, default=[]),
379 member=dict(type='str'),
380 failover_association=dict(type='str'),
381 ms_server=dict(type='str'),
382 server_association_type=dict(type='str', choices=['NONE', 'FAILOVER', 'MEMBER', 'FAILOVER_MS', 'MS_SERVER']),
383 extattrs=dict(type='dict'),
384 comment=dict()
385 )
386
387 argument_spec = dict(
388 provider=dict(required=True),
389 state=dict(default='present', choices=['present', 'absent'])
390 )
391
392 argument_spec.update(normalize_ib_spec(ib_spec))
393 argument_spec.update(WapiModule.provider_spec)
394
395 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
396 module = convert_range_member_to_struct(module)
397 wapi = WapiModule(module)
398 # to check for vendor specific dhcp option
399 ib_spec = check_vendor_specific_dhcp_option(module, ib_spec)
400
401 result = wapi.run(NIOS_RANGE, ib_spec)
402
403 module.exit_json(**result)
404
405
406 if __name__ == '__main__':
407 main()