Debian Codegraph @

ansible_collections/cisco/nxos/plugins/modules/nxos_vrf_af.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_vrf_af
26 extends_documentation_fragment:
27 - cisco.nxos.nxos
28 short_description: Manages VRF AF.
29 description:
30 - Manages VRF AF
31 version_added: 1.0.0
32 author: Gabriele Gerbino (@GGabriele)
33 notes:
34 - Tested against NXOSv 7.3.(0)D1(1) on VIRL
35 - Unsupported for Cisco MDS
36 - Default, where supported, restores params default value.
37 - In case of C(state=absent) the address-family configuration will be absent. Therefore
38 the options C(route_target_both_auto_evpn) and C(route_targets) are ignored.
39 options:
40 vrf:
41 description:
42 - Name of the VRF.
43 required: true
44 type: str
45 afi:
46 description:
47 - Address-Family Identifier (AFI).
48 required: true
49 choices:
50 - ipv4
51 - ipv6
52 type: str
53 route_target_both_auto_evpn:
54 description:
55 - Enable/Disable the EVPN route-target 'auto' setting for both import and export
56 target communities.
57 type: bool
58 route_targets:
59 description:
60 - Specify the route-targets which should be imported and/or exported under the
61 AF. This argument accepts a list of dicts that specify the route-target, the
62 direction (import|export|both) and state of each route-target. Default direction
63 is C(direction=both). See examples.
64 suboptions:
65 rt:
66 description:
67 - Defines the route-target itself
68 required: true
69 type: str
70 direction:
71 description:
72 - Indicates the direction of the route-target (import|export|both)
73 choices:
74 - import
75 - export
76 - both
77 default: both
78 type: str
79 state:
80 description:
81 - Determines whether the route-target with the given direction should be present
82 or not on the device.
83 choices:
84 - present
85 - absent
86 default: present
87 type: str
88 elements: dict
89 type: list
90 state:
91 description:
92 - Determines whether the config should be present or not on the device.
93 default: present
94 choices:
95 - present
96 - absent
97 type: str
98 """
99
100 EXAMPLES = """
101 - cisco.nxos.nxos_vrf_af:
102 vrf: ntc
103 afi: ipv4
104 route_target_both_auto_evpn: true
105 state: present
106 - cisco.nxos.nxos_vrf_af:
107 vrf: ntc
108 afi: ipv4
109 route_targets:
110 - rt: '65000:1000'
111 direction: import
112 - rt: '65001:1000'
113 direction: import
114 - cisco.nxos.nxos_vrf_af:
115 vrf: ntc
116 afi: ipv4
117 route_targets:
118 - rt: '65000:1000'
119 direction: import
120 - rt: '65001:1000'
121 state: absent
122 - cisco.nxos.nxos_vrf_af:
123 vrf: ntc
124 afi: ipv4
125 route_targets:
126 - rt: '65000:1000'
127 direction: export
128 - rt: '65001:1000'
129 direction: export
130 - cisco.nxos.nxos_vrf_af:
131 vrf: ntc
132 afi: ipv4
133 route_targets:
134 - rt: '65000:1000'
135 direction: export
136 state: absent
137 - cisco.nxos.nxos_vrf_af:
138 vrf: ntc
139 afi: ipv4
140 route_targets:
141 - rt: '65000:1000'
142 direction: both
143 state: present
144 - rt: '65001:1000'
145 direction: import
146 state: present
147 - rt: '65002:1000'
148 direction: both
149 state: absent
150 """
151
152 RETURN = """
153 commands:
154 description: commands sent to the device
155 returned: always
156 type: list
157 sample: ["vrf context ntc", "address-family ipv4 unicast"]
158 """
159 import re
160
161 from ansible.module_utils.basic import AnsibleModule
162 from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.config import (
163 NetworkConfig,
164 )
165
166 from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import (
167 get_capabilities,
168 get_config,
169 load_config,
170 )
171
172
173 def match_current_rt(rt, direction, current, rt_commands):
174 command = "route-target %s %s" % (direction, rt.get("rt"))
175 match = re.findall(command, current, re.M)
176 want = bool(rt.get("state") != "absent")
177 if not match and want:
178 rt_commands.append(command)
179 elif match and not want:
180 rt_commands.append("no %s" % command)
181 return rt_commands
182
183
184 def main():
185 argument_spec = dict(
186 vrf=dict(required=True),
187 afi=dict(required=True, choices=["ipv4", "ipv6"]),
188 route_target_both_auto_evpn=dict(required=False, type="bool"),
189 state=dict(choices=["present", "absent"], default="present"),
190 route_targets=dict(
191 type="list",
192 elements="dict",
193 options=dict(
194 rt=dict(type="str", required=True),
195 direction=dict(choices=["import", "export", "both"], default="both"),
196 state=dict(choices=["present", "absent"], default="present"),
197 ),
198 ),
199 )
200
201 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
202
203 warnings = list()
204
205 result = {"changed": False, "warnings": warnings}
206
207 config_text = get_config(module)
208 config = NetworkConfig(indent=2, contents=config_text)
209
210 path = [
211 "vrf context %s" % module.params["vrf"],
212 "address-family %s unicast" % module.params["afi"],
213 ]
214
215 try:
216 current = config.get_block_config(path)
217 except ValueError:
218 current = None
219
220 commands = list()
221 if current and module.params["state"] == "absent":
222 commands.append("no address-family %s unicast" % module.params["afi"])
223
224 elif module.params["state"] == "present":
225 rt_commands = list()
226
227 if not current:
228 commands.append("address-family %s unicast" % module.params["afi"])
229 current = ""
230
231 have_auto_evpn = "route-target both auto evpn" in current
232 if module.params["route_target_both_auto_evpn"] is not None:
233 want_auto_evpn = bool(module.params["route_target_both_auto_evpn"])
234 if want_auto_evpn and not have_auto_evpn:
235 commands.append("route-target both auto evpn")
236 elif have_auto_evpn and not want_auto_evpn:
237 commands.append("no route-target both auto evpn")
238
239 if module.params["route_targets"] is not None:
240 for rt in module.params["route_targets"]:
241 if rt.get("direction") == "both" or not rt.get("direction"):
242 platform = get_capabilities(module)["device_info"]["network_os_platform"]
243 if platform.startswith("N9K") and rt.get("rt") == "auto":
244 rt_commands = match_current_rt(rt, "both", current, rt_commands)
245 else:
246 rt_commands = match_current_rt(
247 rt,
248 "import",
249 current,
250 rt_commands,
251 )
252 rt_commands = match_current_rt(
253 rt,
254 "export",
255 current,
256 rt_commands,
257 )
258 else:
259 rt_commands = match_current_rt(
260 rt,
261 rt.get("direction"),
262 current,
263 rt_commands,
264 )
265
266 if rt_commands:
267 commands.extend(rt_commands)
268
269 if commands and current:
270 commands.insert(0, "address-family %s unicast" % module.params["afi"])
271
272 if commands:
273 commands.insert(0, "vrf context %s" % module.params["vrf"])
274 if not module.check_mode:
275 load_config(module, commands)
276 result["changed"] = True
277
278 result["commands"] = commands
279
280 module.exit_json(**result)
281
282
283 if __name__ == "__main__":
284 main()