Debian Codegraph @

ansible_collections/cisco/ucs/plugins/modules/ucs_vhba_template.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_vhba_template
16 short_description: Configures vHBA templates on Cisco UCS Manager
17 description:
18 - Configures vHBA templates on Cisco UCS Manager.
19 extends_documentation_fragment: cisco.ucs.ucs
20 options:
21 state:
22 description:
23 - If C(present), will verify vHBA templates are present and will create if needed.
24 - If C(absent), will verify vHBA templates 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 virtual HBA template.
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 template is created.
34 required: yes
35 type: str
36 description:
37 description:
38 - A user-defined description of the template.
39 - Enter up to 256 characters.
40 - "You can use any characters or spaces except the following:"
41 - "` (accent mark), \ (backslash), ^ (carat), \" (double quote), = (equal sign), > (greater than), < (less than), or ' (single quote)."
42 aliases: [ descr ]
43 type: str
44 fabric:
45 description:
46 - The Fabric ID field.
47 - The name of the fabric interconnect that vHBAs created with this template are associated with.
48 choices: [A, B]
49 default: A
50 type: str
51 redundancy_type:
52 description:
53 - The Redundancy Type used for template pairing from the Primary or Secondary redundancy template.
54 - "primary — Creates configurations that can be shared with the Secondary template."
55 - Any other shared changes on the Primary template are automatically synchronized to the Secondary template.
56 - "secondary — All shared configurations are inherited from the Primary template."
57 - "none - Legacy vHBA template behavior. Select this option if you do not want to use redundancy."
58 choices: [none, primary, secondary]
59 default: none
60 type: str
61 vsan:
62 description:
63 - The VSAN to associate with vHBAs created from this template.
64 default: default
65 type: str
66 template_type:
67 description:
68 - The Template Type field.
69 - "This can be one of the following:"
70 - "initial-template — vHBAs created from this template are not updated if the template changes."
71 - "updating-template - vHBAs created from this template are updated if the template changes."
72 choices: [initial-template, updating-template]
73 default: initial-template
74 type: str
75 max_data:
76 description:
77 - The Max Data Field Size field.
78 - The maximum size of the Fibre Channel frame payload bytes that the vHBA supports.
79 - Enter an string between '256' and '2112'.
80 default: '2048'
81 type: str
82 wwpn_pool:
83 description:
84 - The WWPN pool that a vHBA created from this template uses to derive its WWPN address.
85 default: default
86 type: str
87 qos_policy:
88 description:
89 - The QoS policy that is associated with vHBAs created from this template.
90 type: str
91 pin_group:
92 description:
93 - The SAN pin group that is associated with vHBAs created from this template.
94 type: str
95 stats_policy:
96 description:
97 - The statistics collection policy that is associated with vHBAs created from this template.
98 default: default
99 type: str
100 org_dn:
101 description:
102 - Org dn (distinguished name)
103 default: org-root
104 type: str
105 requirements:
106 - ucsmsdk
107 author:
108 - David Soper (@dsoper2)
109 - John McDonough (@movinalot)
110 - CiscoUcs (@CiscoUcs)
111 '''
112
113 EXAMPLES = r'''
114 - name: Configure vHBA template
115 cisco.ucs.ucs_vhba_template:
116 hostname: 172.16.143.150
117 username: admin
118 password: password
119 name: vHBA-A
120 fabric: A
121 vsan: VSAN-A
122 wwpn_pool: WWPN-Pool-A
123
124 - name: Remote vHBA template
125 cisco.ucs.ucs_vhba_template:
126 hostname: 172.16.143.150
127 username: admin
128 password: password
129 name: vHBA-A
130 state: absent
131 '''
132
133 RETURN = r'''
134 #
135 '''
136
137 from ansible.module_utils.basic import AnsibleModule
138 from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec
139
140
141 def main():
142 argument_spec = ucs_argument_spec.copy()
143 argument_spec.update(
144 org_dn=dict(type='str', default='org-root'),
145 name=dict(type='str', required=True),
146 description=dict(type='str', aliases=['descr']),
147 fabric=dict(type='str', default='A', choices=['A', 'B']),
148 redundancy_type=dict(type='str', default='none', choices=['none', 'primary', 'secondary']),
149 vsan=dict(type='str', default='default'),
150 template_type=dict(type='str', default='initial-template', choices=['initial-template', 'updating-template']),
151 max_data=dict(type='str', default='2048'),
152 wwpn_pool=dict(type='str', default='default'),
153 qos_policy=dict(type='str'),
154 pin_group=dict(type='str'),
155 stats_policy=dict(type='str', default='default'),
156 state=dict(type='str', default='present', choices=['present', 'absent']),
157 )
158
159 module = AnsibleModule(
160 argument_spec,
161 supports_check_mode=True,
162 )
163 ucs = UCSModule(module)
164
165 err = False
166
167 from ucsmsdk.mometa.vnic.VnicSanConnTempl import VnicSanConnTempl
168 from ucsmsdk.mometa.vnic.VnicFcIf import VnicFcIf
169
170 changed = False
171 try:
172 vhba_template_list = [module.params]
173 for vhba_template in vhba_template_list:
174 mo_exists = False
175 props_match = False
176 # set default params. Done here to set values for lists which can't be done in the argument_spec
177 if not vhba_template.get('descr'):
178 vhba_template['descr'] = ''
179 if not vhba_template.get('fabric'):
180 vhba_template['fabric'] = 'A'
181 if not vhba_template.get('redundancy_type'):
182 vhba_template['redundancy_type'] = 'none'
183 if not vhba_template.get('vsan'):
184 vhba_template['vsan'] = 'default'
185 if not vhba_template.get('template_type'):
186 vhba_template['template_type'] = 'initial-template'
187 if not vhba_template.get('max_data'):
188 vhba_template['max_data'] = '2048'
189 if not vhba_template.get('wwpn_pool'):
190 vhba_template['wwpn_pool'] = 'default'
191 if not vhba_template.get('qos_policy'):
192 vhba_template['qos_policy'] = ''
193 if not vhba_template.get('pin_group'):
194 vhba_template['pin_group'] = ''
195 if not vhba_template.get('stats_policy'):
196 vhba_template['stats_policy'] = 'default'
197 # dn is <org_dn>/san-conn-templ-<name>
198 dn = module.params['org_dn'] + '/san-conn-templ-' + vhba_template['name']
199
200 mo = ucs.login_handle.query_dn(dn)
201 if mo:
202 mo_exists = True
203 # check top-level mo props
204 kwargs = dict(descr=vhba_template['descr'])
205 kwargs['switch_id'] = vhba_template['fabric']
206 kwargs['redundancy_pair_type'] = vhba_template['redundancy_type']
207 kwargs['templ_type'] = vhba_template['template_type']
208 kwargs['max_data_field_size'] = vhba_template['max_data']
209 kwargs['ident_pool_name'] = vhba_template['wwpn_pool']
210 kwargs['qos_policy_name'] = vhba_template['qos_policy']
211 kwargs['pin_to_group_name'] = vhba_template['pin_group']
212 kwargs['stats_policy_name'] = vhba_template['stats_policy']
213 if (mo.check_prop_match(**kwargs)):
214 # top-level props match, check next level mo/props
215 child_dn = dn + '/if-default'
216 mo_1 = ucs.login_handle.query_dn(child_dn)
217 if mo_1:
218 kwargs = dict(name=vhba_template['vsan'])
219 if (mo_1.check_prop_match(**kwargs)):
220 props_match = True
221
222 if module.params['state'] == 'absent':
223 # mo must exist but all properties do not have to match
224 if mo_exists:
225 if not module.check_mode:
226 ucs.login_handle.remove_mo(mo)
227 ucs.login_handle.commit()
228 changed = True
229 else:
230 if not props_match:
231 if not module.check_mode:
232 # create if mo does not already exist
233 mo = VnicSanConnTempl(
234 parent_mo_or_dn=module.params['org_dn'],
235 name=vhba_template['name'],
236 descr=vhba_template['descr'],
237 switch_id=vhba_template['fabric'],
238 redundancy_pair_type=vhba_template['redundancy_type'],
239 templ_type=vhba_template['template_type'],
240 max_data_field_size=vhba_template['max_data'],
241 ident_pool_name=vhba_template['wwpn_pool'],
242 qos_policy_name=vhba_template['qos_policy'],
243 pin_to_group_name=vhba_template['pin_group'],
244 stats_policy_name=vhba_template['stats_policy'],
245 )
246
247 mo_1 = VnicFcIf(
248 parent_mo_or_dn=mo,
249 name=vhba_template['vsan'],
250 )
251
252 ucs.login_handle.add_mo(mo, True)
253 ucs.login_handle.commit()
254 changed = True
255
256 except Exception as e:
257 err = True
258 ucs.result['msg'] = "setup error: %s " % str(e)
259
260 ucs.result['changed'] = changed
261 if err:
262 module.fail_json(**ucs.result)
263 module.exit_json(**ucs.result)
264
265
266 if __name__ == '__main__':
267 main()