| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
# |
| 18 |
from __future__ import absolute_import, division, print_function |
| 19 |
|
| 20 |
|
| 21 |
__metaclass__ = type |
| 22 |
|
| 23 |
|
| 24 |
DOCUMENTATION = """ |
| 25 |
module: nxos_vpc_interface |
| 26 |
extends_documentation_fragment: |
| 27 |
- cisco.nxos.nxos |
| 28 |
short_description: Manages interface VPC configuration |
| 29 |
description: |
| 30 |
- Manages interface VPC configuration |
| 31 |
version_added: 1.0.0 |
| 32 |
author: |
| 33 |
- Jason Edelman (@jedelman8) |
| 34 |
- Gabriele Gerbino (@GGabriele) |
| 35 |
notes: |
| 36 |
- Tested against NXOSv 7.3.(0)D1(1) on VIRL |
| 37 |
- Unsupported for Cisco MDS |
| 38 |
- Either vpc or peer_link param is required, but not both. |
| 39 |
- C(state=absent) removes whatever VPC config is on a port-channel if one exists. |
| 40 |
- Re-assigning a vpc or peerlink from one portchannel to another is not supported. The |
| 41 |
module will force the user to unconfigure an existing vpc/pl before configuring |
| 42 |
the same value on a new portchannel |
| 43 |
options: |
| 44 |
portchannel: |
| 45 |
description: |
| 46 |
- Group number of the portchannel that will be configured. |
| 47 |
required: true |
| 48 |
type: str |
| 49 |
vpc: |
| 50 |
description: |
| 51 |
- VPC group/id that will be configured on associated portchannel. |
| 52 |
type: str |
| 53 |
peer_link: |
| 54 |
description: |
| 55 |
- Set to true/false for peer link config on associated portchannel. |
| 56 |
type: bool |
| 57 |
state: |
| 58 |
description: |
| 59 |
- Manages desired state of the resource. |
| 60 |
choices: |
| 61 |
- present |
| 62 |
- absent |
| 63 |
default: present |
| 64 |
type: str |
| 65 |
""" |
| 66 |
|
| 67 |
EXAMPLES = """ |
| 68 |
- cisco.nxos.nxos_vpc_interface: |
| 69 |
portchannel: 10 |
| 70 |
vpc: 100 |
| 71 |
""" |
| 72 |
|
| 73 |
RETURN = """ |
| 74 |
commands: |
| 75 |
description: commands sent to the device |
| 76 |
returned: always |
| 77 |
type: list |
| 78 |
sample: ["interface port-channel100", "vpc 10"] |
| 79 |
""" |
| 80 |
|
| 81 |
from ansible.module_utils.basic import AnsibleModule |
| 82 |
|
| 83 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import ( |
| 84 |
get_config, |
| 85 |
load_config, |
| 86 |
run_commands, |
| 87 |
) |
| 88 |
|
| 89 |
|
| 90 |
def flatten_list(command_lists): |
| 91 |
flat_command_list = [] |
| 92 |
for command in command_lists: |
| 93 |
if isinstance(command, list): |
| 94 |
flat_command_list.extend(command) |
| 95 |
else: |
| 96 |
flat_command_list.append(command) |
| 97 |
return flat_command_list |
| 98 |
|
| 99 |
|
| 100 |
def get_portchannel_list(module): |
| 101 |
portchannels = [] |
| 102 |
pc_list = [] |
| 103 |
|
| 104 |
try: |
| 105 |
body = run_commands(module, ["show port-channel summary | json"])[0] |
| 106 |
pc_list = body["TABLE_channel"]["ROW_channel"] |
| 107 |
except (KeyError, AttributeError, TypeError): |
| 108 |
return portchannels |
| 109 |
|
| 110 |
if pc_list: |
| 111 |
if isinstance(pc_list, dict): |
| 112 |
pc_list = [pc_list] |
| 113 |
|
| 114 |
for pc in pc_list: |
| 115 |
portchannels.append(pc["group"]) |
| 116 |
|
| 117 |
return portchannels |
| 118 |
|
| 119 |
|
| 120 |
def get_existing_portchannel_to_vpc_mappings(module): |
| 121 |
pc_vpc_mapping = {} |
| 122 |
|
| 123 |
try: |
| 124 |
body = run_commands(module, ["show vpc brief | json"])[0] |
| 125 |
vpc_table = body["TABLE_vpc"]["ROW_vpc"] |
| 126 |
except (KeyError, AttributeError, TypeError): |
| 127 |
vpc_table = None |
| 128 |
|
| 129 |
if vpc_table: |
| 130 |
if isinstance(vpc_table, dict): |
| 131 |
vpc_table = [vpc_table] |
| 132 |
|
| 133 |
for vpc in vpc_table: |
| 134 |
pc_vpc_mapping[str(vpc["vpc-id"])] = str(vpc["vpc-ifindex"]) |
| 135 |
|
| 136 |
return pc_vpc_mapping |
| 137 |
|
| 138 |
|
| 139 |
def peer_link_exists(module): |
| 140 |
found = False |
| 141 |
run = get_config(module, flags=["vpc"]) |
| 142 |
|
| 143 |
vpc_list = run.split("\n") |
| 144 |
for each in vpc_list: |
| 145 |
if "peer-link" in each: |
| 146 |
found = True |
| 147 |
return found |
| 148 |
|
| 149 |
|
| 150 |
def get_active_vpc_peer_link(module): |
| 151 |
peer_link = None |
| 152 |
|
| 153 |
try: |
| 154 |
body = run_commands(module, ["show vpc brief | json"])[0] |
| 155 |
peer_link = body["TABLE_peerlink"]["ROW_peerlink"]["peerlink-ifindex"] |
| 156 |
except (KeyError, AttributeError, TypeError): |
| 157 |
return peer_link |
| 158 |
|
| 159 |
return peer_link |
| 160 |
|
| 161 |
|
| 162 |
def get_portchannel_vpc_config(module, portchannel): |
| 163 |
peer_link_pc = None |
| 164 |
peer_link = False |
| 165 |
vpc = "" |
| 166 |
pc = "" |
| 167 |
config = {} |
| 168 |
|
| 169 |
try: |
| 170 |
body = run_commands(module, ["show vpc brief | json"])[0] |
| 171 |
table = body["TABLE_peerlink"]["ROW_peerlink"] |
| 172 |
except (KeyError, AttributeError, TypeError): |
| 173 |
table = {} |
| 174 |
|
| 175 |
if table: |
| 176 |
peer_link_pc = table.get("peerlink-ifindex", None) |
| 177 |
|
| 178 |
if peer_link_pc: |
| 179 |
plpc = str(peer_link_pc[2:]) |
| 180 |
if portchannel == plpc: |
| 181 |
config["portchannel"] = portchannel |
| 182 |
config["peer-link"] = True |
| 183 |
config["vpc"] = vpc |
| 184 |
|
| 185 |
mapping = get_existing_portchannel_to_vpc_mappings(module) |
| 186 |
|
| 187 |
for existing_vpc, port_channel in mapping.items(): |
| 188 |
port_ch = str(port_channel[2:]) |
| 189 |
if port_ch == portchannel: |
| 190 |
pc = port_ch |
| 191 |
vpc = str(existing_vpc) |
| 192 |
|
| 193 |
config["portchannel"] = pc |
| 194 |
config["peer-link"] = peer_link |
| 195 |
config["vpc"] = vpc |
| 196 |
|
| 197 |
return config |
| 198 |
|
| 199 |
|
| 200 |
def get_commands_to_config_vpc_interface(portchannel, delta, config_value, existing): |
| 201 |
commands = [] |
| 202 |
|
| 203 |
if not delta.get("peer-link") and existing.get("peer-link"): |
| 204 |
commands.append("no vpc peer-link") |
| 205 |
commands.insert(0, "interface port-channel{0}".format(portchannel)) |
| 206 |
|
| 207 |
elif delta.get("peer-link") and not existing.get("peer-link"): |
| 208 |
commands.append("vpc peer-link") |
| 209 |
commands.insert(0, "interface port-channel{0}".format(portchannel)) |
| 210 |
|
| 211 |
elif delta.get("vpc") and not existing.get("vpc"): |
| 212 |
command = "vpc {0}".format(config_value) |
| 213 |
commands.append(command) |
| 214 |
commands.insert(0, "interface port-channel{0}".format(portchannel)) |
| 215 |
|
| 216 |
return commands |
| 217 |
|
| 218 |
|
| 219 |
def state_present(portchannel, delta, config_value, existing): |
| 220 |
commands = [] |
| 221 |
|
| 222 |
command = get_commands_to_config_vpc_interface(portchannel, delta, config_value, existing) |
| 223 |
commands.append(command) |
| 224 |
|
| 225 |
return commands |
| 226 |
|
| 227 |
|
| 228 |
def state_absent(portchannel, existing): |
| 229 |
commands = [] |
| 230 |
if existing.get("vpc"): |
| 231 |
command = "no vpc" |
| 232 |
commands.append(command) |
| 233 |
elif existing.get("peer-link"): |
| 234 |
command = "no vpc peer-link" |
| 235 |
commands.append(command) |
| 236 |
if commands: |
| 237 |
commands.insert(0, "interface port-channel{0}".format(portchannel)) |
| 238 |
|
| 239 |
return commands |
| 240 |
|
| 241 |
|
| 242 |
def main(): |
| 243 |
argument_spec = dict( |
| 244 |
portchannel=dict(required=True, type="str"), |
| 245 |
vpc=dict(required=False, type="str"), |
| 246 |
peer_link=dict(required=False, type="bool"), |
| 247 |
state=dict(choices=["absent", "present"], default="present"), |
| 248 |
) |
| 249 |
|
| 250 |
module = AnsibleModule( |
| 251 |
argument_spec=argument_spec, |
| 252 |
mutually_exclusive=[["vpc", "peer_link"]], |
| 253 |
supports_check_mode=True, |
| 254 |
) |
| 255 |
|
| 256 |
warnings = list() |
| 257 |
commands = [] |
| 258 |
results = {"changed": False, "warnings": warnings} |
| 259 |
|
| 260 |
portchannel = module.params["portchannel"] |
| 261 |
vpc = module.params["vpc"] |
| 262 |
peer_link = module.params["peer_link"] |
| 263 |
state = module.params["state"] |
| 264 |
|
| 265 |
args = {"portchannel": portchannel, "vpc": vpc, "peer-link": peer_link} |
| 266 |
active_peer_link = None |
| 267 |
|
| 268 |
if portchannel not in get_portchannel_list(module): |
| 269 |
if not portchannel.isdigit() or int(portchannel) not in get_portchannel_list(module): |
| 270 |
module.fail_json( |
| 271 |
msg="The portchannel you are trying to make a" |
| 272 |
" VPC or PL is not created yet. " |
| 273 |
"Create it first!", |
| 274 |
) |
| 275 |
if vpc: |
| 276 |
mapping = get_existing_portchannel_to_vpc_mappings(module) |
| 277 |
|
| 278 |
if vpc in mapping and portchannel != mapping[vpc].strip("Po"): |
| 279 |
module.fail_json( |
| 280 |
msg="This vpc is already configured on " |
| 281 |
"another portchannel. Remove it first " |
| 282 |
"before trying to assign it here. ", |
| 283 |
existing_portchannel=mapping[vpc], |
| 284 |
) |
| 285 |
|
| 286 |
for vpcid, existing_pc in mapping.items(): |
| 287 |
if portchannel == existing_pc.strip("Po") and vpcid != vpc: |
| 288 |
module.fail_json( |
| 289 |
msg="This portchannel already has another" |
| 290 |
" VPC configured. Remove it first " |
| 291 |
"before assigning this one", |
| 292 |
existing_vpc=vpcid, |
| 293 |
) |
| 294 |
|
| 295 |
if peer_link_exists(module): |
| 296 |
active_peer_link = get_active_vpc_peer_link(module) |
| 297 |
if active_peer_link[-2:] == portchannel: |
| 298 |
module.fail_json( |
| 299 |
msg="That port channel is the current " |
| 300 |
"PEER LINK. Remove it if you want it" |
| 301 |
" to be a VPC", |
| 302 |
) |
| 303 |
config_value = vpc |
| 304 |
|
| 305 |
elif peer_link is not None: |
| 306 |
if peer_link_exists(module): |
| 307 |
active_peer_link = get_active_vpc_peer_link(module)[2::] |
| 308 |
if active_peer_link != portchannel: |
| 309 |
if peer_link: |
| 310 |
module.fail_json( |
| 311 |
msg="A peer link already exists on" " the device. Remove it first", |
| 312 |
current_peer_link="Po{0}".format(active_peer_link), |
| 313 |
) |
| 314 |
config_value = "peer-link" |
| 315 |
|
| 316 |
proposed = dict((k, v) for k, v in args.items() if v is not None) |
| 317 |
existing = get_portchannel_vpc_config(module, portchannel) |
| 318 |
|
| 319 |
if state == "present": |
| 320 |
delta = dict(set(proposed.items()).difference(existing.items())) |
| 321 |
if delta: |
| 322 |
commands = state_present(portchannel, delta, config_value, existing) |
| 323 |
|
| 324 |
elif state == "absent" and existing: |
| 325 |
commands = state_absent(portchannel, existing) |
| 326 |
|
| 327 |
cmds = flatten_list(commands) |
| 328 |
if cmds: |
| 329 |
if module.check_mode: |
| 330 |
module.exit_json(changed=True, commands=cmds) |
| 331 |
else: |
| 332 |
load_config(module, cmds) |
| 333 |
results["changed"] = True |
| 334 |
if "configure" in cmds: |
| 335 |
cmds.pop(0) |
| 336 |
|
| 337 |
results["commands"] = cmds |
| 338 |
module.exit_json(**results) |
| 339 |
|
| 340 |
|
| 341 |
if __name__ == "__main__": |
| 342 |
main() |