| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2018-2025, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
''' |
| 7 |
na_ontap_net_vlan |
| 8 |
''' |
| 9 |
|
| 10 |
from __future__ import absolute_import, division, print_function |
| 11 |
|
| 12 |
__metaclass__ = type |
| 13 |
|
| 14 |
DOCUMENTATION = ''' |
| 15 |
module: na_ontap_net_vlan |
| 16 |
short_description: NetApp ONTAP network VLAN |
| 17 |
extends_documentation_fragment: |
| 18 |
- netapp.ontap.netapp.na_ontap |
| 19 |
version_added: 2.6.0 |
| 20 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 21 |
description: |
| 22 |
- Create/Modify/Delete network VLAN |
| 23 |
- Modify VLAN are supported only with REST |
| 24 |
- broadcast_domain, ipspace and enabled keys are supported with REST and is ignored with ZAPI |
| 25 |
options: |
| 26 |
state: |
| 27 |
description: |
| 28 |
- Whether the specified network VLAN should exist or not |
| 29 |
choices: ['present', 'absent'] |
| 30 |
type: str |
| 31 |
default: present |
| 32 |
parent_interface: |
| 33 |
description: |
| 34 |
- The interface that hosts the VLAN interface. |
| 35 |
required: true |
| 36 |
type: str |
| 37 |
vlanid: |
| 38 |
description: |
| 39 |
- The VLAN id. Ranges from 1 to 4094. |
| 40 |
required: true |
| 41 |
type: int |
| 42 |
node: |
| 43 |
description: |
| 44 |
- Node name of VLAN interface. |
| 45 |
required: true |
| 46 |
type: str |
| 47 |
broadcast_domain: |
| 48 |
description: |
| 49 |
- Specify the broadcast_domain name. |
| 50 |
- Only supported with REST and is ignored with ZAPI. |
| 51 |
- Required with 9.6 and 9.7, but optional with 9.8 or later. |
| 52 |
type: str |
| 53 |
version_added: 21.13.0 |
| 54 |
ipspace: |
| 55 |
description: |
| 56 |
- Specify the ipspace for the broadcast domain. |
| 57 |
- Only supported with REST and is ignored with ZAPI. |
| 58 |
- Required with 9.6 and 9.7, but optional with 9.8 or later. |
| 59 |
type: str |
| 60 |
version_added: 21.13.0 |
| 61 |
enabled: |
| 62 |
description: |
| 63 |
- Enable/Disable Net vlan. |
| 64 |
- Only supported with REST and is ignored with ZAPI. |
| 65 |
type: bool |
| 66 |
version_added: 21.13.0 |
| 67 |
notes: |
| 68 |
- The C(interface_name) option has been removed and should be deleted from playbooks |
| 69 |
''' |
| 70 |
|
| 71 |
EXAMPLES = """ |
| 72 |
- name: Create VLAN |
| 73 |
netapp.ontap.na_ontap_net_vlan: |
| 74 |
state: present |
| 75 |
vlanid: 13 |
| 76 |
node: "{{ vlan_node }}" |
| 77 |
ipspace: "{{ ipspace_name }}" |
| 78 |
username: "{{ netapp_username }}" |
| 79 |
password: "{{ netapp_password }}" |
| 80 |
hostname: "{{ netapp_hostname }}" |
| 81 |
|
| 82 |
- name: Create and add vlan to broadcast domain - REST |
| 83 |
netapp.ontap.na_ontap_net_vlan: |
| 84 |
state: present |
| 85 |
vlanid: 14 |
| 86 |
node: "{{ vlan_node }}" |
| 87 |
parent_interface: "{{ vlan_parent_interface_name }}" |
| 88 |
broadcast_domain: "{{ broadcast_domain_name }}" |
| 89 |
ipspace: "{{ ipspace_name }}" |
| 90 |
enabled: true |
| 91 |
username: "{{ netapp_username }}" |
| 92 |
password: "{{ netapp_password }}" |
| 93 |
hostname: "{{ netapp_hostname }}" |
| 94 |
|
| 95 |
- name: Disable VLAN - REST |
| 96 |
netapp.ontap.na_ontap_net_vlan: |
| 97 |
state: present |
| 98 |
vlanid: 14 |
| 99 |
node: "{{ vlan_node }}" |
| 100 |
parent_interface: "{{ vlan_parent_interface_name }}" |
| 101 |
enabled: false |
| 102 |
username: "{{ netapp_username }}" |
| 103 |
password: "{{ netapp_password }}" |
| 104 |
hostname: "{{ netapp_hostname }}" |
| 105 |
|
| 106 |
- name: Delete VLAN |
| 107 |
netapp.ontap.na_ontap_net_vlan: |
| 108 |
state: absent |
| 109 |
vlanid: 14 |
| 110 |
node: "{{ vlan_node }}" |
| 111 |
parent_interface: "{{ vlan_parent_interface_name }}" |
| 112 |
username: "{{ netapp_username }}" |
| 113 |
password: "{{ netapp_password }}" |
| 114 |
hostname: "{{ netapp_hostname }}" |
| 115 |
""" |
| 116 |
|
| 117 |
RETURN = """ |
| 118 |
|
| 119 |
""" |
| 120 |
|
| 121 |
import traceback |
| 122 |
from ansible.module_utils.basic import AnsibleModule |
| 123 |
from ansible.module_utils._text import to_native |
| 124 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 125 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 126 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 127 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 128 |
|
| 129 |
HAS_NETAPP_LIB = netapp_utils.has_netapp_lib() |
| 130 |
|
| 131 |
|
| 132 |
class NetAppOntapVlan: |
| 133 |
""" |
| 134 |
Created, and destorys Net Vlans's |
| 135 |
""" |
| 136 |
def __init__(self): |
| 137 |
""" |
| 138 |
Initializes the NetAppOntapVlan function |
| 139 |
""" |
| 140 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 141 |
self.argument_spec.update(dict( |
| 142 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 143 |
parent_interface=dict(required=True, type='str'), |
| 144 |
vlanid=dict(required=True, type='int'), |
| 145 |
node=dict(required=True, type='str'), |
| 146 |
broadcast_domain=dict(required=False, type='str'), |
| 147 |
ipspace=dict(required=False, type='str'), |
| 148 |
enabled=dict(required=False, type='bool') |
| 149 |
)) |
| 150 |
|
| 151 |
self.module = AnsibleModule( |
| 152 |
argument_spec=self.argument_spec, |
| 153 |
required_together=[['broadcast_domain', 'ipspace']], |
| 154 |
supports_check_mode=True |
| 155 |
) |
| 156 |
|
| 157 |
self.na_helper = NetAppModule() |
| 158 |
|
| 159 |
# set up state variables |
| 160 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 161 |
self.parameters['interface_name'] = "%s-%s" % (self.parameters['parent_interface'], self.parameters['vlanid']) |
| 162 |
|
| 163 |
# Set up Rest API |
| 164 |
self.rest_api = OntapRestAPI(self.module) |
| 165 |
self.use_rest = self.rest_api.is_rest() |
| 166 |
|
| 167 |
if self.use_rest and not self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 8, 0): |
| 168 |
if 'broadcast_domain' not in self.parameters and 'ipspace' not in self.parameters and self.parameters['state'] == 'present': |
| 169 |
error_msg = 'broadcast_domain and ipspace are required fields with ONTAP 9.6 and 9.7' |
| 170 |
self.module.fail_json(msg=error_msg) |
| 171 |
|
| 172 |
if not self.use_rest and ('broadcast_domain' in self.parameters or 'enabled' in self.parameters): |
| 173 |
msg = 'Using ZAPI and ignoring keys - enabled, broadcast_domain and ipspace' |
| 174 |
self.module.warn(msg) |
| 175 |
self.parameters.pop('broadcast_domain', None) |
| 176 |
self.parameters.pop('ipspace', None) |
| 177 |
self.parameters.pop('enabled', None) |
| 178 |
|
| 179 |
if not self.use_rest: |
| 180 |
if HAS_NETAPP_LIB is False: |
| 181 |
self.module.fail_json(msg="the python NetApp-Lib module is required") |
| 182 |
else: |
| 183 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module) |
| 184 |
|
| 185 |
def invoke_vlan(self, zapi): |
| 186 |
""" |
| 187 |
Invoke zapi - add/delete take the same NaElement structure |
| 188 |
""" |
| 189 |
vlan_obj = netapp_utils.zapi.NaElement(zapi) |
| 190 |
vlan_info = self.create_vlan_info() |
| 191 |
vlan_obj.add_child_elem(vlan_info) |
| 192 |
try: |
| 193 |
self.server.invoke_successfully(vlan_obj, enable_tunneling=True) |
| 194 |
except netapp_utils.zapi.NaApiError as error: |
| 195 |
if zapi == 'net-vlan-create': |
| 196 |
action = 'adding' |
| 197 |
elif zapi == 'net-vlan-delete': |
| 198 |
action = 'deleting' |
| 199 |
else: |
| 200 |
action = 'unexpected' |
| 201 |
self.module.fail_json(msg='Error %s Net Vlan %s: %s' % (action, self.parameters['interface_name'], to_native(error)), |
| 202 |
exception=traceback.format_exc()) |
| 203 |
|
| 204 |
def create_vlan(self): |
| 205 |
""" |
| 206 |
Creates a new vlan |
| 207 |
""" |
| 208 |
if self.use_rest: |
| 209 |
api = 'network/ethernet/ports' |
| 210 |
body = { |
| 211 |
'type': 'vlan', |
| 212 |
'node': {'name': self.parameters['node']}, |
| 213 |
'vlan': { |
| 214 |
'base_port': { |
| 215 |
'name': self.parameters['parent_interface'], |
| 216 |
'node': {'name': self.parameters['node']} |
| 217 |
}, |
| 218 |
'tag': self.parameters['vlanid'] |
| 219 |
} |
| 220 |
} |
| 221 |
if 'broadcast_domain' in self.parameters: |
| 222 |
body['broadcast_domain'] = {'name': self.parameters['broadcast_domain']} |
| 223 |
body['broadcast_domain']['ipspace'] = {'name': self.parameters['ipspace']} |
| 224 |
if 'enabled' in self.parameters: |
| 225 |
body['enabled'] = self.parameters['enabled'] |
| 226 |
dummy, error = rest_generic.post_async(self.rest_api, api, body) |
| 227 |
if error: |
| 228 |
self.module.fail_json(msg=error) |
| 229 |
else: |
| 230 |
self.invoke_vlan('net-vlan-create') |
| 231 |
|
| 232 |
def delete_vlan(self, current=None): |
| 233 |
""" |
| 234 |
Deletes a vland |
| 235 |
""" |
| 236 |
if self.use_rest: |
| 237 |
uuid = current['uuid'] |
| 238 |
api = 'network/ethernet/ports' |
| 239 |
dummy, error = rest_generic.delete_async(self.rest_api, api, uuid) |
| 240 |
if error: |
| 241 |
self.module.fail_json(msg=error) |
| 242 |
else: |
| 243 |
self.invoke_vlan('net-vlan-delete') |
| 244 |
|
| 245 |
def get_vlan(self): |
| 246 |
""" |
| 247 |
Checks to see if a vlan already exists or not |
| 248 |
:return: Returns dictionary of attributes if the vlan exists, None if it dosn't |
| 249 |
""" |
| 250 |
if self.use_rest: |
| 251 |
return self.get_vlan_rest() |
| 252 |
vlan_obj = netapp_utils.zapi.NaElement("net-vlan-get-iter") |
| 253 |
query = { |
| 254 |
'query': { |
| 255 |
'vlan-info': { |
| 256 |
'interface-name': self.parameters['interface_name'], |
| 257 |
'node': self.parameters['node'] |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
vlan_obj.translate_struct(query) |
| 262 |
try: |
| 263 |
result = self.server.invoke_successfully(vlan_obj, True) |
| 264 |
except netapp_utils.zapi.NaApiError as error: |
| 265 |
self.module.fail_json(msg=to_native(error), exception=traceback.format_exc()) |
| 266 |
# This checks desired vlan already exists and returns interface_name and node |
| 267 |
if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) == 1: |
| 268 |
vlan_info = result.get_child_by_name('attributes-list').get_child_by_name('vlan-info') |
| 269 |
current = { |
| 270 |
'interface_name': vlan_info.get_child_content('interface-name'), |
| 271 |
'node': vlan_info.get_child_content('node') |
| 272 |
} |
| 273 |
return current |
| 274 |
return None |
| 275 |
|
| 276 |
def get_vlan_rest(self): |
| 277 |
api = 'network/ethernet/ports' |
| 278 |
query = { |
| 279 |
'name': self.parameters['interface_name'], |
| 280 |
'node.name': self.parameters['node'], |
| 281 |
} |
| 282 |
fields = 'name,node,uuid,broadcast_domain,enabled' |
| 283 |
record, error = rest_generic.get_one_record(self.rest_api, api, query, fields) |
| 284 |
if error: |
| 285 |
self.module.fail_json(msg=error) |
| 286 |
if record: |
| 287 |
current = { |
| 288 |
'interface_name': record['name'], |
| 289 |
'node': record['node']['name'], |
| 290 |
'uuid': record['uuid'], |
| 291 |
'enabled': record['enabled'], |
| 292 |
'broadcast_domain': self.na_helper.safe_get(record, ['broadcast_domain', 'name']), |
| 293 |
'ipspace': self.na_helper.safe_get(record, ['broadcast_domain', 'ipspace', 'name']) |
| 294 |
} |
| 295 |
return current |
| 296 |
return None |
| 297 |
|
| 298 |
def modify_vlan(self, current, modify): |
| 299 |
""" |
| 300 |
Modify broadcast domain, ipspace and enable/disable vlan |
| 301 |
""" |
| 302 |
uuid = current['uuid'] |
| 303 |
api = 'network/ethernet/ports' |
| 304 |
body = {} |
| 305 |
# Requires both broadcast_domain and ipspace in body |
| 306 |
# of PATCH call if any one of it present in modify |
| 307 |
if 'broadcast_domain' in modify or 'ipspace' in modify: |
| 308 |
broadcast_domain = modify['broadcast_domain'] if 'broadcast_domain' in modify else current['broadcast_domain'] |
| 309 |
ipspace = modify['ipspace'] if 'ipspace' in modify else current['ipspace'] |
| 310 |
body['broadcast_domain'] = {'name': broadcast_domain} |
| 311 |
body['broadcast_domain']['ipspace'] = {'name': ipspace} |
| 312 |
if 'enabled' in modify: |
| 313 |
body['enabled'] = modify['enabled'] |
| 314 |
dummy, error = rest_generic.patch_async(self.rest_api, api, uuid, body) |
| 315 |
if error: |
| 316 |
self.module.fail_json(msg=error) |
| 317 |
|
| 318 |
def create_vlan_info(self): |
| 319 |
""" |
| 320 |
Create a vlan_info object to be used in a create/delete |
| 321 |
:return: |
| 322 |
""" |
| 323 |
vlan_info = netapp_utils.zapi.NaElement("vlan-info") |
| 324 |
|
| 325 |
# set up the vlan_info object: |
| 326 |
vlan_info.add_new_child("parent-interface", self.parameters['parent_interface']) |
| 327 |
vlan_info.add_new_child("vlanid", str(self.parameters['vlanid'])) |
| 328 |
vlan_info.add_new_child("node", self.parameters['node']) |
| 329 |
return vlan_info |
| 330 |
|
| 331 |
def apply(self): |
| 332 |
""" |
| 333 |
check the option in the playbook to see what needs to be done |
| 334 |
:return: |
| 335 |
""" |
| 336 |
modify = None |
| 337 |
current = self.get_vlan() |
| 338 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 339 |
if self.use_rest and cd_action is None: |
| 340 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 341 |
if self.na_helper.changed and not self.module.check_mode: |
| 342 |
if cd_action == 'create': |
| 343 |
self.create_vlan() |
| 344 |
# enabled key in POST call has no effect |
| 345 |
# applying PATCH if there is change in default value |
| 346 |
if self.use_rest: |
| 347 |
current = self.get_vlan_rest() |
| 348 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 349 |
if cd_action == 'delete': |
| 350 |
self.delete_vlan(current) |
| 351 |
if modify: |
| 352 |
self.modify_vlan(current, modify) |
| 353 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify) |
| 354 |
self.module.exit_json(**result) |
| 355 |
|
| 356 |
|
| 357 |
def main(): |
| 358 |
""" |
| 359 |
Creates the NetApp Ontap vlan object, and runs the correct play task. |
| 360 |
""" |
| 361 |
vlan_obj = NetAppOntapVlan() |
| 362 |
vlan_obj.apply() |
| 363 |
|
| 364 |
|
| 365 |
if __name__ == '__main__': |
| 366 |
main() |