Debian Codegraph @

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