| 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_vlans |
| 16 |
short_description: Configures VLANs on Cisco UCS Manager |
| 17 |
description: |
| 18 |
- Configures VLANs on Cisco UCS Manager. |
| 19 |
extends_documentation_fragment: cisco.ucs.ucs |
| 20 |
options: |
| 21 |
state: |
| 22 |
description: |
| 23 |
- If C(present), will verify VLANs are present and will create if needed. |
| 24 |
- If C(absent), will verify VLANs are absent and will delete if needed. |
| 25 |
choices: [present, absent] |
| 26 |
default: present |
| 27 |
type: str |
| 28 |
name: |
| 29 |
description: |
| 30 |
- The name assigned to the VLAN. |
| 31 |
- The VLAN name is case sensitive. |
| 32 |
- This name can be between 1 and 32 alphanumeric characters. |
| 33 |
- "You cannot use spaces or any special characters other than - (hyphen), \"_\" (underscore), : (colon), and . (period)." |
| 34 |
- You cannot change this name after the VLAN is created. |
| 35 |
required: yes |
| 36 |
type: str |
| 37 |
multicast_policy: |
| 38 |
description: |
| 39 |
- The multicast policy associated with this VLAN. |
| 40 |
- This option is only valid if the Sharing Type field is set to None or Primary. |
| 41 |
type: str |
| 42 |
fabric: |
| 43 |
description: |
| 44 |
- "The fabric configuration of the VLAN. This can be one of the following:" |
| 45 |
- "common - The VLAN applies to both fabrics and uses the same configuration parameters in both cases." |
| 46 |
- "A — The VLAN only applies to fabric A." |
| 47 |
- "B — The VLAN only applies to fabric B." |
| 48 |
- For upstream disjoint L2 networks, Cisco recommends that you choose common to create VLANs that apply to both fabrics. |
| 49 |
choices: [common, A, B] |
| 50 |
default: common |
| 51 |
type: str |
| 52 |
id: |
| 53 |
description: |
| 54 |
- The unique string identifier assigned to the VLAN. |
| 55 |
- A VLAN ID can be between '1' and '3967', or between '4048' and '4093'. |
| 56 |
- You cannot create VLANs with IDs from 4030 to 4047. This range of VLAN IDs is reserved. |
| 57 |
- The VLAN IDs you specify must also be supported on the switch that you are using. |
| 58 |
- VLANs in the LAN cloud and FCoE VLANs in the SAN cloud must have different IDs. |
| 59 |
- Optional if state is absent. |
| 60 |
type: str |
| 61 |
sharing: |
| 62 |
description: |
| 63 |
- The Sharing Type field. |
| 64 |
- "Whether this VLAN is subdivided into private or secondary VLANs. This can be one of the following:" |
| 65 |
- "none - This VLAN does not have any secondary or private VLANs. This is a regular VLAN." |
| 66 |
- "primary - This VLAN can have one or more secondary VLANs, as shown in the Secondary VLANs area. This VLAN is a primary VLAN in the private VLAN domain." |
| 67 |
- "isolated - This is a private VLAN associated with a primary VLAN. This VLAN is an Isolated VLAN." |
| 68 |
- "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." |
| 69 |
choices: [none, primary, isolated, community] |
| 70 |
default: none |
| 71 |
type: str |
| 72 |
native: |
| 73 |
description: |
| 74 |
- Designates the VLAN as a native VLAN. |
| 75 |
choices: ['yes', 'no'] |
| 76 |
default: 'no' |
| 77 |
type: str |
| 78 |
requirements: |
| 79 |
- ucsmsdk |
| 80 |
author: |
| 81 |
- David Soper (@dsoper2) |
| 82 |
- CiscoUcs (@CiscoUcs) |
| 83 |
''' |
| 84 |
|
| 85 |
EXAMPLES = r''' |
| 86 |
- name: Configure VLAN |
| 87 |
cisco.ucs.ucs_vlans: |
| 88 |
hostname: 172.16.143.150 |
| 89 |
username: admin |
| 90 |
password: password |
| 91 |
name: vlan2 |
| 92 |
id: '2' |
| 93 |
native: 'yes' |
| 94 |
|
| 95 |
- name: Remove VLAN |
| 96 |
cisco.ucs.ucs_vlans: |
| 97 |
hostname: 172.16.143.150 |
| 98 |
username: admin |
| 99 |
password: password |
| 100 |
name: vlan2 |
| 101 |
state: absent |
| 102 |
''' |
| 103 |
|
| 104 |
RETURN = r''' |
| 105 |
# |
| 106 |
''' |
| 107 |
|
| 108 |
from ansible.module_utils.basic import AnsibleModule |
| 109 |
from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec |
| 110 |
|
| 111 |
|
| 112 |
def main(): |
| 113 |
argument_spec = ucs_argument_spec.copy() |
| 114 |
argument_spec.update( |
| 115 |
name=dict(type='str', required=True), |
| 116 |
multicast_policy=dict(type='str'), |
| 117 |
fabric=dict(type='str', default='common', choices=['common', 'A', 'B']), |
| 118 |
id=dict(type='str'), |
| 119 |
sharing=dict(type='str', default='none', choices=['none', 'primary', 'isolated', 'community']), |
| 120 |
native=dict(type='str', default='no', choices=['yes', 'no']), |
| 121 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 122 |
) |
| 123 |
|
| 124 |
module = AnsibleModule( |
| 125 |
argument_spec, |
| 126 |
supports_check_mode=True, |
| 127 |
required_if=[ |
| 128 |
['state', 'present', ['id']], |
| 129 |
], |
| 130 |
) |
| 131 |
ucs = UCSModule(module) |
| 132 |
|
| 133 |
err = False |
| 134 |
|
| 135 |
# UCSModule creation above verifies ucsmsdk is present and exits on failure, so additional imports are done below. |
| 136 |
from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan |
| 137 |
|
| 138 |
changed = False |
| 139 |
try: |
| 140 |
mo_exists = False |
| 141 |
props_match = False |
| 142 |
# dn is fabric/lan/net-<name> for common vlans or fabric/lan/[A or B]/net-<name> for A or B |
| 143 |
dn_base = 'fabric/lan' |
| 144 |
if module.params['fabric'] != 'common': |
| 145 |
dn_base += '/' + module.params['fabric'] |
| 146 |
dn = dn_base + '/net-' + module.params['name'] |
| 147 |
|
| 148 |
mo = ucs.login_handle.query_dn(dn) |
| 149 |
if mo: |
| 150 |
mo_exists = True |
| 151 |
|
| 152 |
if module.params['state'] == 'absent': |
| 153 |
# mo must exist but all properties do not have to match |
| 154 |
if mo_exists: |
| 155 |
if not module.check_mode: |
| 156 |
ucs.login_handle.remove_mo(mo) |
| 157 |
ucs.login_handle.commit() |
| 158 |
changed = True |
| 159 |
else: |
| 160 |
if mo_exists: |
| 161 |
# check top-level mo props |
| 162 |
kwargs = dict(id=module.params['id']) |
| 163 |
kwargs['default_net'] = module.params['native'] |
| 164 |
kwargs['sharing'] = module.params['sharing'] |
| 165 |
kwargs['mcast_policy_name'] = module.params['multicast_policy'] |
| 166 |
if mo.check_prop_match(**kwargs): |
| 167 |
props_match = True |
| 168 |
|
| 169 |
if not props_match: |
| 170 |
if not module.check_mode: |
| 171 |
# create if mo does not already exist |
| 172 |
mo = FabricVlan( |
| 173 |
parent_mo_or_dn=dn_base, |
| 174 |
name=module.params['name'], |
| 175 |
id=module.params['id'], |
| 176 |
default_net=module.params['native'], |
| 177 |
sharing=module.params['sharing'], |
| 178 |
mcast_policy_name=module.params['multicast_policy'], |
| 179 |
) |
| 180 |
|
| 181 |
ucs.login_handle.add_mo(mo, True) |
| 182 |
ucs.login_handle.commit() |
| 183 |
changed = True |
| 184 |
|
| 185 |
except Exception as e: |
| 186 |
err = True |
| 187 |
ucs.result['msg'] = "setup error: %s " % str(e) |
| 188 |
|
| 189 |
ucs.result['changed'] = changed |
| 190 |
if err: |
| 191 |
module.fail_json(**ucs.result) |
| 192 |
module.exit_json(**ucs.result) |
| 193 |
|
| 194 |
|
| 195 |
if __name__ == '__main__': |
| 196 |
main() |