| 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_igmp_snooping |
| 26 |
extends_documentation_fragment: |
| 27 |
- cisco.nxos.nxos |
| 28 |
short_description: Manages IGMP snooping global configuration. |
| 29 |
description: |
| 30 |
- Manages IGMP snooping global 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 |
- When C(state=default), params will be reset to a default state. |
| 39 |
- C(group_timeout) also accepts I(never) as an input. |
| 40 |
options: |
| 41 |
snooping: |
| 42 |
description: |
| 43 |
- Enables/disables IGMP snooping on the switch. |
| 44 |
type: bool |
| 45 |
group_timeout: |
| 46 |
description: |
| 47 |
- Group membership timeout value for all VLANs on the device. Accepted values |
| 48 |
are integer in range 1-10080, I(never) and I(default). |
| 49 |
type: str |
| 50 |
link_local_grp_supp: |
| 51 |
description: |
| 52 |
- Global link-local groups suppression. |
| 53 |
type: bool |
| 54 |
report_supp: |
| 55 |
description: |
| 56 |
- Global IGMPv1/IGMPv2 Report Suppression. |
| 57 |
type: bool |
| 58 |
v3_report_supp: |
| 59 |
description: |
| 60 |
- Global IGMPv3 Report Suppression and Proxy Reporting. |
| 61 |
type: bool |
| 62 |
state: |
| 63 |
description: |
| 64 |
- Manage the state of the resource. |
| 65 |
default: present |
| 66 |
choices: |
| 67 |
- present |
| 68 |
- default |
| 69 |
type: str |
| 70 |
""" |
| 71 |
|
| 72 |
EXAMPLES = """ |
| 73 |
# ensure igmp snooping params supported in this module are in there default state |
| 74 |
- cisco.nxos.nxos_igmp_snooping: |
| 75 |
state: default |
| 76 |
|
| 77 |
# ensure following igmp snooping params are in the desired state |
| 78 |
- cisco.nxos.nxos_igmp_snooping: |
| 79 |
group_timeout: never |
| 80 |
snooping: true |
| 81 |
link_local_grp_supp: false |
| 82 |
optimize_mcast_flood: false |
| 83 |
report_supp: true |
| 84 |
v3_report_supp: true |
| 85 |
""" |
| 86 |
|
| 87 |
RETURN = """ |
| 88 |
commands: |
| 89 |
description: command sent to the device |
| 90 |
returned: always |
| 91 |
type: list |
| 92 |
sample: ["ip igmp snooping link-local-groups-suppression", |
| 93 |
"ip igmp snooping group-timeout 50", |
| 94 |
"no ip igmp snooping report-suppression", |
| 95 |
"no ip igmp snooping v3-report-suppression", |
| 96 |
"no ip igmp snooping"] |
| 97 |
""" |
| 98 |
|
| 99 |
|
| 100 |
import re |
| 101 |
|
| 102 |
from ansible.module_utils.basic import AnsibleModule |
| 103 |
|
| 104 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import ( |
| 105 |
load_config, |
| 106 |
run_commands, |
| 107 |
) |
| 108 |
|
| 109 |
|
| 110 |
def execute_show_command(command, module, output="text"): |
| 111 |
command = {"command": command, "output": output} |
| 112 |
|
| 113 |
return run_commands(module, [command]) |
| 114 |
|
| 115 |
|
| 116 |
def flatten_list(command_lists): |
| 117 |
flat_command_list = [] |
| 118 |
for command in command_lists: |
| 119 |
if isinstance(command, list): |
| 120 |
flat_command_list.extend(command) |
| 121 |
else: |
| 122 |
flat_command_list.append(command) |
| 123 |
return flat_command_list |
| 124 |
|
| 125 |
|
| 126 |
def get_group_timeout(config): |
| 127 |
match = re.search(r" Group timeout configured: (\S+)", config, re.M) |
| 128 |
if match: |
| 129 |
value = match.group(1) |
| 130 |
else: |
| 131 |
value = "" |
| 132 |
return value |
| 133 |
|
| 134 |
|
| 135 |
def get_igmp_snooping(module): |
| 136 |
command = "show ip igmp snooping" |
| 137 |
existing = {} |
| 138 |
|
| 139 |
try: |
| 140 |
body = execute_show_command(command, module, output="json")[0] |
| 141 |
except IndexError: |
| 142 |
body = [] |
| 143 |
|
| 144 |
if body: |
| 145 |
snooping = str(body.get("enabled")).lower() |
| 146 |
if "none" in snooping: |
| 147 |
snooping = str(body.get("GlobalSnoopEnabled")).lower() |
| 148 |
if snooping == "true" or snooping == "enabled" or snooping == "yes": |
| 149 |
existing["snooping"] = True |
| 150 |
else: |
| 151 |
existing["snooping"] = False |
| 152 |
|
| 153 |
report_supp = str(body.get("grepsup")).lower() |
| 154 |
if "none" in report_supp: |
| 155 |
report_supp = str(body.get("GlobalReportSupression")).lower() |
| 156 |
if report_supp == "true" or report_supp == "enabled": |
| 157 |
existing["report_supp"] = True |
| 158 |
else: |
| 159 |
existing["report_supp"] = False |
| 160 |
|
| 161 |
link_local_grp_supp = str(body.get("glinklocalgrpsup")).lower() |
| 162 |
if "none" in link_local_grp_supp: |
| 163 |
link_local_grp_supp = str(body.get("GlobalLinkLocalGroupSupression")).lower() |
| 164 |
if link_local_grp_supp == "true" or link_local_grp_supp == "enabled": |
| 165 |
existing["link_local_grp_supp"] = True |
| 166 |
else: |
| 167 |
existing["link_local_grp_supp"] = False |
| 168 |
|
| 169 |
v3_report_supp = str(body.get("gv3repsup")).lower() |
| 170 |
if "none" in v3_report_supp: |
| 171 |
v3_report_supp = str(body.get("GlobalV3ReportSupression")).lower() |
| 172 |
if v3_report_supp == "true" or v3_report_supp == "enabled": |
| 173 |
existing["v3_report_supp"] = True |
| 174 |
else: |
| 175 |
existing["v3_report_supp"] = False |
| 176 |
|
| 177 |
command = "show ip igmp snooping" |
| 178 |
body = execute_show_command(command, module)[0] |
| 179 |
if body: |
| 180 |
existing["group_timeout"] = get_group_timeout(body) |
| 181 |
|
| 182 |
return existing |
| 183 |
|
| 184 |
|
| 185 |
def config_igmp_snooping(delta, existing, default=False): |
| 186 |
CMDS = { |
| 187 |
"snooping": "ip igmp snooping", |
| 188 |
"group_timeout": "ip igmp snooping group-timeout {}", |
| 189 |
"link_local_grp_supp": "ip igmp snooping link-local-groups-suppression", |
| 190 |
"v3_report_supp": "ip igmp snooping v3-report-suppression", |
| 191 |
"report_supp": "ip igmp snooping report-suppression", |
| 192 |
} |
| 193 |
|
| 194 |
commands = [] |
| 195 |
command = None |
| 196 |
gt_command = None |
| 197 |
for key, value in delta.items(): |
| 198 |
if value: |
| 199 |
if default and key == "group_timeout": |
| 200 |
if existing.get(key): |
| 201 |
gt_command = "no " + CMDS.get(key).format(existing.get(key)) |
| 202 |
elif value == "default" and key == "group_timeout": |
| 203 |
if existing.get(key): |
| 204 |
command = "no " + CMDS.get(key).format(existing.get(key)) |
| 205 |
else: |
| 206 |
command = CMDS.get(key).format(value) |
| 207 |
else: |
| 208 |
command = "no " + CMDS.get(key).format(value) |
| 209 |
|
| 210 |
if command: |
| 211 |
commands.append(command) |
| 212 |
command = None |
| 213 |
|
| 214 |
if gt_command: |
| 215 |
# ensure that group-timeout command is configured last |
| 216 |
commands.append(gt_command) |
| 217 |
return commands |
| 218 |
|
| 219 |
|
| 220 |
def get_igmp_snooping_defaults(): |
| 221 |
group_timeout = "dummy" |
| 222 |
report_supp = True |
| 223 |
link_local_grp_supp = True |
| 224 |
v3_report_supp = False |
| 225 |
snooping = True |
| 226 |
|
| 227 |
args = dict( |
| 228 |
snooping=snooping, |
| 229 |
link_local_grp_supp=link_local_grp_supp, |
| 230 |
report_supp=report_supp, |
| 231 |
v3_report_supp=v3_report_supp, |
| 232 |
group_timeout=group_timeout, |
| 233 |
) |
| 234 |
|
| 235 |
default = dict((param, value) for (param, value) in args.items() if value is not None) |
| 236 |
|
| 237 |
return default |
| 238 |
|
| 239 |
|
| 240 |
def igmp_snooping_gt_dependency(command, existing, module): |
| 241 |
# group-timeout will fail if igmp snooping is disabled |
| 242 |
gt = [i for i in command if i.startswith("ip igmp snooping group-timeout")] |
| 243 |
if gt: |
| 244 |
if "no ip igmp snooping" in command or ( |
| 245 |
existing["snooping"] is False and "ip igmp snooping" not in command |
| 246 |
): |
| 247 |
msg = "group-timeout cannot be enabled or changed when ip igmp snooping is disabled" |
| 248 |
module.fail_json(msg=msg) |
| 249 |
else: |
| 250 |
# ensure that group-timeout command is configured last |
| 251 |
command.remove(gt[0]) |
| 252 |
command.append(gt[0]) |
| 253 |
|
| 254 |
|
| 255 |
def main(): |
| 256 |
argument_spec = dict( |
| 257 |
snooping=dict(required=False, type="bool"), |
| 258 |
group_timeout=dict(required=False, type="str"), |
| 259 |
link_local_grp_supp=dict(required=False, type="bool"), |
| 260 |
report_supp=dict(required=False, type="bool"), |
| 261 |
v3_report_supp=dict(required=False, type="bool"), |
| 262 |
state=dict(choices=["present", "default"], default="present"), |
| 263 |
) |
| 264 |
|
| 265 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 266 |
|
| 267 |
warnings = list() |
| 268 |
results = {"changed": False, "commands": [], "warnings": warnings} |
| 269 |
|
| 270 |
snooping = module.params["snooping"] |
| 271 |
link_local_grp_supp = module.params["link_local_grp_supp"] |
| 272 |
report_supp = module.params["report_supp"] |
| 273 |
v3_report_supp = module.params["v3_report_supp"] |
| 274 |
group_timeout = module.params["group_timeout"] |
| 275 |
state = module.params["state"] |
| 276 |
|
| 277 |
args = dict( |
| 278 |
snooping=snooping, |
| 279 |
link_local_grp_supp=link_local_grp_supp, |
| 280 |
report_supp=report_supp, |
| 281 |
v3_report_supp=v3_report_supp, |
| 282 |
group_timeout=group_timeout, |
| 283 |
) |
| 284 |
|
| 285 |
proposed = dict((param, value) for (param, value) in args.items() if value is not None) |
| 286 |
|
| 287 |
existing = get_igmp_snooping(module) |
| 288 |
|
| 289 |
commands = [] |
| 290 |
if state == "present": |
| 291 |
delta = dict(set(proposed.items()).difference(existing.items())) |
| 292 |
if delta: |
| 293 |
command = config_igmp_snooping(delta, existing) |
| 294 |
if command: |
| 295 |
if group_timeout: |
| 296 |
igmp_snooping_gt_dependency(command, existing, module) |
| 297 |
commands.append(command) |
| 298 |
elif state == "default": |
| 299 |
proposed = get_igmp_snooping_defaults() |
| 300 |
delta = dict(set(proposed.items()).difference(existing.items())) |
| 301 |
if delta: |
| 302 |
command = config_igmp_snooping(delta, existing, default=True) |
| 303 |
if command: |
| 304 |
commands.append(command) |
| 305 |
|
| 306 |
cmds = flatten_list(commands) |
| 307 |
if cmds: |
| 308 |
results["changed"] = True |
| 309 |
if not module.check_mode: |
| 310 |
load_config(module, cmds) |
| 311 |
if "configure" in cmds: |
| 312 |
cmds.pop(0) |
| 313 |
results["commands"] = cmds |
| 314 |
|
| 315 |
module.exit_json(**results) |
| 316 |
|
| 317 |
|
| 318 |
if __name__ == "__main__": |
| 319 |
main() |