Debian Codegraph @

ansible_collections/cisco/ucs/plugins/modules/ucs_vsans.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_vsans
16 short_description: Configures VSANs on Cisco UCS Manager
17 description:
18 - Configures VSANs on Cisco UCS Manager.
19 extends_documentation_fragment: cisco.ucs.ucs
20 options:
21 state:
22 description:
23 - If C(present), will verify VSANs are present and will create if needed.
24 - If C(absent), will verify VSANs 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 VSAN.
31 - This name can be between 1 and 32 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 VSAN is created.
34 required: yes
35 type: str
36 vsan_id:
37 description:
38 - The unique identifier assigned to the VSAN.
39 - The ID can be a string between '1' and '4078', or between '4080' and '4093'. '4079' is a reserved VSAN ID.
40 - In addition, if you plan to use FC end-host mode, the range between '3840' to '4079' is also a reserved VSAN ID range.
41 - Optional if state is absent.
42 type: str
43 vlan_id:
44 description:
45 - The unique string identifier assigned to the VLAN used for Fibre Channel connections.
46 - Note that Cisco UCS Manager uses VLAN '4048'. See the UCS Manager configuration guide if you want to assign '4048' to a VLAN.
47 - Optional if state is absent.
48 type: str
49 fc_zoning:
50 description:
51 - Fibre Channel zoning configuration for the Cisco UCS domain.
52 - "Fibre Channel zoning can be set to one of the following values:"
53 - "disabled — The upstream switch handles Fibre Channel zoning, or Fibre Channel zoning is not implemented for the Cisco UCS domain."
54 - "enabled — Cisco UCS Manager configures and controls Fibre Channel zoning for the Cisco UCS domain."
55 - If you enable Fibre Channel zoning, do not configure the upstream switch with any VSANs that are being used for Fibre Channel zoning.
56 choices: [disabled, enabled]
57 default: disabled
58 type: str
59 fabric:
60 description:
61 - "The fabric configuration of the VSAN. This can be one of the following:"
62 - "common - The VSAN maps to the same VSAN ID in all available fabrics."
63 - "A - The VSAN maps to the a VSAN ID that exists only in fabric A."
64 - "B - The VSAN maps to the a VSAN ID that exists only in fabric B."
65 choices: [common, A, B]
66 default: common
67 type: str
68 requirements:
69 - ucsmsdk
70 author:
71 - David Soper (@dsoper2)
72 - John McDonough (@movinalot)
73 - CiscoUcs (@CiscoUcs)
74 '''
75
76 EXAMPLES = r'''
77 - name: Configure VSAN
78 cisco.ucs.ucs_vsans:
79 hostname: 172.16.143.150
80 username: admin
81 password: password
82 name: vsan110
83 fabric: common
84 vsan_id: '110'
85 vlan_id: '110'
86
87 - name: Remove VSAN
88 cisco.ucs.ucs_vsans:
89 hostname: 172.16.143.150
90 username: admin
91 password: password
92 name: vsan110
93 state: absent
94 '''
95
96 RETURN = r'''
97 #
98 '''
99
100 from ansible.module_utils.basic import AnsibleModule
101 from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec
102
103
104 def main():
105 argument_spec = ucs_argument_spec.copy()
106 argument_spec.update(
107 name=dict(type='str', required=True),
108 vsan_id=dict(type='str'),
109 vlan_id=dict(type='str'),
110 fc_zoning=dict(type='str', default='disabled', choices=['disabled', 'enabled']),
111 fabric=dict(type='str', default='common', choices=['common', 'A', 'B']),
112 state=dict(type='str', default='present', choices=['present', 'absent']),
113 )
114
115 module = AnsibleModule(
116 argument_spec,
117 supports_check_mode=True,
118 )
119 ucs = UCSModule(module)
120
121 err = False
122
123 from ucsmsdk.mometa.fabric.FabricVsan import FabricVsan
124
125 changed = False
126 try:
127 # single resource specified, create list from the current params
128 vsan_list = [module.params]
129 for vsan in vsan_list:
130 mo_exists = False
131 props_match = False
132 # set default params. Done here to set values for lists which can't be done in the argument_spec
133 if not vsan.get('fc_zoning'):
134 vsan['fc_zoning'] = 'disabled'
135 if not vsan.get('fabric'):
136 vsan['fabric'] = 'common'
137 # dn is fabric/san/net-<name> for common vsans or fabric/san/[A or B]/net-<name> for A or B
138 dn_base = 'fabric/san'
139 if vsan['fabric'] != 'common':
140 dn_base += '/' + vsan['fabric']
141 dn = dn_base + '/net-' + vsan['name']
142
143 mo = ucs.login_handle.query_dn(dn)
144 if mo:
145 mo_exists = True
146
147 if module.params['state'] == 'absent':
148 # mo must exist but all properties do not have to match
149 if mo_exists:
150 if not module.check_mode:
151 ucs.login_handle.remove_mo(mo)
152 ucs.login_handle.commit()
153 changed = True
154 else:
155 if mo_exists:
156 # check top-level mo props
157 kwargs = dict(id=vsan['vsan_id'])
158 kwargs['fcoe_vlan'] = vsan['vlan_id']
159 kwargs['zoning_state'] = vsan['fc_zoning']
160 if (mo.check_prop_match(**kwargs)):
161 props_match = True
162
163 if not props_match:
164 if not module.check_mode:
165 # create if mo does not already exist
166 mo = FabricVsan(
167 parent_mo_or_dn=dn_base,
168 name=vsan['name'],
169 id=vsan['vsan_id'],
170 fcoe_vlan=vsan['vlan_id'],
171 zoning_state=vsan['fc_zoning'],
172 )
173
174 ucs.login_handle.add_mo(mo, True)
175 ucs.login_handle.commit()
176 changed = True
177
178 except Exception as e:
179 err = True
180 ucs.result['msg'] = "setup error: %s " % str(e)
181
182 ucs.result['changed'] = changed
183 if err:
184 module.fail_json(**ucs.result)
185 module.exit_json(**ucs.result)
186
187
188 if __name__ == '__main__':
189 main()