| 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_vnic_template |
| 16 |
short_description: Configures vNIC templates on Cisco UCS Manager |
| 17 |
description: |
| 18 |
- Configures vNIC templates on Cisco UCS Manager. |
| 19 |
extends_documentation_fragment: cisco.ucs.ucs |
| 20 |
options: |
| 21 |
state: |
| 22 |
description: |
| 23 |
- If C(present), will verify vNIC templates are present and will create if needed. |
| 24 |
- If C(absent), will verify vNIC 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 vNIC 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 vNIC 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 specifying the fabric interconnect associated with vNICs created from this template. |
| 47 |
- If you want fabric failover enabled on vNICs created from this template, use of of the following:" |
| 48 |
- "A-B to use Fabric A by default with failover enabled." |
| 49 |
- "B-A to use Fabric B by default with failover enabled." |
| 50 |
- "Do not enable vNIC fabric failover under the following circumstances:" |
| 51 |
- "- If the Cisco UCS domain is running in Ethernet switch mode. vNIC fabric failover is not supported in that mode." |
| 52 |
- "- If you plan to associate one or more vNICs created from this template to a server with an adapter that does not support fabric failover." |
| 53 |
choices: [A, B, A-B, B-A] |
| 54 |
default: A |
| 55 |
type: str |
| 56 |
redundancy_type: |
| 57 |
description: |
| 58 |
- The Redundancy Type used for vNIC redundancy pairs during fabric failover. |
| 59 |
- "This can be one of the following:" |
| 60 |
- "primary — Creates configurations that can be shared with the Secondary template." |
| 61 |
- "secondary — All shared configurations are inherited from the Primary template." |
| 62 |
- "none - Legacy vNIC template behavior. Select this option if you do not want to use redundancy." |
| 63 |
choices: [none, primary, secondary] |
| 64 |
default: none |
| 65 |
type: str |
| 66 |
peer_redundancy_template: |
| 67 |
description: |
| 68 |
- The Peer Redundancy Template. |
| 69 |
- The name of the vNIC template sharing a configuration with this template. |
| 70 |
- If the redundancy_type is primary, the name of the secondary template should be provided. |
| 71 |
- If the redundancy_type is secondary, the name of the primary template should be provided. |
| 72 |
- Secondary templates can only configure non-shared properties (name, description, and mac_pool). |
| 73 |
aliases: [ peer_redundancy_templ ] |
| 74 |
type: str |
| 75 |
target: |
| 76 |
description: |
| 77 |
- The possible target for vNICs created from this template. |
| 78 |
- The target determines whether or not Cisco UCS Manager automatically creates a VM-FEX port profile with the appropriate settings for the vNIC template. |
| 79 |
- "This can be one of the following:" |
| 80 |
- "adapter — The vNICs apply to all adapters. No VM-FEX port profile is created if you choose this option." |
| 81 |
- "vm - The vNICs apply to all virtual machines. A VM-FEX port profile is created if you choose this option." |
| 82 |
choices: [adapter, vm] |
| 83 |
default: adapter |
| 84 |
type: str |
| 85 |
template_type: |
| 86 |
description: |
| 87 |
- The Template Type field. |
| 88 |
- "This can be one of the following:" |
| 89 |
- "initial-template — vNICs created from this template are not updated if the template changes." |
| 90 |
- "updating-template - vNICs created from this template are updated if the template changes." |
| 91 |
choices: [initial-template, updating-template] |
| 92 |
default: initial-template |
| 93 |
type: str |
| 94 |
vlans_list: |
| 95 |
description: |
| 96 |
- List of VLANs used by the vNIC template. |
| 97 |
type: list |
| 98 |
elements: dict |
| 99 |
suboptions: |
| 100 |
name: |
| 101 |
description: |
| 102 |
- The name of the VLAN (required if state is present). |
| 103 |
type: str |
| 104 |
native: |
| 105 |
description: |
| 106 |
- Designates the VLAN as a native VLAN. Only one VLAN in the list can be a native VLAN. |
| 107 |
choices: ['no', 'yes'] |
| 108 |
default: 'no' |
| 109 |
type: str |
| 110 |
multicast_policy: |
| 111 |
description: |
| 112 |
- Note this option is not used and is only provided to allow use of ucs_vlans parameters in the vlan_list. |
| 113 |
- The multicast policy associated with this VLAN. |
| 114 |
type: str |
| 115 |
fabric: |
| 116 |
description: |
| 117 |
- Note this option is not used and is only provided to allow use of ucs_vlans parameters in the vlan_list. |
| 118 |
- "The fabric configuration of the VLAN. This can be one of the following:" |
| 119 |
- "common - The VLAN applies to both fabrics and uses the same configuration parameters in both cases." |
| 120 |
- "A — The VLAN only applies to fabric A." |
| 121 |
- "B — The VLAN only applies to fabric B." |
| 122 |
choices: [common, A, B] |
| 123 |
type: str |
| 124 |
id: |
| 125 |
description: |
| 126 |
- Note this option is not used and is only provided to allow use of ucs_vlans parameters in the vlan_list. |
| 127 |
- The unique string identifier assigned to the VLAN. |
| 128 |
type: str |
| 129 |
sharing: |
| 130 |
description: |
| 131 |
- Note this option is not used and is only provided to allow use of ucs_vlans parameters in the vlan_list. |
| 132 |
- The Sharing Type field. |
| 133 |
- "Whether this VLAN is subdivided into private or secondary VLANs. This can be one of the following:" |
| 134 |
- "none - This VLAN does not have any secondary or private VLANs. This is a regular VLAN." |
| 135 |
- "primary - This VLAN can have one or more secondary VLANs, as shown in the Secondary VLANs area." |
| 136 |
- "isolated - This is a private VLAN associated with a primary VLAN. This VLAN is an Isolated VLAN." |
| 137 |
- "community - This VLAN can communicate with other ports on the same community VLAN as well as the promiscuous port. This VLAN is a Community VLAN." |
| 138 |
choices: [none, primary, isolated, community] |
| 139 |
type: str |
| 140 |
state: |
| 141 |
description: |
| 142 |
- If present, will verify VLAN is present on template. |
| 143 |
- If absent, will verify VLAN is absent on template. |
| 144 |
choices: [present, absent] |
| 145 |
default: present |
| 146 |
type: str |
| 147 |
cdn_source: |
| 148 |
description: |
| 149 |
- CDN Source field. |
| 150 |
- "This can be one of the following options:" |
| 151 |
- "vnic-name - Uses the vNIC template name of the vNIC instance as the CDN name. This is the default option." |
| 152 |
- "user-defined - Uses a user-defined CDN name for the vNIC template. If this option is chosen, cdn_name must also be provided." |
| 153 |
choices: [vnic-name, user-defined] |
| 154 |
default: vnic-name |
| 155 |
type: str |
| 156 |
cdn_name: |
| 157 |
description: |
| 158 |
- CDN Name used when cdn_source is set to user-defined. |
| 159 |
type: str |
| 160 |
mtu: |
| 161 |
description: |
| 162 |
- The MTU field. |
| 163 |
- The maximum transmission unit, or packet size, that vNICs created from this vNIC template should use. |
| 164 |
- Enter a string between '1500' and '9000'. |
| 165 |
- If the vNIC template has an associated QoS policy, the MTU specified here must be equal to or less than the MTU specified in the QoS system class. |
| 166 |
default: '1500' |
| 167 |
type: str |
| 168 |
mac_pool: |
| 169 |
description: |
| 170 |
- The MAC address pool that vNICs created from this vNIC template should use. |
| 171 |
type: str |
| 172 |
qos_policy: |
| 173 |
description: |
| 174 |
- The quality of service (QoS) policy that vNICs created from this vNIC template should use. |
| 175 |
type: str |
| 176 |
network_control_policy: |
| 177 |
description: |
| 178 |
- The network control policy that vNICs created from this vNIC template should use. |
| 179 |
type: str |
| 180 |
pin_group: |
| 181 |
description: |
| 182 |
- The LAN pin group that vNICs created from this vNIC template should use. |
| 183 |
type: str |
| 184 |
stats_policy: |
| 185 |
description: |
| 186 |
- The statistics collection policy that vNICs created from this vNIC template should use. |
| 187 |
type: str |
| 188 |
org_dn: |
| 189 |
description: |
| 190 |
- Org dn (distinguished name) |
| 191 |
default: org-root |
| 192 |
type: str |
| 193 |
requirements: |
| 194 |
- ucsmsdk |
| 195 |
author: |
| 196 |
- David Soper (@dsoper2) |
| 197 |
- John McDonough (@movinalot) |
| 198 |
- CiscoUcs (@CiscoUcs) |
| 199 |
''' |
| 200 |
|
| 201 |
EXAMPLES = r''' |
| 202 |
- name: Configure vNIC template |
| 203 |
cisco.ucs.ucs_vnic_template: |
| 204 |
hostname: 172.16.143.150 |
| 205 |
username: admin |
| 206 |
password: password |
| 207 |
name: vNIC-A |
| 208 |
fabric: A |
| 209 |
vlans_list: |
| 210 |
- name: default |
| 211 |
native: 'yes' |
| 212 |
|
| 213 |
- name: Configure vNIC template with failover |
| 214 |
cisco.ucs.ucs_vnic_template: |
| 215 |
hostname: 172.16.143.150 |
| 216 |
username: admin |
| 217 |
password: password |
| 218 |
name: vNIC-A-B |
| 219 |
fabric: A-B |
| 220 |
vlans_list: |
| 221 |
- name: default |
| 222 |
native: 'yes' |
| 223 |
state: present |
| 224 |
|
| 225 |
- name: Remove vNIC template |
| 226 |
cisco.ucs.ucs_vnic_template: |
| 227 |
hostname: 172.16.143.150 |
| 228 |
username: admin |
| 229 |
password: password |
| 230 |
name: vNIC-A |
| 231 |
state: absent |
| 232 |
|
| 233 |
- name: Remove another vNIC template |
| 234 |
cisco.ucs.ucs_vnic_template: |
| 235 |
hostname: 172.16.143.150 |
| 236 |
username: admin |
| 237 |
password: password |
| 238 |
name: vNIC-A-B |
| 239 |
state: absent |
| 240 |
|
| 241 |
- name: Remove VLAN from template |
| 242 |
cisco.ucs.ucs_vnic_template: |
| 243 |
hostname: 172.16.143.150 |
| 244 |
username: admin |
| 245 |
password: password |
| 246 |
name: vNIC-A-B |
| 247 |
fabric: A-B |
| 248 |
vlans_list: |
| 249 |
- name: default |
| 250 |
native: 'yes' |
| 251 |
state: absent |
| 252 |
''' |
| 253 |
|
| 254 |
RETURN = r''' |
| 255 |
# |
| 256 |
''' |
| 257 |
|
| 258 |
from ansible.module_utils.basic import AnsibleModule |
| 259 |
from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec |
| 260 |
|
| 261 |
|
| 262 |
def main(): |
| 263 |
vlans_list = dict( |
| 264 |
name=dict(type='str'), |
| 265 |
multicast_policy=dict(type='str'), |
| 266 |
fabric=dict(type='str', choices=['common', 'A', 'B']), |
| 267 |
id=dict(type='str'), |
| 268 |
sharing=dict(type='str', choices=['none', 'primary', 'isolated', 'community']), |
| 269 |
native=dict(type='str', default='no', choices=['no', 'yes']), |
| 270 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 271 |
) |
| 272 |
argument_spec = ucs_argument_spec.copy() |
| 273 |
argument_spec.update( |
| 274 |
org_dn=dict(type='str', default='org-root'), |
| 275 |
name=dict(type='str', required=True), |
| 276 |
description=dict(type='str', aliases=['descr']), |
| 277 |
fabric=dict(type='str', default='A', choices=['A', 'B', 'A-B', 'B-A']), |
| 278 |
redundancy_type=dict(type='str', default='none', choices=['none', 'primary', 'secondary']), |
| 279 |
peer_redundancy_template=dict(type='str', aliases=['peer_redundancy_templ']), |
| 280 |
target=dict(type='str', default='adapter', choices=['adapter', 'vm']), |
| 281 |
template_type=dict(type='str', default='initial-template', choices=['initial-template', 'updating-template']), |
| 282 |
vlans_list=dict(type='list', elements='dict', options=vlans_list), |
| 283 |
cdn_source=dict(type='str', default='vnic-name', choices=['vnic-name', 'user-defined']), |
| 284 |
cdn_name=dict(type='str'), |
| 285 |
mtu=dict(type='str', default='1500'), |
| 286 |
mac_pool=dict(type='str'), |
| 287 |
qos_policy=dict(type='str'), |
| 288 |
network_control_policy=dict(type='str'), |
| 289 |
pin_group=dict(type='str'), |
| 290 |
stats_policy=dict(type='str'), |
| 291 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 292 |
) |
| 293 |
|
| 294 |
module = AnsibleModule( |
| 295 |
argument_spec, |
| 296 |
supports_check_mode=True, |
| 297 |
required_if=[ |
| 298 |
['cdn_source', 'user-defined', ['cdn_name']], |
| 299 |
], |
| 300 |
) |
| 301 |
ucs = UCSModule(module) |
| 302 |
|
| 303 |
err = False |
| 304 |
|
| 305 |
# UCSModule creation above verifies ucsmsdk is present and exits on failure. Additional imports are done below. |
| 306 |
from ucsmsdk.mometa.vnic.VnicLanConnTempl import VnicLanConnTempl |
| 307 |
from ucsmsdk.mometa.vnic.VnicEtherIf import VnicEtherIf |
| 308 |
|
| 309 |
changed = False |
| 310 |
try: |
| 311 |
mo_exists = False |
| 312 |
props_match = False |
| 313 |
# dn is <org_dn>/lan-conn-templ-<name> |
| 314 |
dn = module.params['org_dn'] + '/lan-conn-templ-' + module.params['name'] |
| 315 |
|
| 316 |
mo = ucs.login_handle.query_dn(dn) |
| 317 |
if mo: |
| 318 |
mo_exists = True |
| 319 |
|
| 320 |
if module.params['state'] == 'absent': |
| 321 |
# mo must exist but all properties do not have to match |
| 322 |
if mo_exists: |
| 323 |
if not module.check_mode: |
| 324 |
ucs.login_handle.remove_mo(mo) |
| 325 |
ucs.login_handle.commit() |
| 326 |
changed = True |
| 327 |
else: |
| 328 |
# set default params for lists which can't be done in the argument_spec |
| 329 |
if module.params.get('vlans_list'): |
| 330 |
for vlan in module.params['vlans_list']: |
| 331 |
if not vlan.get('native'): |
| 332 |
vlan['native'] = 'no' |
| 333 |
if not vlan.get('state'): |
| 334 |
vlan['state'] = 'present' |
| 335 |
# for target 'adapter', change to internal UCS Manager spelling 'adaptor' |
| 336 |
if module.params['target'] == 'adapter': |
| 337 |
module.params['target'] = 'adaptor' |
| 338 |
if mo_exists: |
| 339 |
# check top-level mo props |
| 340 |
kwargs = dict(descr=module.params['description']) |
| 341 |
kwargs['switch_id'] = module.params['fabric'] |
| 342 |
kwargs['redundancy_pair_type'] = module.params['redundancy_type'] |
| 343 |
kwargs['peer_redundancy_templ_name'] = module.params['peer_redundancy_template'] |
| 344 |
kwargs['ident_pool_name'] = module.params['mac_pool'] |
| 345 |
# do not check shared props if this is a secondary template |
| 346 |
if module.params['redundancy_type'] != 'secondary': |
| 347 |
kwargs['target'] = module.params['target'] |
| 348 |
kwargs['templ_type'] = module.params['template_type'] |
| 349 |
kwargs['cdn_source'] = module.params['cdn_source'] |
| 350 |
kwargs['admin_cdn_name'] = module.params['cdn_name'] |
| 351 |
kwargs['mtu'] = module.params['mtu'] |
| 352 |
kwargs['qos_policy_name'] = module.params['qos_policy'] |
| 353 |
kwargs['nw_ctrl_policy_name'] = module.params['network_control_policy'] |
| 354 |
kwargs['pin_to_group_name'] = module.params['pin_group'] |
| 355 |
kwargs['stats_policy_name'] = module.params['stats_policy'] |
| 356 |
if mo.check_prop_match(**kwargs): |
| 357 |
# top-level props match, check next level mo/props |
| 358 |
if not module.params.get('vlans_list'): |
| 359 |
props_match = True |
| 360 |
else: |
| 361 |
# check vlan props |
| 362 |
for vlan in module.params['vlans_list']: |
| 363 |
child_dn = dn + '/if-' + str(vlan['name']) |
| 364 |
mo_1 = ucs.login_handle.query_dn(child_dn) |
| 365 |
if vlan['state'] == 'absent': |
| 366 |
if mo_1: |
| 367 |
props_match = False |
| 368 |
break |
| 369 |
else: |
| 370 |
if mo_1: |
| 371 |
kwargs = dict(default_net=vlan['native']) |
| 372 |
if mo_1.check_prop_match(**kwargs): |
| 373 |
props_match = True |
| 374 |
else: |
| 375 |
props_match = False |
| 376 |
break |
| 377 |
|
| 378 |
if not props_match: |
| 379 |
if not module.check_mode: |
| 380 |
# create if mo does not already exist |
| 381 |
# secondary template only sets non shared props |
| 382 |
if module.params['redundancy_type'] == 'secondary': |
| 383 |
mo = VnicLanConnTempl( |
| 384 |
parent_mo_or_dn=module.params['org_dn'], |
| 385 |
name=module.params['name'], |
| 386 |
descr=module.params['description'], |
| 387 |
switch_id=module.params['fabric'], |
| 388 |
redundancy_pair_type=module.params['redundancy_type'], |
| 389 |
peer_redundancy_templ_name=module.params['peer_redundancy_template'], |
| 390 |
ident_pool_name=module.params['mac_pool'], |
| 391 |
) |
| 392 |
else: |
| 393 |
mo = VnicLanConnTempl( |
| 394 |
parent_mo_or_dn=module.params['org_dn'], |
| 395 |
name=module.params['name'], |
| 396 |
descr=module.params['description'], |
| 397 |
switch_id=module.params['fabric'], |
| 398 |
redundancy_pair_type=module.params['redundancy_type'], |
| 399 |
peer_redundancy_templ_name=module.params['peer_redundancy_template'], |
| 400 |
target=module.params['target'], |
| 401 |
templ_type=module.params['template_type'], |
| 402 |
cdn_source=module.params['cdn_source'], |
| 403 |
admin_cdn_name=module.params['cdn_name'], |
| 404 |
mtu=module.params['mtu'], |
| 405 |
ident_pool_name=module.params['mac_pool'], |
| 406 |
qos_policy_name=module.params['qos_policy'], |
| 407 |
nw_ctrl_policy_name=module.params['network_control_policy'], |
| 408 |
pin_to_group_name=module.params['pin_group'], |
| 409 |
stats_policy_name=module.params['stats_policy'], |
| 410 |
) |
| 411 |
|
| 412 |
if module.params.get('vlans_list'): |
| 413 |
for vlan in module.params['vlans_list']: |
| 414 |
if vlan['state'] == 'absent': |
| 415 |
child_dn = dn + '/if-' + str(vlan['name']) |
| 416 |
mo_1 = ucs.login_handle.query_dn(child_dn) |
| 417 |
ucs.login_handle.remove_mo(mo_1) |
| 418 |
else: |
| 419 |
mo_1 = VnicEtherIf( |
| 420 |
parent_mo_or_dn=mo, |
| 421 |
name=str(vlan['name']), |
| 422 |
default_net=vlan['native'], |
| 423 |
) |
| 424 |
|
| 425 |
ucs.login_handle.add_mo(mo, True) |
| 426 |
ucs.login_handle.commit() |
| 427 |
changed = True |
| 428 |
|
| 429 |
except Exception as e: |
| 430 |
err = True |
| 431 |
ucs.result['msg'] = "setup error: %s " % str(e) |
| 432 |
|
| 433 |
ucs.result['changed'] = changed |
| 434 |
if err: |
| 435 |
module.fail_json(**ucs.result) |
| 436 |
module.exit_json(**ucs.result) |
| 437 |
|
| 438 |
|
| 439 |
if __name__ == '__main__': |
| 440 |
main() |