| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# Copyright 2019 Red Hat |
| 4 |
# GNU General Public License v3.0+ |
| 5 |
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
############################################# |
| 8 |
# WARNING # |
| 9 |
############################################# |
| 10 |
# |
| 11 |
# This file is auto generated by the resource |
| 12 |
# module builder playbook. |
| 13 |
# |
| 14 |
# Do not edit this file manually. |
| 15 |
# |
| 16 |
# Changes to this file will be over written |
| 17 |
# by the resource module builder. |
| 18 |
# |
| 19 |
# Changes should be made in the model used to |
| 20 |
# generate this file or in the resource module |
| 21 |
# builder template. |
| 22 |
# |
| 23 |
############################################# |
| 24 |
|
| 25 |
""" |
| 26 |
The module file for nxos_vlans |
| 27 |
""" |
| 28 |
|
| 29 |
from __future__ import absolute_import, division, print_function |
| 30 |
|
| 31 |
|
| 32 |
__metaclass__ = type |
| 33 |
|
| 34 |
|
| 35 |
DOCUMENTATION = """ |
| 36 |
module: nxos_vlans |
| 37 |
short_description: VLANs resource module |
| 38 |
description: This module creates and manages VLAN configurations on Cisco NX-OS. |
| 39 |
version_added: 1.0.0 |
| 40 |
author: Trishna Guha (@trishnaguha) |
| 41 |
notes: |
| 42 |
- Tested against NXOS 7.3.(0)D1(1) on VIRL |
| 43 |
- Unsupported for Cisco MDS |
| 44 |
options: |
| 45 |
running_config: |
| 46 |
description: |
| 47 |
- This option is used only with state I(parsed). |
| 48 |
- The value of this option should be the output received from the NX-OS device |
| 49 |
by executing the commands B(show vlans | json-pretty) and B(show running-config |
| 50 |
| section ^vlan) in order and delimited by a line. |
| 51 |
- The state I(parsed) reads the configuration from C(running_config) option and |
| 52 |
transforms it into Ansible structured data as per the resource module's argspec |
| 53 |
and the value is then returned in the I(parsed) key within the result. |
| 54 |
type: str |
| 55 |
config: |
| 56 |
description: A dictionary of Vlan options |
| 57 |
type: list |
| 58 |
elements: dict |
| 59 |
suboptions: |
| 60 |
vlan_id: |
| 61 |
description: |
| 62 |
- Vlan ID. |
| 63 |
type: int |
| 64 |
required: true |
| 65 |
name: |
| 66 |
description: |
| 67 |
- Name of VLAN. |
| 68 |
type: str |
| 69 |
state: |
| 70 |
description: |
| 71 |
- Manage operational state of the vlan. |
| 72 |
type: str |
| 73 |
choices: |
| 74 |
- active |
| 75 |
- suspend |
| 76 |
enabled: |
| 77 |
description: |
| 78 |
- Manage administrative state of the vlan. |
| 79 |
type: bool |
| 80 |
mode: |
| 81 |
description: |
| 82 |
- Set vlan mode to classical ethernet or fabricpath. This is a valid option |
| 83 |
for Nexus 5000, 6000 and 7000 series. |
| 84 |
type: str |
| 85 |
choices: |
| 86 |
- ce |
| 87 |
- fabricpath |
| 88 |
mapped_vni: |
| 89 |
description: |
| 90 |
- The Virtual Network Identifier (VNI) ID that is mapped to the VLAN. |
| 91 |
type: int |
| 92 |
state: |
| 93 |
description: |
| 94 |
- The state of the configuration after module completion. |
| 95 |
- The state I(overridden) would override the configuration of all the |
| 96 |
VLANs on the device (including VLAN 1) with the provided configuration in |
| 97 |
the task. Use caution with this state. |
| 98 |
type: str |
| 99 |
choices: |
| 100 |
- merged |
| 101 |
- replaced |
| 102 |
- overridden |
| 103 |
- deleted |
| 104 |
- gathered |
| 105 |
- rendered |
| 106 |
- parsed |
| 107 |
default: merged |
| 108 |
|
| 109 |
""" |
| 110 |
EXAMPLES = """ |
| 111 |
# Using merged |
| 112 |
|
| 113 |
# Before state: |
| 114 |
# ------------- |
| 115 |
# vlan 1 |
| 116 |
|
| 117 |
- name: Merge provided configuration with device configuration. |
| 118 |
cisco.nxos.nxos_vlans: |
| 119 |
config: |
| 120 |
- vlan_id: 5 |
| 121 |
name: test-vlan5 |
| 122 |
- vlan_id: 10 |
| 123 |
enabled: false |
| 124 |
state: merged |
| 125 |
|
| 126 |
# After state: |
| 127 |
# ------------ |
| 128 |
# vlan 5 |
| 129 |
# name test-vlan5 |
| 130 |
# state active |
| 131 |
# no shutdown |
| 132 |
# vlan 10 |
| 133 |
# state active |
| 134 |
# shutdown |
| 135 |
|
| 136 |
|
| 137 |
# Using replaced |
| 138 |
|
| 139 |
# Before state: |
| 140 |
# ------------- |
| 141 |
# vlan 1 |
| 142 |
# vlan 5 |
| 143 |
# name test-vlan5 |
| 144 |
# vlan 10 |
| 145 |
# shutdown |
| 146 |
|
| 147 |
- name: Replace device configuration of specified vlan with provided configuration. |
| 148 |
cisco.nxos.nxos_vlans: |
| 149 |
config: |
| 150 |
- vlan_id: 5 |
| 151 |
name: test-vlan |
| 152 |
enabled: false |
| 153 |
- vlan_id: 10 |
| 154 |
enabled: false |
| 155 |
state: replaced |
| 156 |
|
| 157 |
# After state: |
| 158 |
# ------------ |
| 159 |
# vlan 1 |
| 160 |
# vlan 5 |
| 161 |
# name test-vlan |
| 162 |
# state active |
| 163 |
# shutdown |
| 164 |
# vlan 10 |
| 165 |
# state active |
| 166 |
# shutdown |
| 167 |
|
| 168 |
|
| 169 |
# Using overridden |
| 170 |
|
| 171 |
# Before state: |
| 172 |
# ------------- |
| 173 |
# vlan 1 |
| 174 |
# vlan 3 |
| 175 |
# name testing |
| 176 |
# vlan 5 |
| 177 |
# name test-vlan5 |
| 178 |
# shutdown |
| 179 |
# vlan 10 |
| 180 |
# shutdown |
| 181 |
|
| 182 |
- name: Override device configuration of all vlans with provided configuration. |
| 183 |
cisco.nxos.nxos_vlans: |
| 184 |
config: |
| 185 |
- vlan_id: 5 |
| 186 |
name: test-vlan |
| 187 |
- vlan_id: 10 |
| 188 |
state: active |
| 189 |
state: overridden |
| 190 |
|
| 191 |
# After state: |
| 192 |
# ------------ |
| 193 |
# vlan 5 |
| 194 |
# name test-vlan |
| 195 |
# state active |
| 196 |
# no shutdown |
| 197 |
# vlan 10 |
| 198 |
# state active |
| 199 |
# no shutdown |
| 200 |
|
| 201 |
|
| 202 |
# Using deleted |
| 203 |
|
| 204 |
# Before state: |
| 205 |
# ------------- |
| 206 |
# vlan 1 |
| 207 |
# vlan 5 |
| 208 |
# vlan 10 |
| 209 |
|
| 210 |
- name: Delete vlans. |
| 211 |
cisco.nxos.nxos_vlans: |
| 212 |
config: |
| 213 |
- vlan_id: 5 |
| 214 |
- vlan_id: 10 |
| 215 |
state: deleted |
| 216 |
|
| 217 |
# After state: |
| 218 |
# ------------ |
| 219 |
# |
| 220 |
|
| 221 |
# Using rendered |
| 222 |
|
| 223 |
- name: Use rendered state to convert task input to device specific commands |
| 224 |
cisco.nxos.nxos_vlans: |
| 225 |
config: |
| 226 |
- vlan_id: 5 |
| 227 |
name: vlan5 |
| 228 |
mapped_vni: 100 |
| 229 |
|
| 230 |
- vlan_id: 6 |
| 231 |
name: vlan6 |
| 232 |
state: suspend |
| 233 |
state: rendered |
| 234 |
|
| 235 |
# Task Output (redacted) |
| 236 |
# ----------------------- |
| 237 |
|
| 238 |
# rendered: |
| 239 |
# - vlan 5 |
| 240 |
# - name vlan5 |
| 241 |
# - vn-segment 100 |
| 242 |
# - vlan 6 |
| 243 |
# - name vlan6 |
| 244 |
# - state suspend |
| 245 |
|
| 246 |
# Using parsed |
| 247 |
|
| 248 |
# parsed.cfg |
| 249 |
# ------------ |
| 250 |
# { |
| 251 |
# "TABLE_vlanbrief": { |
| 252 |
# "ROW_vlanbrief": [ |
| 253 |
# { |
| 254 |
# "vlanshowbr-vlanid": "1", |
| 255 |
# "vlanshowbr-vlanid-utf": "1", |
| 256 |
# "vlanshowbr-vlanname": "default", |
| 257 |
# "vlanshowbr-vlanstate": "active", |
| 258 |
# "vlanshowbr-shutstate": "noshutdown" |
| 259 |
# }, |
| 260 |
# { |
| 261 |
# "vlanshowbr-vlanid": "5", |
| 262 |
# "vlanshowbr-vlanid-utf": "5", |
| 263 |
# "vlanshowbr-vlanname": "vlan5", |
| 264 |
# "vlanshowbr-vlanstate": "suspend", |
| 265 |
# "vlanshowbr-shutstate": "noshutdown" |
| 266 |
# }, |
| 267 |
# { |
| 268 |
# "vlanshowbr-vlanid": "6", |
| 269 |
# "vlanshowbr-vlanid-utf": "6", |
| 270 |
# "vlanshowbr-vlanname": "VLAN0006", |
| 271 |
# "vlanshowbr-vlanstate": "active", |
| 272 |
# "vlanshowbr-shutstate": "noshutdown" |
| 273 |
# }, |
| 274 |
# { |
| 275 |
# "vlanshowbr-vlanid": "7", |
| 276 |
# "vlanshowbr-vlanid-utf": "7", |
| 277 |
# "vlanshowbr-vlanname": "vlan7", |
| 278 |
# "vlanshowbr-vlanstate": "active", |
| 279 |
# "vlanshowbr-shutstate": "noshutdown" |
| 280 |
# } |
| 281 |
# ] |
| 282 |
# }, |
| 283 |
# "TABLE_mtuinfo": { |
| 284 |
# "ROW_mtuinfo": [ |
| 285 |
# { |
| 286 |
# "vlanshowinfo-vlanid": "1", |
| 287 |
# "vlanshowinfo-media-type": "enet", |
| 288 |
# "vlanshowinfo-vlanmode": "ce-vlan" |
| 289 |
# }, |
| 290 |
# { |
| 291 |
# "vlanshowinfo-vlanid": "5", |
| 292 |
# "vlanshowinfo-media-type": "enet", |
| 293 |
# "vlanshowinfo-vlanmode": "ce-vlan" |
| 294 |
# }, |
| 295 |
# { |
| 296 |
# "vlanshowinfo-vlanid": "6", |
| 297 |
# "vlanshowinfo-media-type": "enet", |
| 298 |
# "vlanshowinfo-vlanmode": "ce-vlan" |
| 299 |
# }, |
| 300 |
# { |
| 301 |
# "vlanshowinfo-vlanid": "7", |
| 302 |
# "vlanshowinfo-media-type": "enet", |
| 303 |
# "vlanshowinfo-vlanmode": "ce-vlan" |
| 304 |
# } |
| 305 |
# ] |
| 306 |
# } |
| 307 |
# } |
| 308 |
# |
| 309 |
# vlan 1,5-7 |
| 310 |
# vlan 5 |
| 311 |
# state suspend |
| 312 |
# name vlan5 |
| 313 |
# vlan 7 |
| 314 |
# name vlan7 |
| 315 |
# vn-segment 100 |
| 316 |
|
| 317 |
- name: Use parsed state to convert externally supplied config to structured format |
| 318 |
cisco.nxos.nxos_vlans: |
| 319 |
running_config: "{{ lookup('file', 'parsed.cfg') }}" |
| 320 |
state: parsed |
| 321 |
|
| 322 |
# Task output (redacted) |
| 323 |
# ----------------------- |
| 324 |
|
| 325 |
# parsed: |
| 326 |
# - vlan_id: 5 |
| 327 |
# enabled: True |
| 328 |
# mode: "ce" |
| 329 |
# name: "vlan5" |
| 330 |
# state: suspend |
| 331 |
# |
| 332 |
# - vlan_id: 6 |
| 333 |
# enabled: True |
| 334 |
# mode: "ce" |
| 335 |
# state: active |
| 336 |
# |
| 337 |
# - vlan_id: 7 |
| 338 |
# enabled: True |
| 339 |
# mode: "ce" |
| 340 |
# name: "vlan7" |
| 341 |
# state: active |
| 342 |
# mapped_vni: 100 |
| 343 |
|
| 344 |
# Using gathered |
| 345 |
|
| 346 |
# Existing device config state |
| 347 |
# ------------------------------- |
| 348 |
# nxos-9k# show vlan | json |
| 349 |
# {"TABLE_vlanbrief": {"ROW_vlanbrief": [{"vlanshowbr-vlanid": "1", "vlanshowbr-vlanid-utf": "1", "vlanshowbr-vlanname": "default", "vlanshowbr-vlanstate |
| 350 |
# ": "active", "vlanshowbr-shutstate": "noshutdown"}, {"vlanshowbr-vlanid": "5", "vlanshowbr-vlanid-utf": "5", "vlanshowbr-vlanname": "vlan5", "vlanshowb |
| 351 |
# r-vlanstate": "suspend", "vlanshowbr-shutstate": "noshutdown"}, {"vlanshowbr-vlanid": "6", "vlanshowbr-vlanid-utf": "6", "vlanshowbr-vlanname": "VLAN00 |
| 352 |
# 06", "vlanshowbr-vlanstate": "active", "vlanshowbr-shutstate": "noshutdown"}, {"vlanshowbr-vlanid": "7", "vlanshowbr-vlanid-utf": "7", "vlanshowbr-vlan |
| 353 |
# name": "vlan7", "vlanshowbr-vlanstate": "active", "vlanshowbr-shutstate": "shutdown"}]}, "TABLE_mtuinfo": {"ROW_mtuinfo": [{"vlanshowinfo-vlanid": "1", |
| 354 |
# "vlanshowinfo-media-type": "enet", "vlanshowinfo-vlanmode": "ce-vlan"}, {"vlanshowinfo-vlanid": "5", "vlanshowinfo-media-type": "enet", "vlanshowinfo- |
| 355 |
# vlanmode": "ce-vlan"}, {"vlanshowinfo-vlanid": "6", "vlanshowinfo-media-type": "enet", "vlanshowinfo-vlanmode": "ce-vlan"}, {"vlanshowinfo-vlanid": "7" |
| 356 |
# , "vlanshowinfo-media-type": "enet", "vlanshowinfo-vlanmode": "ce-vlan"}]}} |
| 357 |
# |
| 358 |
# nxos-9k# show running-config | section ^vlan |
| 359 |
# vlan 1,5-7 |
| 360 |
# vlan 5 |
| 361 |
# state suspend |
| 362 |
# name vlan5 |
| 363 |
# vlan 7 |
| 364 |
# shutdown |
| 365 |
# name vlan7 |
| 366 |
# vn-segment 190 |
| 367 |
|
| 368 |
- name: Gather vlans facts from the device using nxos_vlans |
| 369 |
cisco.nxos.nxos_vlans: |
| 370 |
state: gathered |
| 371 |
|
| 372 |
# Task output (redacted) |
| 373 |
# ----------------------- |
| 374 |
# gathered: |
| 375 |
# - vlan_id: 5 |
| 376 |
# enabled: True |
| 377 |
# mode: "ce" |
| 378 |
# name: "vlan5" |
| 379 |
# state: suspend |
| 380 |
# |
| 381 |
# - vlan_id: 6 |
| 382 |
# enabled: True |
| 383 |
# mode: "ce" |
| 384 |
# state: active |
| 385 |
# |
| 386 |
# - vlan_id: 7 |
| 387 |
# enabled: False |
| 388 |
# mode: "ce" |
| 389 |
# name: "vlan7" |
| 390 |
# state: active |
| 391 |
# mapped_vni: 190 |
| 392 |
""" |
| 393 |
RETURN = """ |
| 394 |
before: |
| 395 |
description: The configuration as structured data prior to module invocation. |
| 396 |
returned: always |
| 397 |
type: list |
| 398 |
sample: > |
| 399 |
The configuration returned will always be in the same format |
| 400 |
of the parameters above. |
| 401 |
after: |
| 402 |
description: The configuration as structured data after module completion. |
| 403 |
returned: when changed |
| 404 |
type: list |
| 405 |
sample: > |
| 406 |
The configuration returned will always be in the same format |
| 407 |
of the parameters above. |
| 408 |
commands: |
| 409 |
description: The set of commands pushed to the remote device. |
| 410 |
returned: always |
| 411 |
type: list |
| 412 |
sample: ['vlan 5', 'name test-vlan5', 'state suspend'] |
| 413 |
""" |
| 414 |
|
| 415 |
|
| 416 |
from ansible.module_utils.basic import AnsibleModule |
| 417 |
|
| 418 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.argspec.vlans.vlans import ( |
| 419 |
VlansArgs, |
| 420 |
) |
| 421 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.config.vlans.vlans import ( |
| 422 |
Vlans, |
| 423 |
) |
| 424 |
|
| 425 |
|
| 426 |
def main(): |
| 427 |
""" |
| 428 |
Main entry point for module execution |
| 429 |
|
| 430 |
:returns: the result form module invocation |
| 431 |
""" |
| 432 |
module = AnsibleModule(argument_spec=VlansArgs.argument_spec, supports_check_mode=True) |
| 433 |
|
| 434 |
result = Vlans(module).execute_module() |
| 435 |
module.exit_json(**result) |
| 436 |
|
| 437 |
|
| 438 |
if __name__ == "__main__": |
| 439 |
main() |