Debian Codegraph @

ansible_collections/cisco/nxos/plugins/modules/nxos_pim.py

raw
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_pim
26 extends_documentation_fragment:
27 - cisco.nxos.nxos
28 short_description: Manages configuration of a PIM instance.
29 description:
30 - Manages configuration of a Protocol Independent Multicast (PIM) instance.
31 notes:
32 - Unsupported for Cisco MDS
33 version_added: 1.0.0
34 author: Gabriele Gerbino (@GGabriele)
35 options:
36 bfd:
37 description:
38 - Enables BFD on all PIM interfaces.
39 - "Dependency: ''feature bfd''"
40 type: str
41 choices:
42 - enable
43 - disable
44 ssm_range:
45 description:
46 - Configure group ranges for Source Specific Multicast (SSM). Valid values are
47 multicast addresses or the keyword C(none) or keyword C(default). C(none) removes
48 all SSM group ranges. C(default) will set ssm_range to the default multicast
49 address. If you set multicast address, please ensure that it is not the same
50 as the C(default), otherwise use the C(default) option.
51 type: list
52 default: []
53 elements: str
54 """
55 EXAMPLES = """
56 - name: Configure ssm_range, enable bfd
57 cisco.nxos.nxos_pim:
58 bfd: enable
59 ssm_range: 224.0.0.0/8
60
61 - name: Set to default
62 cisco.nxos.nxos_pim:
63 ssm_range: default
64
65 - name: Remove all ssm group ranges
66 cisco.nxos.nxos_pim:
67 ssm_range: none
68 """
69
70 RETURN = """
71 commands:
72 description: commands sent to the device
73 returned: always
74 type: list
75 sample:
76 - ip pim bfd
77 - ip pim ssm range 224.0.0.0/8
78 """
79
80
81 import re
82
83 from ansible.module_utils.basic import AnsibleModule
84 from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.config import (
85 CustomNetworkConfig,
86 )
87
88 from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import (
89 get_config,
90 load_config,
91 )
92
93
94 PARAM_TO_COMMAND_KEYMAP = {
95 "bfd": "ip pim bfd",
96 "ssm_range": "ip pim ssm range",
97 }
98
99
100 def get_existing(module, args):
101 existing = {}
102 config = str(get_config(module))
103
104 for arg in args:
105 if "ssm_range" in arg:
106 # <value> may be 'n.n.n.n/s', 'none', or 'default'
107 m = re.search(
108 r"ssm range (?P<value>(?:[\s\d.\/]+|none|default))?$",
109 config,
110 re.M,
111 )
112 if m:
113 # Remove rsvd SSM range
114 value = m.group("value").replace("232.0.0.0/8", "")
115 existing[arg] = value.split()
116
117 elif "bfd" in arg and "ip pim bfd" in config:
118 existing[arg] = "enable"
119
120 return existing
121
122
123 def apply_key_map(key_map, table):
124 new_dict = {}
125 for key, value in table.items():
126 new_key = key_map.get(key)
127 if value is not None:
128 new_dict[new_key] = value
129 return new_dict
130
131
132 def get_commands(module, existing, proposed, candidate):
133 commands = list()
134 proposed_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, proposed)
135
136 for key, value in proposed_commands.items():
137 command = ""
138 if key == "ip pim ssm range":
139 if value == "default":
140 # no cmd needs a value but the actual value does not matter
141 command = "no ip pim ssm range none"
142 elif value == "none":
143 command = "ip pim ssm range none"
144 elif value:
145 command = "ip pim ssm range {0}".format(value)
146 elif key == "ip pim bfd":
147 no_cmd = "no " if value == "disable" else ""
148 command = no_cmd + key
149
150 if command:
151 commands.append(command)
152
153 if commands:
154 candidate.add(commands, parents=[])
155
156
157 def main():
158 argument_spec = dict(
159 bfd=dict(required=False, type="str", choices=["enable", "disable"]),
160 ssm_range=dict(required=False, type="list", default=[], elements="str"),
161 )
162
163 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
164 warnings = list()
165 result = {"changed": False, "commands": [], "warnings": warnings}
166
167 params = module.params
168 args = [k for k in PARAM_TO_COMMAND_KEYMAP.keys() if params[k] is not None]
169
170 # SSM syntax check
171 if "ssm_range" in args:
172 for item in params["ssm_range"]:
173 if re.search("none|default", item):
174 break
175 if len(item.split(".")) != 4:
176 module.fail_json(
177 msg="Valid ssm_range values are multicast addresses "
178 "or the keyword 'none' or the keyword 'default'.",
179 )
180
181 existing = get_existing(module, args)
182 proposed_args = dict((k, v) for k, v in params.items() if k in args)
183
184 proposed = {}
185 for key, value in proposed_args.items():
186 if key == "ssm_range":
187 if value and value[0] == "default":
188 if existing.get(key):
189 proposed[key] = "default"
190 else:
191 v = sorted(set([str(i) for i in value]))
192 ex = sorted(set([str(i) for i in existing.get(key, [])]))
193 if v != ex:
194 proposed[key] = " ".join(str(s) for s in v)
195
196 elif key == "bfd":
197 if value != existing.get("bfd", "disable"):
198 proposed[key] = value
199
200 elif value != existing.get(key):
201 proposed[key] = value
202
203 candidate = CustomNetworkConfig(indent=3)
204 get_commands(module, existing, proposed, candidate)
205
206 if candidate:
207 candidate = candidate.items_text()
208 result["commands"] = candidate
209 result["changed"] = True
210 load_config(module, candidate)
211
212 module.exit_json(**result)
213
214
215 if __name__ == "__main__":
216 main()