| 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: ucs_vlan_to_group |
| 16 |
short_description: Add VLANs to a VLAN Group. Requires VLAN and VLAN Group to already be created on UCS prior to running module. |
| 17 |
description: |
| 18 |
- Add VLANs to VLAN Groups on Cisco UCS Manager. |
| 19 |
extends_documentation_fragment: 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 |
vlanname: |
| 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 |
required: yes |
| 35 |
type: str |
| 36 |
vlangroup: |
| 37 |
description: |
| 38 |
- The name assigned to the VLAN Group. |
| 39 |
- The VLAN Group name is case sensitive. |
| 40 |
- This name can be between 1 and 32 alphanumeric characters. |
| 41 |
- "You cannot use spaces or any special characters other than - (hyphen), \"_\" (underscore), : (colon), and . (period)." |
| 42 |
required: yes |
| 43 |
type: str |
| 44 |
requirements: |
| 45 |
- ucsmsdk |
| 46 |
author: |
| 47 |
- Derrick Johnson (@derricktj) |
| 48 |
''' |
| 49 |
|
| 50 |
EXAMPLES = r''' |
| 51 |
- name: Configure VLAN |
| 52 |
cisco.ucs.ucs_vlan_to_group: |
| 53 |
hostname: 1.1.1.1 |
| 54 |
username: admin |
| 55 |
password: password |
| 56 |
vlangroup: VLANGROUP |
| 57 |
vlanname: VLANNAME |
| 58 |
state: present |
| 59 |
- name: Remove VLAN |
| 60 |
cisco.ucs.ucs_vlan_to_group: |
| 61 |
hostname: 1.1.1.1 |
| 62 |
username: admin |
| 63 |
password: password |
| 64 |
vlangroup: VLANGROUP |
| 65 |
vlanname: VLANNAME |
| 66 |
state: absent |
| 67 |
''' |
| 68 |
|
| 69 |
RETURN = r''' |
| 70 |
# |
| 71 |
''' |
| 72 |
|
| 73 |
from ansible.module_utils.basic import AnsibleModule |
| 74 |
from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec |
| 75 |
|
| 76 |
|
| 77 |
def main(): |
| 78 |
argument_spec = ucs_argument_spec.copy() |
| 79 |
argument_spec.update( |
| 80 |
vlangroup=dict(type='str', required=True), |
| 81 |
vlanname=dict(type='str', required=True), |
| 82 |
state=dict(default='present', choices=['present', 'absent'], type='str'), |
| 83 |
) |
| 84 |
|
| 85 |
module = AnsibleModule( |
| 86 |
argument_spec, |
| 87 |
supports_check_mode=True, |
| 88 |
required_if=[ |
| 89 |
['state', 'present', ['vlangroup', 'vlanname']], |
| 90 |
], |
| 91 |
) |
| 92 |
ucs = UCSModule(module) |
| 93 |
|
| 94 |
err = False |
| 95 |
|
| 96 |
from ucsmsdk.mometa.fabric.FabricNetGroup import FabricNetGroup |
| 97 |
from ucsmsdk.mometa.fabric.FabricPooledVlan import FabricPooledVlan |
| 98 |
|
| 99 |
changed = False |
| 100 |
try: |
| 101 |
dnpooled1_exists = False |
| 102 |
|
| 103 |
# dn = fabric/lan/net-group-VLANGROUP |
| 104 |
# Check for VLAN Group |
| 105 |
dngroup = 'fabric/lan/net-group-' + module.params['vlangroup'] |
| 106 |
dngroup1 = ucs.login_handle.query_dn(dngroup) |
| 107 |
|
| 108 |
# dn = fabric/lan/net-VLANNAME |
| 109 |
# Check for VLAN |
| 110 |
dnvlan = 'fabric/lan/net-' + module.params['vlanname'] |
| 111 |
dnvlan1 = ucs.login_handle.query_dn(dnvlan) |
| 112 |
|
| 113 |
# dn = fabric/lan/net-group-VLANGROUP/net-VLANNAME |
| 114 |
# Check for VLAN within VLAN Group |
| 115 |
dnpooled = 'fabric/lan/net-group-' + module.params['vlangroup'] + '/net-' + module.params['vlanname'] |
| 116 |
dnpooled1 = ucs.login_handle.query_dn(dnpooled) |
| 117 |
|
| 118 |
# Configuration MOs. Couldn't really get this to work off the DNs, so I built additional objects. |
| 119 |
mo = FabricNetGroup(parent_mo_or_dn="fabric/lan", name=module.params['vlangroup']) |
| 120 |
mo_1 = FabricPooledVlan(parent_mo_or_dn=mo, name=module.params['vlanname']) |
| 121 |
|
| 122 |
if not dngroup1: |
| 123 |
# Error out if the VLAN Group is missing |
| 124 |
err = True |
| 125 |
ucs.result['msg'] = module.params['vlangroup'] + " VLAN Group not configured in UCS" |
| 126 |
|
| 127 |
if not dnvlan1: |
| 128 |
# Error out if VLAN is missing |
| 129 |
err = True |
| 130 |
ucs.result['msg'] = module.params['vlanname'] + " VLAN not configured in UCS. Use ucs_vlans module to create the VLAN first" |
| 131 |
|
| 132 |
if dnpooled1: |
| 133 |
dnpooled1_exists = True |
| 134 |
|
| 135 |
if module.params['state'] == 'absent': |
| 136 |
if dnpooled1_exists: |
| 137 |
if not module.check_mode: |
| 138 |
ucs.login_handle.remove_mo(mo_1) |
| 139 |
ucs.login_handle.commit() |
| 140 |
changed = True |
| 141 |
|
| 142 |
if module.params['state'] == 'present': |
| 143 |
if not dnpooled1_exists: |
| 144 |
if not module.check_mode: |
| 145 |
ucs.login_handle.add_mo(mo, True) |
| 146 |
ucs.login_handle.commit() |
| 147 |
changed = True |
| 148 |
|
| 149 |
except Exception as e: |
| 150 |
err = True |
| 151 |
ucs.result['msg'] = "setup error: %s " % str(e) |
| 152 |
|
| 153 |
ucs.result['changed'] = changed |
| 154 |
if err: |
| 155 |
module.fail_json(**ucs.result) |
| 156 |
module.exit_json(**ucs.result) |
| 157 |
|
| 158 |
|
| 159 |
if __name__ == '__main__': |
| 160 |
main() |