Debian Codegraph @

ansible_collections/cisco/intersight/plugins/modules/intersight_ip_pool.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
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 ANSIBLE_METADATA = {'metadata_version': '1.1',
10 'status': ['preview'],
11 'supported_by': 'community'}
12
13 DOCUMENTATION = r'''
14 ---
15 module: intersight_ip_pool
16 short_description: IP Pool configuration for Cisco Intersight
17 description:
18 - IP Pool configuration for Cisco Intersight.
19 - Used to configure IP pools settings on Cisco Intersight managed devices.
20 - For more information see L(Cisco Intersight,https://intersight.com/apidocs).
21 extends_documentation_fragment: intersight
22 options:
23 state:
24 description:
25 - If C(present), will verify the resource is present and will create if needed.
26 - If C(absent), will verify the resource is absent and will delete if needed.
27 type: str
28 choices: [present, absent]
29 default: present
30 organization:
31 description:
32 - The name of the Organization this resource is assigned to.
33 - Profiles, Policies, and Pools that are created within a Custom Organization are applicable only to devices in the same Organization.
34 type: str
35 default: default
36 name:
37 description:
38 - The name assigned to the IP Pool.
39 - The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_.
40 type: str
41 required: true
42 tags:
43 description:
44 - List of tags in Key:<user-defined key> Value:<user-defined value> format.
45 type: list
46 elements: dict
47 description:
48 description:
49 - The user-defined description of the IP Pool.
50 - Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_).
51 type: str
52 aliases: [descr]
53 assignment_order:
54 description:
55 - Assignment order decides the order in which the next identifier is allocated.
56 - If C(sequential), identifiers are assigned in a sequential order.
57 - If C(default), the system determines the assignment order.
58 type: str
59 choices: [default, sequential]
60 default: default
61 enable_block_level_subnet_config:
62 description:
63 - Determines if the "Netmask", "Gateway", "PrimaryDns" and "SecondaryDns" is globally defined or specified per IPv4 block.
64 - Determines if the "Prefix", "Gateway", "PrimaryDns" and "SecondaryDns" is globally defined or specified per IPv6 block.
65 type: bool
66 default: false
67 ipv4_config:
68 description:
69 - Global definition of IPv4 "Netmask", "Gateway", "PrimaryDns" and "SecondaryDns".
70 - Used only when "enable_block_level_subnet_config" is false, otherwise should be defined inside ipv4_blocks.
71 type: dict
72 suboptions:
73 netmask:
74 description:
75 - A subnet mask is a 32-bit number that masks an IP address and divides the IP address into network address and host address.
76 - Netmask used for all IPv4 blocks.
77 type: str
78 gateway:
79 description:
80 - IP address of the default IPv4 gateway.
81 - Gateway IP used for all IPv4 blocks.
82 type: str
83 primary_dns:
84 description:
85 - IP Address of the primary Domain Name System (DNS) server.
86 - PrimaryDns IP used for all IPv4 blocks.
87 type: str
88 secondary_dns:
89 description:
90 - IP Address of the secondary Domain Name System (DNS) server.
91 - SecondaryDns IP used for all IPv4 blocks.
92 type: str
93 ipv4_blocks:
94 description:
95 - List of the IPv4 blocks.
96 - Should include the ipv4_config details per block in case "enable_block_level_subnet_config" is true
97 type: list
98 elements: dict
99 suboptions:
100 from:
101 description:
102 - First IPv4 address of the block.
103 type: str
104 size:
105 description:
106 - Number of identifiers this block can hold.
107 type: int
108 ipv4_config:
109 description:
110 - Block definition of IPv4 "Netmask", "Gateway", "PrimaryDns" and "SecondaryDns".
111 - Used only when "enable_block_level_subnet_config" is true, otherwise should be defined globally.
112 type: dict
113 suboptions:
114 netmask:
115 description:
116 - The Netmask used for the current IPv4 block.
117 type: str
118 gateway:
119 description:
120 - The Gateway IP used for the current IPv4 block.
121 type: str
122 primary_dns:
123 description:
124 - The PrimaryDns IP used for the current IPv4 block.
125 type: str
126 secondary_dns:
127 description:
128 - The SecondaryDns IP used for the current IPv4 block.
129 type: str
130 ipv6_config:
131 description:
132 - Global definition of IPv6 "Prefix", "Gateway", "PrimaryDns" and "SecondaryDns".
133 - Used only when "enable_block_level_subnet_config" is false, otherwise should be defined inside ipv6_blocks.
134 type: dict
135 suboptions:
136 prefix:
137 description:
138 - A prefix length which masks the IP address and divides the IP address into network address and host address.
139 - Prefix used for all IPv6 blocks.
140 type: str
141 gateway:
142 description:
143 - IP address of the default IPv6 gateway.
144 - Gateway IP used for all IPv6 blocks.
145 type: str
146 primary_dns:
147 description:
148 - IP Address of the primary Domain Name System (DNS) server.
149 - PrimaryDns IP used for all IPv6 blocks.
150 type: str
151 secondary_dns:
152 description:
153 - IP Address of the secondary Domain Name System (DNS) server.
154 - SecondaryDns IP used for all IPv6 blocks.
155 type: str
156 ipv6_blocks:
157 description:
158 - Define the IPv6 blocks.
159 - Should include the ipv6_config details per block in case "enable_block_level_subnet_config" is true
160 type: list
161 elements: dict
162 suboptions:
163 from:
164 description:
165 - The initial IP address for the IPv6 block.
166 type: str
167 size:
168 description:
169 - The number of IPs in the block.
170 type: int
171 ipv6_config:
172 description:
173 - Block definition of IPv6 "Prefix", "Gateway", "PrimaryDns" and "SecondaryDns".
174 - Used only when "enable_block_level_subnet_config" is true, otherwise should be defined globally.
175 type: dict
176 suboptions:
177 prefix:
178 description:
179 - The Prefix used for the current IPv6 block.
180 type: int
181 gateway:
182 description:
183 - The Gateway IP used for the current IPv6 block.
184 type: str
185 primary_dns:
186 description:
187 - The PrimaryDns IP used for the current IPv6 block.
188 type: str
189 secondary_dns:
190 description:
191 - The SecondaryDns IP used for the current IPv6 block.
192 type: str
193 author:
194 - Shahar Golshani (@sgolshan)
195 '''
196
197 EXAMPLES = r'''
198 - name: Configure IP Pool with global config
199 cisco.intersight.intersight_ip_pool:
200 api_private_key: "{{ api_private_key }}"
201 api_key_id: "{{ api_key_id }}"
202 organization: DevNet
203 name: lab-ip-pool
204 description: IP Pool for lab use
205 assignment_order: sequential
206 ipv4_config:
207 netmask: "255.255.255.0"
208 gateway: "172.17.116.1"
209 primary_dns: "172.17.116.2"
210 secondary_dns: "172.17.116.3"
211 ipv4_blocks:
212 - from: "172.17.116.32"
213 size: 16
214 - from: "172.17.116.64"
215 size: 16
216 ipv6_config:
217 prefix: 64
218 gateway: "2001:db8::1"
219 primary_dns: "2001:4860:4860::8888"
220 secondary_dns: "2001:4860:4860::8844"
221 ipv6_blocks:
222 - from: "2001:db8::2"
223 size: 64
224 - from: "2001:db8::42"
225 size: 64
226 tags:
227 - Key: Site
228 Value: RCDN
229
230
231 - name: Configure IP Pool with block level config
232 cisco.intersight.intersight_ip_pool:
233 api_private_key: "{{ api_private_key }}"
234 api_key_id: "{{ api_key_id }}"
235 organization: DevNet
236 name: lab-ip-pool
237 description: IP Pool for lab use
238 enable_block_level_subnet_config: true
239 ipv4_blocks:
240 - from: "172.17.116.32"
241 size: 16
242 ipv4_config:
243 netmask: "255.255.255.0"
244 gateway: "172.17.116.1"
245 primary_dns: "172.17.116.2"
246 secondary_dns: "172.17.116.3"
247 - from: "172.17.117.32"
248 size: 16
249 ipv4_config:
250 netmask: "255.255.255.0"
251 gateway: "172.17.117.1"
252 primary_dns: "172.17.117.2"
253 secondary_dns: "172.17.117.3"
254 ipv6_blocks:
255 - from: "2001:db8::2"
256 size: 64
257 ipv6_config:
258 prefix: 64
259 gateway: "2001:db8::1"
260 primary_dns: "2001:4860:4860::8888"
261 secondary_dns: "2001:4860:4860::8844"
262 - from: "fd12:3456:789a::1"
263 size: 64
264 ipv6_config:
265 prefix: 64
266 gateway: "fd12:3456:789a::1"
267 primary_dns: "2606:4700:4700::1111"
268 secondary_dns: "2606:4700:4700::1001"
269 tags:
270 - Key: Site
271 Value: RCDN
272
273
274 - name: Delete IP Pool
275 cisco.intersight.intersight_ip_pool:
276 api_private_key: "{{ api_private_key }}"
277 api_key_id: "{{ api_key_id }}"
278 organization: DevNet
279 name: lab-ip-pool
280 state: absent
281 '''
282
283 RETURN = r'''
284 api_response:
285 description: The API response output returned by the specified resource.
286 returned: always
287 type: dict
288 sample:
289 "api_response": {
290 "AccountMoid": "",
291 "Ancestors": [],
292 "Assigned": 0,
293 "AssignmentOrder": "sequential",
294 "ClassId": "ippool.Pool",
295 "CreateTime": "",
296 "Description": "IP Pool for lab use",
297 "DomainGroupMoid": "",
298 "EnableBlockLevelSubnetConfig": true,
299 "IpV4Blocks": [],
300 "IpV4Config": {},
301 "IpV6Blocks": [],
302 "IpV6Config": {},
303 "ModTime": "",
304 "Moid": "",
305 "Name": "lab-ip-pool",
306 "ObjectType": "ippool.Pool",
307 "Organization": {},
308 "Owners": [],
309 "PermissionResources": [],
310 "Reservations": [],
311 "Reserved": 0,
312 "ShadowPools": [],
313 "SharedScope": "",
314 "Size": 160,
315 "Tags": [
316 {
317 "Key": "Site",
318 "Value": "RCDN"
319 }
320 ],
321 "V4Assigned": 0,
322 "V4Size": 32,
323 "V6Assigned": 0,
324 "V6Size": 128
325 }
326 '''
327
328
329 from ansible.module_utils.basic import AnsibleModule
330 from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec
331
332
333 def main():
334 argument_spec = intersight_argument_spec.copy()
335 argument_spec.update(
336 state=dict(type='str', choices=['present', 'absent'], default='present'),
337 organization=dict(type='str', default='default'),
338 name=dict(type='str', required=True),
339 description=dict(type='str', aliases=['descr']),
340 tags=dict(type='list', elements='dict'),
341 assignment_order=dict(type='str', choices=['default', 'sequential'], default='default'),
342 enable_block_level_subnet_config=dict(type='bool', default=False),
343 ipv4_config=dict(type='dict'),
344 ipv4_blocks=dict(type='list', elements='dict'),
345 ipv6_config=dict(type='dict'),
346 ipv6_blocks=dict(type='list', elements='dict'),
347 )
348
349 module = AnsibleModule(
350 argument_spec,
351 supports_check_mode=True,
352 )
353
354 intersight = IntersightModule(module)
355 intersight.result['api_response'] = {}
356 intersight.result['trace_id'] = ''
357
358 # Resource path used to configure policy
359 resource_path = '/ippool/Pools'
360 # Define API body used in compares or create
361 intersight.api_body = {
362 'Organization': {
363 'Name': intersight.module.params['organization'],
364 },
365 'Name': intersight.module.params['name'],
366 'EnableBlockLevelSubnetConfig': intersight.module.params['enable_block_level_subnet_config'],
367 }
368
369 if module.params['state'] == 'present':
370 intersight.set_tags_and_description()
371 intersight.api_body['AssignmentOrder'] = intersight.module.params['assignment_order']
372
373 # Validate that at least one of ipv4_blocks/ipv6_blocks was passed. We don't mark it as required in order to support absent.
374 if not intersight.module.params['ipv4_blocks'] and not intersight.module.params['ipv6_blocks']:
375 module.fail_json(msg="at least one of ipv4_blocks/ipv6_blocks parameters must be provided and contain at least one block when state is present")
376 # Validate that when enable_block_level_subnet_config is true, ipv4_blocks/ipv6_blocks contains ipv4_config/ipv6_config.
377 if intersight.module.params['enable_block_level_subnet_config']:
378 if intersight.module.params['ipv4_blocks']:
379 for block in intersight.module.params['ipv4_blocks']:
380 if "ipv4_config" not in block:
381 module.fail_json(msg="a block in ipv4_blocks is missing ipv4_config")
382 if intersight.module.params['ipv6_blocks']:
383 for block in intersight.module.params['ipv6_blocks']:
384 if "ipv6_config" not in block:
385 module.fail_json(msg="a block in ipv6_blocks is missing ipv6_config")
386 # Validate that when enable_block_level_subnet_config is false, ipv4_blocks/ipv6_blocks has a global ipv4_config/ipv6_config.
387 else:
388 if (intersight.module.params['ipv4_blocks'] is None) != (intersight.module.params['ipv4_config'] is None):
389 module.fail_json(msg="when enable_block_level_subnet_config is false, ipv4_blocks should be configured with global ipv4_config")
390 if (intersight.module.params['ipv6_blocks'] is None) != (intersight.module.params['ipv6_config'] is None):
391 module.fail_json(msg="when enable_block_level_subnet_config is false, ipv6_blocks should be configured with global ipv6_config")
392
393 IpV4Blocks = []
394 if intersight.module.params['ipv4_blocks']:
395 for v4_block in intersight.module.params['ipv4_blocks']:
396 block = {}
397 block['From'] = v4_block['from']
398 block['Size'] = v4_block['size']
399 if 'ipv4_config' in v4_block:
400 block['IpV4Config'] = {
401 'Netmask': v4_block['ipv4_config']['netmask'],
402 'Gateway': v4_block['ipv4_config']['gateway'],
403 'PrimaryDns': v4_block['ipv4_config']['primary_dns'],
404 'SecondaryDns': v4_block['ipv4_config']['secondary_dns']
405 }
406 IpV4Blocks.append(block)
407 intersight.api_body['IpV4Blocks'] = IpV4Blocks
408
409 IpV6Blocks = []
410 if intersight.module.params['ipv6_blocks']:
411 for v6_block in intersight.module.params['ipv6_blocks'] :
412 block = {}
413 block['From'] = v6_block['from']
414 block['Size'] = v6_block['size']
415 if 'ipv6_config' in v6_block:
416 block['IpV6Config'] = {
417 'Prefix': v6_block['ipv6_config']['prefix'],
418 'Gateway': v6_block['ipv6_config']['gateway'],
419 'PrimaryDns': v6_block['ipv6_config']['primary_dns'],
420 'SecondaryDns': v6_block['ipv6_config']['secondary_dns']
421 }
422 IpV6Blocks.append(block)
423 intersight.api_body['IpV6Blocks'] = IpV6Blocks
424
425 if not intersight.module.params['enable_block_level_subnet_config']:
426 if intersight.module.params['ipv4_config']:
427 intersight.api_body['IpV4Config'] = {
428 'Netmask': intersight.module.params['ipv4_config']['netmask'],
429 'Gateway': intersight.module.params['ipv4_config']['gateway'],
430 'PrimaryDns': intersight.module.params['ipv4_config']['primary_dns'],
431 'SecondaryDns': intersight.module.params['ipv4_config']['secondary_dns'],
432 }
433 else:
434 intersight.api_body['IpV4Config'] = None
435
436 if intersight.module.params['ipv6_config']:
437 intersight.api_body['IpV6Config'] = {
438 'Prefix': intersight.module.params['ipv6_config']['prefix'],
439 'Gateway': intersight.module.params['ipv6_config']['gateway'],
440 'PrimaryDns': intersight.module.params['ipv6_config']['primary_dns'],
441 'SecondaryDns': intersight.module.params['ipv6_config']['secondary_dns'],
442 }
443 else:
444 intersight.api_body['IpV6Config'] = None
445
446 intersight.configure_policy_or_profile(resource_path=resource_path)
447
448 module.exit_json(**intersight.result)
449
450
451 if __name__ == '__main__':
452 main()