Debian Codegraph @

ansible_collections/cisco/nxos/plugins/modules/nxos_feature.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 DOCUMENTATION = """
24 module: nxos_feature
25 extends_documentation_fragment:
26 - cisco.nxos.nxos
27 short_description: Manage features in NX-OS switches.
28 notes:
29 - Tested against Cisco MDS NX-OS 9.2(2)
30 description:
31 - Offers ability to enable and disable features in NX-OS.
32 version_added: 1.0.0
33 author:
34 - Jason Edelman (@jedelman8)
35 - Gabriele Gerbino (@GGabriele)
36 - Suhas Bharadwaj (@srbharadwaj)
37 options:
38 feature:
39 description:
40 - Name of feature.
41 required: true
42 type: str
43 state:
44 description:
45 - Desired state of the feature.
46 required: false
47 default: enabled
48 choices:
49 - enabled
50 - disabled
51 type: str
52 """
53
54 EXAMPLES = """
55 - name: Ensure lacp is enabled
56 cisco.nxos.nxos_feature:
57 feature: lacp
58 state: enabled
59
60 - name: Ensure ospf is disabled
61 cisco.nxos.nxos_feature:
62 feature: ospf
63 state: disabled
64
65 - name: Ensure vpc is enabled
66 cisco.nxos.nxos_feature:
67 feature: vpc
68 state: enabled
69 """
70
71 RETURN = """
72 commands:
73 description: The set of commands to be sent to the remote device
74 returned: always
75 type: list
76 sample: ['nv overlay evpn']
77 """
78
79 import re
80
81 from ansible.module_utils.basic import AnsibleModule
82 from ansible.module_utils.connection import ConnectionError
83
84 from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import (
85 get_capabilities,
86 get_config,
87 load_config,
88 run_commands,
89 )
90
91
92 def get_available_features(feature, module):
93 available_features = {}
94 feature_regex = r"(?P<feature>\S+)\s+\d+\s+(?P<state>.*)"
95 command = {"command": "show feature", "output": "text"}
96
97 try:
98 body = run_commands(module, [command])[0]
99 split_body = body.splitlines()
100 except (KeyError, IndexError):
101 return {}
102
103 for line in split_body:
104 try:
105 match_feature = re.match(feature_regex, line, re.DOTALL)
106 feature_group = match_feature.groupdict()
107 feature = feature_group["feature"]
108 state = feature_group["state"]
109 except AttributeError:
110 feature = ""
111 state = ""
112
113 if feature and state:
114 if "enabled" in state:
115 state = "enabled"
116
117 if feature not in available_features:
118 available_features[feature] = state
119 else:
120 if available_features[feature] == "disabled" and state == "enabled":
121 available_features[feature] = state
122
123 # certain configurable features do not
124 # show up in the output of "show feature"
125 # but appear in running-config when set
126 run_cfg = get_config(module, flags=["| include ^feature"])
127 for item in re.findall(r"feature\s(.*)", run_cfg):
128 if item not in available_features:
129 available_features[item] = "enabled"
130
131 if "fabric forwarding" not in available_features:
132 available_features["fabric forwarding"] = "disabled"
133
134 return available_features
135
136
137 def get_commands(proposed, existing, state, module):
138 feature = validate_feature(module, mode="config")
139 commands = []
140 feature_check = proposed == existing
141 if not feature_check:
142 if state == "enabled":
143 command = "feature {0}".format(feature)
144 commands.append(command)
145 elif state == "disabled":
146 command = "no feature {0}".format(feature)
147 commands.append(command)
148 return commands
149
150
151 def get_mds_mapping_features():
152 feature_to_be_mapped = {
153 "show": {
154 "fcrxbbcredit": "extended_credit",
155 "port-track": "port_track",
156 "scp-server": "scpServer",
157 "sftp-server": "sftpServer",
158 "ssh": "sshServer",
159 "tacacs+": "tacacs",
160 "telnet": "telnetServer",
161 },
162 "config": {
163 "extended_credit": "fcrxbbcredit",
164 "port_track": "port-track",
165 "scpServer": "scp-server",
166 "sftpServer": "sftp-server",
167 "sshServer": "ssh",
168 "tacacs": "tacacs+",
169 "telnetServer": "telnet",
170 },
171 }
172 return feature_to_be_mapped
173
174
175 def validate_feature(module, mode="show"):
176 """Some features may need to be mapped due to inconsistency
177 between how they appear from "show feature" output and
178 how they are configured"""
179
180 feature = module.params["feature"]
181
182 try:
183 info = get_capabilities(module)
184 device_info = info.get("device_info", {})
185 os_version = device_info.get("network_os_version", "")
186 os_platform = device_info.get("network_os_platform", "")
187 except ConnectionError:
188 os_version = ""
189 os_platform = ""
190
191 if "8.1" in os_version:
192 feature_to_be_mapped = {
193 "show": {
194 "nv overlay": "nve",
195 "vn-segment-vlan-based": "vnseg_vlan",
196 "hsrp": "hsrp_engine",
197 "fabric multicast": "fabric_mcast",
198 "scp-server": "scpServer",
199 "sftp-server": "sftpServer",
200 "sla responder": "sla_responder",
201 "sla sender": "sla_sender",
202 "ssh": "sshServer",
203 "tacacs+": "tacacs",
204 "telnet": "telnetServer",
205 "ethernet-link-oam": "elo",
206 },
207 "config": {
208 "nve": "nv overlay",
209 "vnseg_vlan": "vn-segment-vlan-based",
210 "hsrp_engine": "hsrp",
211 "fabric_mcast": "fabric multicast",
212 "scpServer": "scp-server",
213 "sftpServer": "sftp-server",
214 "sla_sender": "sla sender",
215 "sla_responder": "sla responder",
216 "sshServer": "ssh",
217 "tacacs": "tacacs+",
218 "telnetServer": "telnet",
219 "elo": "ethernet-link-oam",
220 },
221 }
222 else:
223 feature_to_be_mapped = {
224 "show": {
225 "nv overlay": "nve",
226 "vn-segment-vlan-based": "vnseg_vlan",
227 "hsrp": "hsrp_engine",
228 "fabric multicast": "fabric_mcast",
229 "scp-server": "scpServer",
230 "sftp-server": "sftpServer",
231 "sla responder": "sla_responder",
232 "sla sender": "sla_sender",
233 "ssh": "sshServer",
234 "tacacs+": "tacacs",
235 "telnet": "telnetServer",
236 "ethernet-link-oam": "elo",
237 "port-security": "eth-port-sec",
238 },
239 "config": {
240 "nve": "nv overlay",
241 "vnseg_vlan": "vn-segment-vlan-based",
242 "hsrp_engine": "hsrp",
243 "fabric_mcast": "fabric multicast",
244 "scpServer": "scp-server",
245 "sftpServer": "sftp-server",
246 "sla_sender": "sla sender",
247 "sla_responder": "sla responder",
248 "sshServer": "ssh",
249 "tacacs": "tacacs+",
250 "telnetServer": "telnet",
251 "elo": "ethernet-link-oam",
252 "eth-port-sec": "port-security",
253 },
254 }
255
256 if os_platform.startswith("DS-"):
257 feature_to_be_mapped = get_mds_mapping_features()
258
259 if feature in feature_to_be_mapped[mode]:
260 feature = feature_to_be_mapped[mode][feature]
261
262 return feature
263
264
265 def main():
266 argument_spec = dict(
267 feature=dict(type="str", required=True),
268 state=dict(choices=["enabled", "disabled"], default="enabled"),
269 )
270
271 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
272
273 warnings = list()
274 results = dict(changed=False, warnings=warnings)
275
276 feature = validate_feature(module)
277 state = module.params["state"].lower()
278
279 available_features = get_available_features(feature, module)
280 if feature not in available_features:
281 module.fail_json(
282 msg="Invalid feature name.",
283 features_currently_supported=available_features,
284 invalid_feature=feature,
285 )
286 else:
287 existstate = available_features[feature]
288
289 existing = dict(state=existstate)
290 proposed = dict(state=state)
291 results["changed"] = False
292
293 cmds = get_commands(proposed, existing, state, module)
294
295 if cmds:
296 # On N35 A8 images, some features return a yes/no prompt
297 # on enablement or disablement. Bypass using terminal dont-ask
298 cmds.insert(0, "terminal dont-ask")
299 if not module.check_mode:
300 load_config(module, cmds)
301 results["changed"] = True
302
303 results["commands"] = cmds
304 module.exit_json(**results)
305
306
307 if __name__ == "__main__":
308 main()