Debian Codegraph @

ansible_collections/cisco/nxos/plugins/modules/nxos_vtp_version.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_vtp_version
26 extends_documentation_fragment:
27 - cisco.nxos.nxos
28 short_description: Manages VTP version configuration.
29 description:
30 - Manages VTP version configuration.
31 version_added: 1.0.0
32 author:
33 - Gabriele Gerbino (@GGabriele)
34 notes:
35 - Tested against NXOSv 7.3.(0)D1(1) on VIRL
36 - Unsupported for Cisco MDS
37 - VTP feature must be active on the device to use this module.
38 - This module is used to manage only VTP version.
39 - Use this in combination with M(cisco.nxos.nxos_vtp_password) and M(cisco.nxos.nxos_vtp_version)
40 to fully manage VTP operations.
41 options:
42 version:
43 description:
44 - VTP version number.
45 required: true
46 choices:
47 - '1'
48 - '2'
49 - '3'
50 type: str
51 """
52 EXAMPLES = """
53 # ENSURE VTP VERSION IS 2
54 - cisco.nxos.nxos_vtp_version:
55 version: 2
56 """
57
58 RETURN = """
59 proposed:
60 description: k/v pairs of parameters passed into module
61 returned: always
62 type: dict
63 sample: {"version": "2"}
64 existing:
65 description:
66 - k/v pairs of existing vtp
67 returned: always
68 type: dict
69 sample: {"domain": "testing", "version": "1", "vtp_password": "password"}
70 end_state:
71 description: k/v pairs of vtp after module execution
72 returned: always
73 type: dict
74 sample: {"domain": "testing", "version": "2", "vtp_password": "password"}
75 updates:
76 description: command sent to the device
77 returned: always
78 type: list
79 sample: ["vtp version 2"]
80 changed:
81 description: check to see if a change was made on the device
82 returned: always
83 type: bool
84 sample: true
85 """
86 import re
87
88 from ansible.module_utils.basic import AnsibleModule
89
90 from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import (
91 get_capabilities,
92 load_config,
93 run_commands,
94 )
95
96
97 def execute_show_command(command, module, output="json"):
98 cmds = [{"command": command, "output": output}]
99 body = run_commands(module, cmds)
100 return body
101
102
103 def flatten_list(command_lists):
104 flat_command_list = []
105 for command in command_lists:
106 if isinstance(command, list):
107 flat_command_list.extend(command)
108 else:
109 flat_command_list.append(command)
110 return flat_command_list
111
112
113 def get_vtp_config(module):
114 command = "show vtp status"
115 body = execute_show_command(command, module, "text")[0]
116 vtp_parsed = {}
117
118 if body:
119 version_regex = r".*VTP version running\s+:\s+(?P<version>\d).*"
120 domain_regex = r".*VTP Domain Name\s+:\s+(?P<domain>\S+).*"
121
122 try:
123 match_version = re.match(version_regex, body, re.DOTALL)
124 version = match_version.groupdict()["version"]
125 except AttributeError:
126 version = ""
127
128 try:
129 match_domain = re.match(domain_regex, body, re.DOTALL)
130 domain = match_domain.groupdict()["domain"]
131 except AttributeError:
132 domain = ""
133
134 if domain and version:
135 vtp_parsed["domain"] = domain
136 vtp_parsed["version"] = version
137 vtp_parsed["vtp_password"] = get_vtp_password(module)
138
139 return vtp_parsed
140
141
142 def get_vtp_password(module):
143 command = "show vtp password"
144 output = "json"
145 cap = get_capabilities(module)["device_info"]["network_os_model"]
146 if re.search(r"Nexus 6", cap):
147 output = "text"
148
149 body = execute_show_command(command, module, output)[0]
150
151 if output == "json":
152 password = body.get("passwd", "")
153 else:
154 password = ""
155 rp = r"VTP Password: (\S+)"
156 mo = re.search(rp, body)
157 if mo:
158 password = mo.group(1)
159
160 return str(password)
161
162
163 def main():
164 argument_spec = dict(version=dict(type="str", choices=["1", "2", "3"], required=True))
165
166 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
167
168 warnings = list()
169
170 version = module.params["version"]
171
172 existing = get_vtp_config(module)
173 end_state = existing
174
175 args = dict(version=version)
176
177 changed = False
178 proposed = dict((k, v) for k, v in args.items() if v is not None)
179 delta = dict(set(proposed.items()).difference(existing.items()))
180
181 commands = []
182 if delta:
183 commands.append(["vtp version {0}".format(version)])
184
185 cmds = flatten_list(commands)
186 if cmds:
187 if module.check_mode:
188 module.exit_json(changed=True, commands=cmds)
189 else:
190 changed = True
191 load_config(module, cmds)
192 end_state = get_vtp_config(module)
193 if "configure" in cmds:
194 cmds.pop(0)
195
196 results = {}
197 results["proposed"] = proposed
198 results["existing"] = existing
199 results["end_state"] = end_state
200 results["updates"] = cmds
201 results["changed"] = changed
202 results["warnings"] = warnings
203
204 module.exit_json(**results)
205
206
207 if __name__ == "__main__":
208 main()