Debian Codegraph @

ansible_collections/cisco/ucs/plugins/modules/ucs_lan_connectivity.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': 'certified'}
12
13 DOCUMENTATION = r'''
14 ---
15 module: ucs_lan_connectivity
16 short_description: Configures LAN Connectivity Policies on Cisco UCS Manager
17 description:
18 - Configures LAN Connectivity Policies on Cisco UCS Manager.
19 extends_documentation_fragment: cisco.ucs.ucs
20 options:
21 state:
22 description:
23 - If C(present), will verify LAN Connectivity Policies are present and will create if needed.
24 - If C(absent), will verify LAN Connectivity Policies are absent and will delete if needed.
25 choices: [present, absent]
26 default: present
27 type: str
28 name:
29 description:
30 - The name of the LAN Connectivity Policy.
31 - This name can be between 1 and 16 alphanumeric characters.
32 - "You cannot use spaces or any special characters other than - (hyphen), \"_\" (underscore), : (colon), and . (period)."
33 - You cannot change this name after the policy is created.
34 required: true
35 type: str
36 description:
37 description:
38 - A description of the LAN Connectivity Policy.
39 - Cisco recommends including information about where and when to use the policy.
40 - Enter up to 256 characters.
41 - "You can use any characters or spaces except the following:"
42 - "` (accent mark), \ (backslash), ^ (carat), \" (double quote), = (equal sign), > (greater than), < (less than), or ' (single quote)."
43 aliases: [ descr ]
44 type: str
45 vnic_list:
46 description:
47 - List of vNICs used by the LAN Connectivity Policy.
48 - vNICs used by the LAN Connectivity Policy must be created from a vNIC template.
49 type: list
50 elements: dict
51 suboptions:
52 name:
53 description:
54 - The name of the vNIC.
55 required: true
56 type: str
57 vnic_template:
58 description:
59 - The name of the vNIC template.
60 required: true
61 type: str
62 adapter_policy:
63 description:
64 - The name of the Ethernet adapter policy.
65 - A user defined policy can be used, or one of the system defined policies.
66 type: str
67 order:
68 description:
69 - String specifying the vNIC assignment order (e.g., '1', '2').
70 default: 'unspecified'
71 type: str
72 state:
73 description:
74 - If C(present), will verify vnic is configured within policy.
75 If C(absent), will verify vnic is absent from policy.
76 choices: [ present, absent ]
77 default: present
78 type: str
79 iscsi_vnic_list:
80 description:
81 - List of iSCSI vNICs used by the LAN Connectivity Policy.
82 type: list
83 elements: dict
84 suboptions:
85 name:
86 description:
87 - The name of the iSCSI vNIC.
88 required: true
89 type: str
90 overlay_vnic:
91 description:
92 - The LAN vNIC associated with this iSCSI vNIC.
93 type: str
94 iscsi_adapter_policy:
95 description:
96 - The iSCSI adapter policy associated with this iSCSI vNIC.
97 type: str
98 mac_address:
99 description:
100 - The MAC address associated with this iSCSI vNIC.
101 - If the MAC address is not set, Cisco UCS Manager uses a derived MAC address.
102 default: derived
103 type: str
104 vlan_name:
105 description:
106 - The VLAN used for the iSCSI vNIC.
107 default: default
108 type: str
109 state:
110 description:
111 - If C(present), will verify iscsi vnic is configured within policy.
112 If C(absent), will verify iscsi vnic is absent from policy.
113 choices: [ present, absent ]
114 default: present
115 type: str
116 org_dn:
117 description:
118 - Org dn (distinguished name)
119 default: org-root
120 type: str
121 requirements:
122 - ucsmsdk
123 author:
124 - David Soper (@dsoper2)
125 - John McDonough (@movinalot)
126 - CiscoUcs (@CiscoUcs)
127 '''
128
129 EXAMPLES = r'''
130 - name: Configure LAN Connectivity Policy
131 cisco.ucs.ucs_lan_connectivity:
132 hostname: 172.16.143.150
133 username: admin
134 password: password
135 name: Cntr-FC-Boot
136 vnic_list:
137 - name: eno1
138 vnic_template: Cntr-Template
139 adapter_policy: Linux
140 - name: eno2
141 vnic_template: Container-NFS-A
142 adapter_policy: Linux
143 - name: eno3
144 vnic_template: Container-NFS-B
145 adapter_policy: Linux
146 iscsi_vnic_list:
147 - name: iSCSIa
148 overlay_vnic: eno1
149 iscsi_adapter_policy: default
150 vlan_name: Container-MGMT-VLAN
151 - name: iSCSIb
152 overlay_vnic: eno3
153 iscsi_adapter_policy: default
154 vlan_name: Container-TNT-A-NFS
155
156 - name: Remove LAN Connectivity Policy
157 cisco.ucs.ucs_lan_connectivity:
158 hostname: 172.16.143.150
159 username: admin
160 password: password
161 name: Cntr-FC-Boot
162 state: absent
163 '''
164
165 RETURN = r'''
166 #
167 '''
168
169 from ansible.module_utils.basic import AnsibleModule
170 from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec
171
172
173 def configure_lan_connectivity(ucs, module, dn):
174 from ucsmsdk.mometa.vnic.VnicLanConnPolicy import VnicLanConnPolicy
175 from ucsmsdk.mometa.vnic.VnicEther import VnicEther
176 from ucsmsdk.mometa.vnic.VnicIScsiLCP import VnicIScsiLCP
177 from ucsmsdk.mometa.vnic.VnicVlan import VnicVlan
178
179 if not module.check_mode:
180 try:
181 # create if mo does not already exist
182 mo = VnicLanConnPolicy(
183 parent_mo_or_dn=module.params['org_dn'],
184 name=module.params['name'],
185 descr=module.params['description'],
186 )
187
188 if module.params.get('vnic_list'):
189 for vnic in module.params['vnic_list']:
190 if vnic['state'] == 'absent':
191 child_dn = dn + '/ether-' + vnic['name']
192 mo_1 = ucs.login_handle.query_dn(child_dn)
193 if mo_1:
194 ucs.login_handle.remove_mo(mo_1)
195 else: # state == 'present'
196 mo_1 = VnicEther(
197 addr='derived',
198 parent_mo_or_dn=mo,
199 name=vnic['name'],
200 adaptor_profile_name=vnic['adapter_policy'],
201 nw_templ_name=vnic['vnic_template'],
202 order=vnic['order'],
203 )
204
205 if module.params.get('iscsi_vnic_list'):
206 for iscsi_vnic in module.params['iscsi_vnic_list']:
207 if iscsi_vnic['state'] == 'absent':
208 child_dn = dn + '/iscsi-' + iscsi_vnic['name']
209 mo_1 = ucs.login_handle.query_dn(child_dn)
210 if mo_1:
211 ucs.login_handle.remove_mo(mo_1)
212 else: # state == 'present'
213 mo_1 = VnicIScsiLCP(
214 parent_mo_or_dn=mo,
215 name=iscsi_vnic['name'],
216 adaptor_profile_name=iscsi_vnic['iscsi_adapter_policy'],
217 vnic_name=iscsi_vnic['overlay_vnic'],
218 addr=iscsi_vnic['mac_address'],
219 )
220 VnicVlan(
221 parent_mo_or_dn=mo_1,
222 vlan_name=iscsi_vnic['vlan_name'],
223 )
224
225 ucs.login_handle.add_mo(mo, True)
226 ucs.login_handle.commit()
227 except Exception as e: # generic Exception handling because SDK can throw a variety of exceptions
228 ucs.result['msg'] = "setup error: %s " % str(e)
229 module.fail_json(**ucs.result)
230
231 ucs.result['changed'] = True
232
233
234 def check_vnic_props(ucs, module, dn):
235 props_match = True
236
237 if module.params.get('vnic_list'):
238 # check vnicEther props
239 for vnic in module.params['vnic_list']:
240 child_dn = dn + '/ether-' + vnic['name']
241 mo_1 = ucs.login_handle.query_dn(child_dn)
242 if mo_1:
243 if vnic['state'] == 'absent':
244 props_match = False
245 break
246 else: # state == 'present'
247 kwargs = dict(adaptor_profile_name=vnic['adapter_policy'])
248 kwargs['order'] = vnic['order']
249 kwargs['nw_templ_name'] = vnic['vnic_template']
250 if not (mo_1.check_prop_match(**kwargs)):
251 props_match = False
252 break
253 else: # mo_1 did not exist
254 if vnic['state'] == 'present':
255 props_match = False
256 break
257
258 return props_match
259
260
261 def check_iscsi_vnic_props(ucs, module, dn):
262 props_match = True
263
264 if module.params.get('iscsi_vnic_list'):
265 # check vnicIScsiLCP props
266 for iscsi_vnic in module.params['iscsi_vnic_list']:
267 child_dn = dn + '/iscsi-' + iscsi_vnic['name']
268 mo_1 = ucs.login_handle.query_dn(child_dn)
269 if mo_1:
270 if iscsi_vnic['state'] == 'absent':
271 props_match = False
272 break
273 else: # state == 'present'
274 kwargs = dict(vnic_name=iscsi_vnic['overlay_vnic'])
275 kwargs['adaptor_profile_name'] = iscsi_vnic['iscsi_adapter_policy']
276 kwargs['addr'] = iscsi_vnic['mac_address']
277 if (mo_1.check_prop_match(**kwargs)):
278 # check vlan
279 child_dn = child_dn + '/vlan'
280 mo_2 = ucs.login_handle.query_dn(child_dn)
281 if mo_2:
282 kwargs = dict(vlan_name=iscsi_vnic['vlan_name'])
283 if not (mo_2.check_prop_match(**kwargs)):
284 props_match = False
285 break
286 else: # mo_1 props did not match
287 props_match = False
288 break
289 else: # mo_1 did not exist
290 if iscsi_vnic['state'] == 'present':
291 props_match = False
292 break
293
294 return props_match
295
296
297 def check_lan_connecivity_props(ucs, module, mo, dn):
298 props_match = False
299
300 # check top-level mo props
301 kwargs = dict(descr=module.params['description'])
302 if (mo.check_prop_match(**kwargs)):
303 # top-level props match, check next level mo/props
304 # check vnic 1st
305 props_match = check_vnic_props(ucs, module, dn)
306
307 if props_match:
308 props_match = check_iscsi_vnic_props(ucs, module, dn)
309
310 return props_match
311
312
313 def main():
314 vnic = dict(
315 name=dict(type='str', required=True),
316 vnic_template=dict(type='str', required=True),
317 adapter_policy=dict(type='str'),
318 order=dict(type='str', default='unspecified'),
319 state=dict(type='str', default='present', choices=['present', 'absent']),
320 )
321 iscsi_vnic = dict(
322 name=dict(type='str', required=True),
323 overlay_vnic=dict(type='str'),
324 iscsi_adapter_policy=dict(type='str'),
325 mac_address=dict(type='str', default='derived'),
326 vlan_name=dict(type='str', default='default'),
327 state=dict(type='str', default='present', choices=['present', 'absent']),
328 )
329
330 argument_spec = ucs_argument_spec.copy()
331 argument_spec.update(
332 org_dn=dict(type='str', default='org-root'),
333 name=dict(type='str', required=True),
334 description=dict(type='str', aliases=['descr']),
335 vnic_list=dict(type='list', elements='dict', options=vnic),
336 iscsi_vnic_list=dict(type='list', elements='dict', options=iscsi_vnic),
337 state=dict(type='str', default='present', choices=['present', 'absent']),
338 )
339
340 module = AnsibleModule(
341 argument_spec,
342 supports_check_mode=True,
343 )
344 ucs = UCSModule(module)
345 # UCSModule creation above verifies ucsmsdk is present and exits on failure.
346 # Additional imports are done below or in called functions.
347
348 ucs.result['changed'] = False
349 props_match = False
350 # dn is <org_dn>/lan-conn-pol-<name>
351 dn = module.params['org_dn'] + '/lan-conn-pol-' + module.params['name']
352
353 mo = ucs.login_handle.query_dn(dn)
354 if mo:
355 if module.params['state'] == 'absent':
356 # mo must exist but all properties do not have to match
357 if not module.check_mode:
358 ucs.login_handle.remove_mo(mo)
359 ucs.login_handle.commit()
360 ucs.result['changed'] = True
361 else: # state == 'present'
362 props_match = check_lan_connecivity_props(ucs, module, mo, dn)
363
364 if module.params['state'] == 'present' and not props_match:
365 configure_lan_connectivity(ucs, module, dn)
366
367 module.exit_json(**ucs.result)
368
369
370 if __name__ == '__main__':
371 main()