Debian Codegraph @

ansible_collections/cisco/intersight/plugins/modules/intersight_iscsi_boot_policy.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_iscsi_boot_policy
16 short_description: Manage iSCSI Boot Policies for Cisco Intersight
17 description:
18 - Create, update, and delete iSCSI Boot Policies on Cisco Intersight.
19 - iSCSI boot policies define how servers boot from iSCSI targets using IPv4 or IPv6 protocols.
20 - Supports automatic target discovery (Auto) and manual configuration (Static) with DHCP, Pool, or Static IP sources.
21 - For more information see L(Cisco Intersight,https://intersight.com/apidocs/vnic/IscsiBootPolicies/get/).
22 extends_documentation_fragment: intersight
23 options:
24 state:
25 description:
26 - If C(present), will verify the resource is present and will create if needed.
27 - If C(absent), will verify the resource is absent and will delete if needed.
28 type: str
29 choices: [present, absent]
30 default: present
31 organization:
32 description:
33 - The name of the Organization this resource is assigned to.
34 - Policies created within a Custom Organization are applicable only to devices in the same Organization.
35 - Use 'default' for the default organization.
36 type: str
37 default: default
38 name:
39 description:
40 - The name assigned to the iSCSI Boot Policy.
41 - Must be unique within the organization.
42 - The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_.
43 type: str
44 required: true
45 description:
46 description:
47 - The user-defined description for the iSCSI Boot Policy.
48 - Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_).
49 type: str
50 aliases: [descr]
51 tags:
52 description:
53 - List of tags in Key:<user-defined key> Value:<user-defined value> format.
54 type: list
55 elements: dict
56 iscsi_ip_type:
57 description:
58 - Type of the IP address requested for iSCSI vNIC - IPv4/IPv6.
59 - ipv4 supports both auto and static target source types.
60 - ipv6 only supports static target source type (not auto).
61 - Both ipv4 and ipv6 support dhcp, pool, and static initiator IP sources.
62 type: str
63 choices: [ipv4, ipv6]
64 default: ipv4
65 target_source_type:
66 description:
67 - Target discovery method.
68 - auto - Automatic target discovery using DHCP vendor ID/IQN.
69 - static - Manual target configuration using target policies.
70 type: str
71 choices: [auto, static]
72 default: static
73 auto_targetvendor_name:
74 description:
75 - Auto target interface that is represented via the Initiator name or the DHCP vendor ID.
76 - The vendor ID can be up to 64 characters.
77 - Required when target_source_type is auto and iscsi_ip_type is ipv4.
78 type: str
79 iscsi_adapter_policy_name:
80 description:
81 - Relationship to the iSCSI Adapter Policy.
82 - Name of the iSCSI adapter policy to associate with this boot policy.
83 - Optional for all configurations.
84 type: str
85 primary_target_policy_name:
86 description:
87 - Specifies Target Profile information for iSCSI Boot.
88 - Name of the primary iSCSI static target policy.
89 - Required when target_source_type is static.
90 type: str
91 secondary_target_policy_name:
92 description:
93 - Specifies Target Profile information for iSCSI Boot.
94 - Optional when target_source_type is static.
95 type: str
96 chap:
97 description:
98 - CHAP authentication parameters for iSCSI Target.
99 - Provide user_id and password to enable CHAP authentication.
100 - Leave empty or omit to disable CHAP authentication.
101 type: dict
102 suboptions:
103 user_id:
104 description:
105 - CHAP username for authentication.
106 type: str
107 required: true
108 password:
109 description:
110 - CHAP password for authentication.
111 type: str
112 required: true
113 mutual_chap:
114 description:
115 - Mutual CHAP authentication parameters for iSCSI Initiator.
116 - Two-way CHAP mechanism.
117 - Provide user_id and password to enable Mutual CHAP authentication.
118 - Leave empty or omit to disable Mutual CHAP authentication.
119 type: dict
120 suboptions:
121 user_id:
122 description:
123 - Mutual CHAP username for authentication.
124 type: str
125 required: true
126 password:
127 description:
128 - Mutual CHAP password for authentication.
129 type: str
130 required: true
131 initiator_ip_source:
132 description:
133 - Method for assigning initiator IP address.
134 - dhcp - Obtain IP address automatically via DHCP.
135 - pool - Use an IP pool for address assignment.
136 - static - Manually configure static IP address.
137 type: str
138 choices: [dhcp, pool, static]
139 default: dhcp
140 initiator_ip_pool_name:
141 description:
142 - Name of the IP pool to use for initiator IP address assignment.
143 - Required when initiator_ip_source is pool.
144 type: str
145 initiator_static_ipv4_address:
146 description:
147 - Static IPv4 address provided for iSCSI Initiator.
148 - Required when initiator_ip_source is static and iscsi_ip_type is ipv4.
149 type: str
150 initiator_static_ipv4_netmask:
151 description:
152 - A subnet mask is a 32-bit number that masks an IP address and divides the IP address into network address and host address.
153 - Required when initiator_ip_source is static and iscsi_ip_type is ipv4.
154 type: str
155 initiator_static_ipv4_gateway:
156 description:
157 - IP address of the default IPv4 gateway.
158 - Required when initiator_ip_source is static and iscsi_ip_type is ipv4.
159 type: str
160 initiator_static_ipv4_primary_dns:
161 description:
162 - IP Address of the primary Domain Name System (DNS) server.
163 - Optional when initiator_ip_source is static and iscsi_ip_type is ipv4.
164 type: str
165 initiator_static_ipv4_secondary_dns:
166 description:
167 - IP Address of the secondary Domain Name System (DNS) server.
168 - Optional when initiator_ip_source is static and iscsi_ip_type is ipv4.
169 type: str
170 initiator_static_ipv6_address:
171 description:
172 - Static IPv6 address provided for iSCSI Initiator.
173 - Required when initiator_ip_source is static and iscsi_ip_type is ipv6.
174 type: str
175 initiator_static_ipv6_prefix:
176 description:
177 - The integer length of the prefix that masks the IP address and divides it into network and host addresses.
178 - Required when initiator_ip_source is static and iscsi_ip_type is ipv6.
179 type: int
180 initiator_static_ipv6_gateway:
181 description:
182 - IP address of the default IPv6 gateway.
183 - Required when initiator_ip_source is static and iscsi_ip_type is ipv6.
184 type: str
185 author:
186 - Ron Gershburg (@rgershbu)
187 '''
188
189 EXAMPLES = r'''
190 - name: Create iSCSI Boot Policy with Auto discovery (IPv4)
191 cisco.intersight.intersight_iscsi_boot_policy:
192 api_private_key: "{{ api_private_key }}"
193 api_key_id: "{{ api_key_id }}"
194 organization: "default"
195 name: "iscsi-boot-auto"
196 description: "iSCSI boot policy with auto discovery"
197 iscsi_ip_type: "ipv4"
198 target_source_type: "auto"
199 auto_targetvendor_name: "iqn.1991-05.com.cisco"
200 iscsi_adapter_policy_name: "iscsi-adapter-policy"
201 initiator_ip_source: "dhcp"
202 state: present
203
204 - name: Create iSCSI Boot Policy with Static targets and DHCP initiator (IPv4)
205 cisco.intersight.intersight_iscsi_boot_policy:
206 api_private_key: "{{ api_private_key }}"
207 api_key_id: "{{ api_key_id }}"
208 organization: "default"
209 name: "iscsi-boot-static-dhcp"
210 description: "iSCSI boot policy with static targets"
211 iscsi_ip_type: "ipv4"
212 target_source_type: "static"
213 primary_target_policy_name: "primary-target-policy"
214 secondary_target_policy_name: "secondary-target-policy"
215 chap:
216 user_id: "chapuser"
217 password: "chappassword123"
218 mutual_chap:
219 user_id: "mutualuser"
220 password: "mutualpassword123"
221 initiator_ip_source: "dhcp"
222 state: present
223
224 - name: Create iSCSI Boot Policy with Pool-based initiator IP
225 cisco.intersight.intersight_iscsi_boot_policy:
226 api_private_key: "{{ api_private_key }}"
227 api_key_id: "{{ api_key_id }}"
228 organization: "default"
229 name: "iscsi-boot-pool"
230 description: "iSCSI boot policy with IP pool"
231 iscsi_ip_type: "ipv4"
232 target_source_type: "static"
233 primary_target_policy_name: "primary-target-policy"
234 initiator_ip_source: "pool"
235 initiator_ip_pool_name: "iscsi-ip-pool"
236 state: present
237
238 - name: Create iSCSI Boot Policy with Static IPv4 configuration
239 cisco.intersight.intersight_iscsi_boot_policy:
240 api_private_key: "{{ api_private_key }}"
241 api_key_id: "{{ api_key_id }}"
242 organization: "default"
243 name: "iscsi-boot-static-ipv4"
244 description: "iSCSI boot policy with static IPv4"
245 iscsi_ip_type: "ipv4"
246 target_source_type: "static"
247 primary_target_policy_name: "primary-target-policy"
248 initiator_ip_source: "static"
249 initiator_static_ipv4_address: "192.168.1.100"
250 initiator_static_ipv4_netmask: "255.255.255.0"
251 initiator_static_ipv4_gateway: "192.168.1.1"
252 initiator_static_ipv4_primary_dns: "8.8.8.8"
253 initiator_static_ipv4_secondary_dns: "8.8.4.4"
254 tags:
255 - Key: Environment
256 Value: Production
257 state: present
258
259 - name: Create iSCSI Boot Policy with Static IPv6 configuration
260 cisco.intersight.intersight_iscsi_boot_policy:
261 api_private_key: "{{ api_private_key }}"
262 api_key_id: "{{ api_key_id }}"
263 organization: "default"
264 name: "iscsi-boot-static-ipv6"
265 description: "iSCSI boot policy with static IPv6"
266 iscsi_ip_type: "ipv6"
267 target_source_type: "static"
268 primary_target_policy_name: "primary-target-policy-ipv6"
269 initiator_ip_source: "static"
270 initiator_static_ipv6_address: "2001:db8::1"
271 initiator_static_ipv6_prefix: 64
272 initiator_static_ipv6_gateway: "2001:db8::ffff"
273 state: present
274
275 - name: Create iSCSI Boot Policy with IPv6 DHCP initiator
276 cisco.intersight.intersight_iscsi_boot_policy:
277 api_private_key: "{{ api_private_key }}"
278 api_key_id: "{{ api_key_id }}"
279 organization: "default"
280 name: "iscsi-boot-ipv6-dhcp"
281 description: "iSCSI boot policy with IPv6 DHCP"
282 iscsi_ip_type: "ipv6"
283 target_source_type: "static"
284 primary_target_policy_name: "primary-target-policy-ipv6"
285 initiator_ip_source: "dhcp"
286 state: present
287
288 - name: Create iSCSI Boot Policy with IPv6 Pool initiator
289 cisco.intersight.intersight_iscsi_boot_policy:
290 api_private_key: "{{ api_private_key }}"
291 api_key_id: "{{ api_key_id }}"
292 organization: "default"
293 name: "iscsi-boot-ipv6-pool"
294 description: "iSCSI boot policy with IPv6 pool"
295 iscsi_ip_type: "ipv6"
296 target_source_type: "static"
297 primary_target_policy_name: "primary-target-policy-ipv6"
298 initiator_ip_source: "pool"
299 initiator_ip_pool_name: "ipv6-ip-pool"
300 state: present
301
302 - name: Delete iSCSI Boot Policy
303 cisco.intersight.intersight_iscsi_boot_policy:
304 api_private_key: "{{ api_private_key }}"
305 api_key_id: "{{ api_key_id }}"
306 name: "iscsi-boot-auto"
307 state: absent
308 '''
309
310 RETURN = r'''
311 api_response:
312 description: The API response output returned by the specified resource.
313 returned: always
314 type: dict
315 sample:
316 "api_response": {
317 "Name": "iscsi-boot-auto",
318 "ObjectType": "vnic.IscsiBootPolicy",
319 "IscsiIpType": "IPv4",
320 "TargetSourceType": "Auto",
321 "AutoTargetvendorName": "iqn.1991-05.com.cisco",
322 "InitiatorIpSource": "DHCP",
323 "Moid": "1234567890abcdef12345678",
324 "Tags": [
325 {
326 "Key": "Environment",
327 "Value": "Production"
328 }
329 ]
330 }
331 '''
332
333
334 from ansible.module_utils.basic import AnsibleModule
335 from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec
336
337
338 def validate_parameters(module):
339 """
340 Validate module parameters for iSCSI boot policy configuration.
341 """
342 if module.params['state'] != 'present':
343 return
344
345 # Validate required parameters for present state
346 if not module.params.get('iscsi_ip_type'):
347 module.fail_json(msg="Parameter 'iscsi_ip_type' is required when state is present")
348 if not module.params.get('target_source_type'):
349 module.fail_json(msg="Parameter 'target_source_type' is required when state is present")
350 if not module.params.get('initiator_ip_source'):
351 module.fail_json(msg="Parameter 'initiator_ip_source' is required when state is present")
352
353 # Validate target source type requirements
354 if module.params['target_source_type'] == 'auto':
355 if not module.params.get('auto_targetvendor_name'):
356 module.fail_json(msg="Parameter 'auto_targetvendor_name' is required when target_source_type is auto")
357 elif module.params['target_source_type'] == 'static':
358 if not module.params.get('primary_target_policy_name'):
359 module.fail_json(msg="Parameter 'primary_target_policy_name' is required when target_source_type is static")
360
361 # Validate initiator IP source requirements
362 if module.params['initiator_ip_source'] == 'pool':
363 if not module.params.get('initiator_ip_pool_name'):
364 module.fail_json(msg="Parameter 'initiator_ip_pool_name' is required when initiator_ip_source is pool")
365
366 # Validate IPv6 doesn't use Auto target source
367 if module.params['iscsi_ip_type'] == 'ipv6' and module.params['target_source_type'] == 'auto':
368 module.fail_json(msg="ipv6 only supports static target source type, not auto")
369
370 # Validate Static IPv4 configuration
371 if module.params['iscsi_ip_type'] == 'ipv4' and module.params['initiator_ip_source'] == 'static':
372 required_ipv4_params = ['initiator_static_ipv4_address', 'initiator_static_ipv4_netmask', 'initiator_static_ipv4_gateway']
373 for param in required_ipv4_params:
374 if not module.params.get(param):
375 module.fail_json(msg=f"Parameter '{param}' is required when initiator_ip_source is static and iscsi_ip_type is ipv4")
376
377 # Validate Static IPv6 configuration
378 if module.params['iscsi_ip_type'] == 'ipv6' and module.params['initiator_ip_source'] == 'static':
379 required_ipv6_params = ['initiator_static_ipv6_address', 'initiator_static_ipv6_prefix', 'initiator_static_ipv6_gateway']
380 for param in required_ipv6_params:
381 if not module.params.get(param):
382 module.fail_json(msg=f"Parameter '{param}' is required when initiator_ip_source is static and iscsi_ip_type is ipv6")
383
384
385 def main():
386 argument_spec = intersight_argument_spec.copy()
387 argument_spec.update(
388 state=dict(type='str', choices=['present', 'absent'], default='present'),
389 organization=dict(type='str', default='default'),
390 name=dict(type='str', required=True),
391 description=dict(type='str', aliases=['descr']),
392 tags=dict(type='list', elements='dict'),
393 iscsi_ip_type=dict(type='str', choices=['ipv4', 'ipv6'], default='ipv4'),
394 target_source_type=dict(type='str', choices=['auto', 'static'], default='static'),
395 auto_targetvendor_name=dict(type='str'),
396 iscsi_adapter_policy_name=dict(type='str'),
397 primary_target_policy_name=dict(type='str'),
398 secondary_target_policy_name=dict(type='str'),
399 chap=dict(type='dict', options=dict(
400 user_id=dict(type='str', required=True),
401 password=dict(type='str', required=True, no_log=True)
402 )),
403 mutual_chap=dict(type='dict', options=dict(
404 user_id=dict(type='str', required=True),
405 password=dict(type='str', required=True, no_log=True)
406 )),
407 initiator_ip_source=dict(type='str', choices=['dhcp', 'pool', 'static'], default='dhcp'),
408 initiator_ip_pool_name=dict(type='str'),
409 initiator_static_ipv4_address=dict(type='str'),
410 initiator_static_ipv4_netmask=dict(type='str'),
411 initiator_static_ipv4_gateway=dict(type='str'),
412 initiator_static_ipv4_primary_dns=dict(type='str'),
413 initiator_static_ipv4_secondary_dns=dict(type='str'),
414 initiator_static_ipv6_address=dict(type='str'),
415 initiator_static_ipv6_prefix=dict(type='int'),
416 initiator_static_ipv6_gateway=dict(type='str'),
417 )
418
419 module = AnsibleModule(
420 argument_spec,
421 supports_check_mode=True,
422 )
423
424 intersight = IntersightModule(module)
425 intersight.result['api_response'] = {}
426 intersight.result['trace_id'] = ''
427
428 # Validate module parameters
429 validate_parameters(module)
430
431 # Resource path used to configure policy
432 resource_path = '/vnic/IscsiBootPolicies'
433
434 # Define API body used in compares or create
435 intersight.api_body = {
436 'Organization': {
437 'Name': module.params['organization'],
438 },
439 'Name': module.params['name'],
440 }
441
442 if module.params['state'] == 'present':
443 # Convert to proper API format: ipv4 -> IPv4, ipv6 -> IPv6
444 if module.params['iscsi_ip_type'] == 'ipv4':
445 intersight.api_body['IscsiIpType'] = 'IPv4'
446 else:
447 intersight.api_body['IscsiIpType'] = 'IPv6'
448 intersight.api_body['TargetSourceType'] = module.params['target_source_type'].capitalize()
449 intersight.api_body['InitiatorIpSource'] = module.params['initiator_ip_source'].upper() if module.params['initiator_ip_source'] == 'dhcp' \
450 else module.params['initiator_ip_source'].capitalize()
451
452 intersight.set_tags_and_description()
453
454 # Handle Auto target source configuration
455 if module.params['target_source_type'] == 'auto':
456 intersight.api_body['AutoTargetvendorName'] = module.params['auto_targetvendor_name']
457 else:
458 # Handle Static target source configuration
459 # Get primary target policy MOID
460 primary_target_moid = intersight.get_moid_by_name_and_org(
461 resource_path='/vnic/IscsiStaticTargetPolicies',
462 resource_name=module.params['primary_target_policy_name'],
463 organization_name=module.params['organization']
464 )
465 if not primary_target_moid:
466 module.fail_json(
467 msg=f"Primary target policy '{module.params['primary_target_policy_name']}' not found in organization '{module.params['organization']}'")
468 intersight.api_body['PrimaryTargetPolicy'] = primary_target_moid
469 # Get secondary target policy MOID if specified
470 if module.params.get('secondary_target_policy_name'):
471 secondary_target_moid = intersight.get_moid_by_name_and_org(
472 resource_path='/vnic/IscsiStaticTargetPolicies',
473 resource_name=module.params['secondary_target_policy_name'],
474 organization_name=module.params['organization']
475 )
476 if not secondary_target_moid:
477 module.fail_json(
478 msg=f"Secondary target policy '{module.params['secondary_target_policy_name']}' not found \
479 in organization '{module.params['organization']}'")
480 intersight.api_body['SecondaryTargetPolicy'] = secondary_target_moid
481
482 # Handle iSCSI adapter policy if specified
483 if module.params.get('iscsi_adapter_policy_name'):
484 adapter_policy_moid = intersight.get_moid_by_name_and_org(
485 resource_path='/vnic/IscsiAdapterPolicies',
486 resource_name=module.params['iscsi_adapter_policy_name'],
487 organization_name=module.params['organization']
488 )
489 if not adapter_policy_moid:
490 module.fail_json(
491 msg=f"iSCSI adapter policy '{module.params['iscsi_adapter_policy_name']}' not found in organization '{module.params['organization']}'")
492 intersight.api_body['IscsiAdapterPolicy'] = adapter_policy_moid
493 # Handle CHAP authentication
494 chap_config = {
495 'UserId': '',
496 'Password': '',
497 'IsPasswordSet': False
498 }
499 if module.params.get('chap'):
500 chap_config['UserId'] = module.params['chap']['user_id']
501 chap_config['Password'] = module.params['chap']['password']
502 intersight.api_body['Chap'] = chap_config
503 # Handle Mutual CHAP authentication
504 mutual_chap_config = {
505 'UserId': '',
506 'Password': '',
507 'IsPasswordSet': False
508 }
509 if module.params.get('mutual_chap'):
510 mutual_chap_config['UserId'] = module.params['mutual_chap']['user_id']
511 mutual_chap_config['Password'] = module.params['mutual_chap']['password']
512 intersight.api_body['MutualChap'] = mutual_chap_config
513
514 # Handle initiator IP configuration based on IP source
515 if module.params['initiator_ip_source'] == 'pool':
516 # Get IP pool MOID
517 ip_pool_moid = intersight.get_moid_by_name_and_org(
518 resource_path='/ippool/Pools',
519 resource_name=module.params['initiator_ip_pool_name'],
520 organization_name=module.params['organization']
521 )
522 if not ip_pool_moid:
523 module.fail_json(msg=f"IP pool '{module.params['initiator_ip_pool_name']}' not found in organization '{module.params['organization']}'")
524 intersight.api_body['InitiatorIpPool'] = ip_pool_moid
525 elif module.params['initiator_ip_source'] == 'static':
526 if module.params['iscsi_ip_type'] == 'ipv4':
527 intersight.api_body['InitiatorStaticIpV4Address'] = module.params['initiator_static_ipv4_address']
528 ipv4_config = {
529 'Netmask': module.params['initiator_static_ipv4_netmask'],
530 'Gateway': module.params['initiator_static_ipv4_gateway']
531 }
532 if module.params.get('initiator_static_ipv4_primary_dns'):
533 ipv4_config['PrimaryDns'] = module.params['initiator_static_ipv4_primary_dns']
534 if module.params.get('initiator_static_ipv4_secondary_dns'):
535 ipv4_config['SecondaryDns'] = module.params['initiator_static_ipv4_secondary_dns']
536 intersight.api_body['InitiatorStaticIpV4Config'] = ipv4_config
537 else:
538 # IPv6 configuration
539 intersight.api_body['InitiatorStaticIpV6Address'] = module.params['initiator_static_ipv6_address']
540 ipv6_config = {
541 'Prefix': module.params['initiator_static_ipv6_prefix'],
542 'Gateway': module.params['initiator_static_ipv6_gateway']
543 }
544 intersight.api_body['InitiatorStaticIpV6Config'] = ipv6_config
545
546 intersight.configure_policy_or_profile(resource_path=resource_path)
547
548 module.exit_json(**intersight.result)
549
550
551 if __name__ == '__main__':
552 main()