| 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_evpn_vni |
| 26 |
extends_documentation_fragment: |
| 27 |
- cisco.nxos.nxos |
| 28 |
short_description: Manages Cisco EVPN VXLAN Network Identifier (VNI). |
| 29 |
description: |
| 30 |
- Manages Cisco Ethernet Virtual Private Network (EVPN) VXLAN Network Identifier (VNI) |
| 31 |
configurations of a Nexus device. |
| 32 |
version_added: 1.0.0 |
| 33 |
author: Gabriele Gerbino (@GGabriele) |
| 34 |
notes: |
| 35 |
- Tested against NXOSv 7.3.(0)D1(1) on VIRL |
| 36 |
- Unsupported for Cisco MDS |
| 37 |
- default, where supported, restores params default value. |
| 38 |
- RD override is not permitted. You should set it to the default values first and |
| 39 |
then reconfigure it. |
| 40 |
- C(route_target_both), C(route_target_import) and C(route_target_export valid) values |
| 41 |
are a list of extended communities, (i.e. ['1.2.3.4:5', '33:55']) or the keywords |
| 42 |
'auto' or 'default'. |
| 43 |
- The C(route_target_both) property is discouraged due to the inconsistent behavior |
| 44 |
of the property across Nexus platforms and image versions. For this reason it is |
| 45 |
recommended to use explicit C(route_target_export) and C(route_target_import) properties |
| 46 |
instead of C(route_target_both). |
| 47 |
- RD valid values are a string in one of the route-distinguisher formats, the keyword |
| 48 |
'auto', or the keyword 'default'. |
| 49 |
options: |
| 50 |
vni: |
| 51 |
description: |
| 52 |
- The EVPN VXLAN Network Identifier. |
| 53 |
required: true |
| 54 |
type: str |
| 55 |
route_distinguisher: |
| 56 |
description: |
| 57 |
- The VPN Route Distinguisher (RD). The RD is combined with the IPv4 or IPv6 prefix |
| 58 |
learned by the PE router to create a globally unique address. |
| 59 |
type: str |
| 60 |
route_target_both: |
| 61 |
description: |
| 62 |
- Enables/Disables route-target settings for both import and export target communities |
| 63 |
using a single property. |
| 64 |
type: list |
| 65 |
elements: str |
| 66 |
route_target_import: |
| 67 |
description: |
| 68 |
- Sets the route-target 'import' extended communities. |
| 69 |
type: list |
| 70 |
elements: str |
| 71 |
route_target_export: |
| 72 |
description: |
| 73 |
- Sets the route-target 'export' extended communities. |
| 74 |
type: list |
| 75 |
elements: str |
| 76 |
state: |
| 77 |
description: |
| 78 |
- Determines whether the config should be present or not on the device. |
| 79 |
default: present |
| 80 |
choices: |
| 81 |
- present |
| 82 |
- absent |
| 83 |
type: str |
| 84 |
""" |
| 85 |
|
| 86 |
EXAMPLES = """ |
| 87 |
- name: vni configuration |
| 88 |
cisco.nxos.nxos_evpn_vni: |
| 89 |
vni: 6000 |
| 90 |
route_distinguisher: 60:10 |
| 91 |
route_target_import: |
| 92 |
- "5000:10" |
| 93 |
- "4100:100" |
| 94 |
route_target_export: auto |
| 95 |
route_target_both: default |
| 96 |
""" |
| 97 |
|
| 98 |
RETURN = """ |
| 99 |
commands: |
| 100 |
description: commands sent to the device |
| 101 |
returned: always |
| 102 |
type: list |
| 103 |
sample: ["evpn", "vni 6000 l2", "route-target import 5001:10"] |
| 104 |
""" |
| 105 |
|
| 106 |
import re |
| 107 |
|
| 108 |
from ansible.module_utils.basic import AnsibleModule |
| 109 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.config import ( |
| 110 |
CustomNetworkConfig, |
| 111 |
) |
| 112 |
|
| 113 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import ( |
| 114 |
get_config, |
| 115 |
load_config, |
| 116 |
) |
| 117 |
|
| 118 |
|
| 119 |
PARAM_TO_COMMAND_KEYMAP = { |
| 120 |
"vni": "vni", |
| 121 |
"route_distinguisher": "rd", |
| 122 |
"route_target_both": "route-target both", |
| 123 |
"route_target_import": "route-target import", |
| 124 |
"route_target_export": "route-target export", |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
def get_value(arg, config, module): |
| 129 |
command = PARAM_TO_COMMAND_KEYMAP.get(arg) |
| 130 |
command_re = re.compile(r"(?:{0}\s)(?P<value>.*)$".format(command), re.M) |
| 131 |
value = "" |
| 132 |
if command in config: |
| 133 |
value = command_re.search(config).group("value") |
| 134 |
return value |
| 135 |
|
| 136 |
|
| 137 |
def get_route_target_value(arg, config, module): |
| 138 |
splitted_config = config.splitlines() |
| 139 |
value_list = [] |
| 140 |
command = PARAM_TO_COMMAND_KEYMAP.get(arg) |
| 141 |
command_re = re.compile(r"(?:{0}\s)(?P<value>.*)$".format(command), re.M) |
| 142 |
|
| 143 |
for line in splitted_config: |
| 144 |
value = "" |
| 145 |
if command in line.strip(): |
| 146 |
value = command_re.search(line).group("value") |
| 147 |
value_list.append(value) |
| 148 |
return value_list |
| 149 |
|
| 150 |
|
| 151 |
def get_existing(module, args): |
| 152 |
existing = {} |
| 153 |
netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) |
| 154 |
parents = ["evpn", "vni {0} l2".format(module.params["vni"])] |
| 155 |
config = netcfg.get_section(parents) |
| 156 |
|
| 157 |
if config: |
| 158 |
for arg in args: |
| 159 |
if arg != "vni": |
| 160 |
if arg == "route_distinguisher": |
| 161 |
existing[arg] = get_value(arg, config, module) |
| 162 |
else: |
| 163 |
existing[arg] = get_route_target_value(arg, config, module) |
| 164 |
|
| 165 |
existing_fix = dict((k, v) for k, v in existing.items() if v) |
| 166 |
if not existing_fix: |
| 167 |
existing = existing_fix |
| 168 |
|
| 169 |
existing["vni"] = module.params["vni"] |
| 170 |
|
| 171 |
return existing |
| 172 |
|
| 173 |
|
| 174 |
def apply_key_map(key_map, table): |
| 175 |
new_dict = {} |
| 176 |
for key in table: |
| 177 |
new_key = key_map.get(key) |
| 178 |
if new_key: |
| 179 |
new_dict[new_key] = table.get(key) |
| 180 |
return new_dict |
| 181 |
|
| 182 |
|
| 183 |
def fix_proposed(proposed_commands): |
| 184 |
new_proposed = {} |
| 185 |
for key, value in proposed_commands.items(): |
| 186 |
if key == "route-target both": |
| 187 |
new_proposed["route-target export"] = value |
| 188 |
new_proposed["route-target import"] = value |
| 189 |
else: |
| 190 |
new_proposed[key] = value |
| 191 |
return new_proposed |
| 192 |
|
| 193 |
|
| 194 |
def state_present(module, existing, proposed): |
| 195 |
commands = list() |
| 196 |
parents = list() |
| 197 |
proposed_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, proposed) |
| 198 |
existing_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, existing) |
| 199 |
|
| 200 |
if proposed_commands.get("route-target both"): |
| 201 |
proposed_commands = fix_proposed(proposed_commands) |
| 202 |
|
| 203 |
for key, value in proposed_commands.items(): |
| 204 |
if key.startswith("route-target"): |
| 205 |
if value == ["default"]: |
| 206 |
existing_value = existing_commands.get(key) |
| 207 |
|
| 208 |
if existing_value: |
| 209 |
for target in existing_value: |
| 210 |
commands.append("no {0} {1}".format(key, target)) |
| 211 |
elif not isinstance(value, list): |
| 212 |
value = [value] |
| 213 |
|
| 214 |
for target in value: |
| 215 |
if target == "default": |
| 216 |
continue |
| 217 |
if existing: |
| 218 |
if target not in existing.get( |
| 219 |
key.replace("-", "_").replace(" ", "_"), |
| 220 |
): |
| 221 |
commands.append("{0} {1}".format(key, target)) |
| 222 |
else: |
| 223 |
commands.append("{0} {1}".format(key, target)) |
| 224 |
|
| 225 |
if existing.get(key.replace("-", "_").replace(" ", "_")): |
| 226 |
for exi in existing.get(key.replace("-", "_").replace(" ", "_")): |
| 227 |
if exi not in value: |
| 228 |
commands.append("no {0} {1}".format(key, exi)) |
| 229 |
|
| 230 |
elif value == "default": |
| 231 |
existing_value = existing_commands.get(key) |
| 232 |
if existing_value: |
| 233 |
commands.append("no {0} {1}".format(key, existing_value)) |
| 234 |
else: |
| 235 |
command = "{0} {1}".format(key, value) |
| 236 |
commands.append(command) |
| 237 |
|
| 238 |
if commands: |
| 239 |
parents = ["evpn", "vni {0} l2".format(module.params["vni"])] |
| 240 |
|
| 241 |
return commands, parents |
| 242 |
|
| 243 |
|
| 244 |
def state_absent(module, existing, proposed): |
| 245 |
commands = ["no vni {0} l2".format(module.params["vni"])] |
| 246 |
parents = ["evpn"] |
| 247 |
return commands, parents |
| 248 |
|
| 249 |
|
| 250 |
def main(): |
| 251 |
argument_spec = dict( |
| 252 |
vni=dict(required=True, type="str"), |
| 253 |
route_distinguisher=dict(required=False, type="str"), |
| 254 |
route_target_both=dict(required=False, type="list", elements="str"), |
| 255 |
route_target_import=dict(required=False, type="list", elements="str"), |
| 256 |
route_target_export=dict(required=False, type="list", elements="str"), |
| 257 |
state=dict(choices=["present", "absent"], default="present", required=False), |
| 258 |
) |
| 259 |
|
| 260 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 261 |
|
| 262 |
warnings = list() |
| 263 |
results = dict(changed=False, warnings=warnings) |
| 264 |
|
| 265 |
state = module.params["state"] |
| 266 |
args = PARAM_TO_COMMAND_KEYMAP.keys() |
| 267 |
existing = get_existing(module, args) |
| 268 |
proposed_args = dict((k, v) for k, v in module.params.items() if v is not None and k in args) |
| 269 |
commands = [] |
| 270 |
parents = [] |
| 271 |
|
| 272 |
proposed = {} |
| 273 |
for key, value in proposed_args.items(): |
| 274 |
if key != "vni": |
| 275 |
if value == "true": |
| 276 |
value = True |
| 277 |
elif value == "false": |
| 278 |
value = False |
| 279 |
if existing.get(key) != value: |
| 280 |
proposed[key] = value |
| 281 |
|
| 282 |
if state == "present": |
| 283 |
commands, parents = state_present(module, existing, proposed) |
| 284 |
elif state == "absent" and existing: |
| 285 |
commands, parents = state_absent(module, existing, proposed) |
| 286 |
|
| 287 |
if commands: |
| 288 |
candidate = CustomNetworkConfig(indent=3) |
| 289 |
candidate.add(commands, parents=parents) |
| 290 |
candidate = candidate.items_text() |
| 291 |
if not module.check_mode: |
| 292 |
load_config(module, candidate) |
| 293 |
results["changed"] = True |
| 294 |
results["commands"] = candidate |
| 295 |
else: |
| 296 |
results["commands"] = [] |
| 297 |
module.exit_json(**results) |
| 298 |
|
| 299 |
|
| 300 |
if __name__ == "__main__": |
| 301 |
main() |