Debian Codegraph @

ansible_collections/cisco/nxos/plugins/modules/nxos_udld_interface.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_udld_interface
26 extends_documentation_fragment:
27 - cisco.nxos.nxos
28 short_description: Manages UDLD interface configuration params.
29 description:
30 - Manages UDLD interface configuration params.
31 version_added: 1.0.0
32 author:
33 - Jason Edelman (@jedelman8)
34 notes:
35 - Tested against NXOSv 7.3.(0)D1(1) on VIRL
36 - Unsupported for Cisco MDS
37 - Feature UDLD must be enabled on the device to use this module.
38 options:
39 mode:
40 description:
41 - Manages UDLD mode for an interface.
42 required: true
43 choices:
44 - enabled
45 - disabled
46 - aggressive
47 type: str
48 interface:
49 description:
50 - FULL name of the interface, i.e. Ethernet1/1-
51 required: true
52 type: str
53 state:
54 description:
55 - Manage the state of the resource.
56 required: false
57 default: present
58 choices:
59 - present
60 - absent
61 type: str
62 """
63 EXAMPLES = """
64 # ensure Ethernet1/1 is configured to be in aggressive mode
65 - cisco.nxos.nxos_udld_interface:
66 interface: Ethernet1/1
67 mode: aggressive
68 state: present
69 host: '{{ inventory_hostname }}'
70 username: '{{ un }}'
71 password: '{{ pwd }}'
72
73 # Remove the aggressive config only if it's currently in aggressive mode and then disable udld (switch default)
74 - cisco.nxos.nxos_udld_interface:
75 interface: Ethernet1/1
76 mode: aggressive
77 state: absent
78 host: '{{ inventory_hostname }}'
79 username: '{{ un }}'
80 password: '{{ pwd }}'
81
82 # ensure Ethernet1/1 has aggressive mode enabled
83 - cisco.nxos.nxos_udld_interface:
84 interface: Ethernet1/1
85 mode: enabled
86 host: '{{ inventory_hostname }}'
87 username: '{{ un }}'
88 password: '{{ pwd }}'
89 """
90
91 RETURN = """
92 proposed:
93 description: k/v pairs of parameters passed into module
94 returned: always
95 type: dict
96 sample: {"mode": "enabled"}
97 existing:
98 description:
99 - k/v pairs of existing configuration
100 returned: always
101 type: dict
102 sample: {"mode": "aggressive"}
103 end_state:
104 description: k/v pairs of configuration after module execution
105 returned: always
106 type: dict
107 sample: {"mode": "enabled"}
108 updates:
109 description: command sent to the device
110 returned: always
111 type: list
112 sample: ["interface ethernet1/33",
113 "no udld aggressive ; no udld disable"]
114 changed:
115 description: check to see if a change was made on the device
116 returned: always
117 type: bool
118 sample: true
119 """
120
121
122 from ansible.module_utils.basic import AnsibleModule
123
124 from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import (
125 load_config,
126 run_commands,
127 )
128
129
130 def flatten_list(command_lists):
131 flat_command_list = []
132 for command in command_lists:
133 if isinstance(command, list):
134 flat_command_list.extend(command)
135 else:
136 flat_command_list.append(command)
137 return flat_command_list
138
139
140 def get_udld_interface(module, interface):
141 command = "show run udld all | section " + interface.title() + "$"
142 interface_udld = {}
143 mode = None
144 mode_str = None
145 try:
146 body = run_commands(module, [{"command": command, "output": "text"}])[0]
147 if "aggressive" in body:
148 mode = "aggressive"
149 mode_str = "aggressive"
150 elif "no udld enable" in body:
151 mode = "disabled"
152 mode_str = "no udld enable"
153 elif "no udld disable" in body:
154 mode = "enabled"
155 mode_str = "no udld disable"
156 elif "udld disable" in body:
157 mode = "disabled"
158 mode_str = "udld disable"
159 elif "udld enable" in body:
160 mode = "enabled"
161 mode_str = "udld enable"
162 interface_udld["mode"] = mode
163
164 except (KeyError, AttributeError, IndexError):
165 interface_udld = {}
166
167 return interface_udld, mode_str
168
169
170 def get_commands_config_udld_interface1(delta, interface, module, existing):
171 commands = []
172 mode = delta["mode"]
173 if mode == "aggressive":
174 commands.append("udld aggressive")
175 else:
176 commands.append("no udld aggressive")
177 commands.insert(0, "interface {0}".format(interface))
178
179 return commands
180
181
182 def get_commands_config_udld_interface2(delta, interface, module, existing):
183 commands = []
184 existing, mode_str = get_udld_interface(module, interface)
185 mode = delta["mode"]
186 if mode == "enabled":
187 if mode_str == "no udld enable":
188 command = "udld enable"
189 else:
190 command = "no udld disable"
191 else:
192 if mode_str == "no udld disable":
193 command = "udld disable"
194 else:
195 command = "no udld enable"
196 if command:
197 commands.append(command)
198 commands.insert(0, "interface {0}".format(interface))
199
200 return commands
201
202
203 def get_commands_remove_udld_interface(delta, interface, module, existing):
204 commands = []
205 existing, mode_str = get_udld_interface(module, interface)
206
207 mode = delta["mode"]
208 if mode == "aggressive":
209 command = "no udld aggressive"
210 else:
211 if mode == "enabled":
212 if mode_str == "udld enable":
213 command = "no udld enable"
214 else:
215 command = "udld disable"
216 elif mode == "disabled":
217 if mode_str == "no udld disable":
218 command = "udld disable"
219 else:
220 command = "no udld enable"
221 if command:
222 commands.append(command)
223 commands.insert(0, "interface {0}".format(interface))
224
225 return commands
226
227
228 def main():
229 argument_spec = dict(
230 mode=dict(choices=["enabled", "disabled", "aggressive"], required=True),
231 interface=dict(type="str", required=True),
232 state=dict(choices=["absent", "present"], default="present"),
233 )
234
235 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
236
237 warnings = list()
238
239 interface = module.params["interface"].lower()
240 mode = module.params["mode"]
241 state = module.params["state"]
242
243 proposed = dict(mode=mode)
244 existing, mode_str = get_udld_interface(module, interface)
245 end_state = existing
246
247 delta = dict(set(proposed.items()).difference(existing.items()))
248
249 changed = False
250 commands = []
251 cmds = []
252 if state == "present":
253 if delta:
254 command = get_commands_config_udld_interface1(delta, interface, module, existing)
255 commands.append(command)
256 cmds = flatten_list(commands)
257 if module.check_mode:
258 module.exit_json(changed=True, commands=cmds)
259 else:
260 changed = True
261 load_config(module, cmds)
262
263 if delta["mode"] == "enabled" or delta["mode"] == "disabled":
264 commands = []
265 command = get_commands_config_udld_interface2(delta, interface, module, existing)
266 commands.append(command)
267 cmds = flatten_list(commands)
268 if module.check_mode:
269 module.exit_json(changed=True, commands=cmds)
270 else:
271 load_config(module, cmds)
272
273 else:
274 common = set(proposed.items()).intersection(existing.items())
275 if common:
276 command = get_commands_remove_udld_interface(dict(common), interface, module, existing)
277 cmds = flatten_list(commands)
278 if module.check_mode:
279 module.exit_json(changed=True, commands=cmds)
280 else:
281 changed = True
282 load_config(module, cmds)
283
284 if not module.check_mode:
285 end_state, mode_str = get_udld_interface(module, interface)
286 if "configure" in cmds:
287 cmds.pop(0)
288
289 results = {}
290 results["proposed"] = proposed
291 results["existing"] = existing
292 results["end_state"] = end_state
293 results["updates"] = cmds
294 results["changed"] = changed
295 results["warnings"] = warnings
296
297 module.exit_json(**results)
298
299
300 if __name__ == "__main__":
301 main()