Debian Codegraph @

ansible_collections/cisco/iosxr/plugins/modules/iosxr_interfaces.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
19 """
20 The module file for iosxr_interfaces
21 """
22
23 from __future__ import absolute_import, division, print_function
24
25
26 __metaclass__ = type
27 DOCUMENTATION = """
28 module: iosxr_interfaces
29 short_description: Resource module to configure interfaces.
30 description: This module manages the interface attributes on Cisco IOS-XR network
31 devices.
32 version_added: 1.0.0
33 author:
34 - Sumit Jaiswal (@justjais)
35 - Rohit Thakur (@rohitthakur2590)
36 notes:
37 - This module works with connection C(network_cli).
38 See U(https://docs.ansible.com/ansible/latest/network/user_guide/platform_iosxr.html)
39 - The module examples uses callback plugin (stdout_callback = yaml) to generate task
40 output in yaml format.
41 options:
42 config:
43 description: A dictionary of interface options
44 type: list
45 elements: dict
46 suboptions:
47 name:
48 description:
49 - Full name of the interface to configure in C(type + path) format. e.g. C(GigabitEthernet0/0/0/0)
50 type: str
51 required: true
52 description:
53 description:
54 - Interface description.
55 type: str
56 enabled:
57 description:
58 - Administrative state of the interface.
59 - Set the value to C(True) to administratively enable the interface or C(False)
60 to disable it.
61 type: bool
62 speed:
63 description:
64 - Configure the speed for an interface. Default is auto-negotiation when not
65 configured.
66 type: int
67 mtu:
68 description:
69 - Sets the MTU value for the interface. Applicable for Ethernet interfaces
70 only.
71 - Refer to vendor documentation for valid values.
72 type: int
73 duplex:
74 description:
75 - Configures the interface duplex mode. Default is auto-negotiation when not
76 configured.
77 type: str
78 choices:
79 - full
80 - half
81 running_config:
82 description:
83 - This option is used only with state I(parsed).
84 - The value of this option should be the output received from the IOS-XR device
85 by executing the command B(show running-config interface).
86 - The state I(parsed) reads the configuration from C(running_config) option and
87 transforms it into Ansible structured data as per the resource module's argspec
88 and the value is then returned in the I(parsed) key within the result.
89 type: str
90 state:
91 choices:
92 - merged
93 - parsed
94 - deleted
95 - replaced
96 - rendered
97 - gathered
98 - overridden
99 default: merged
100 description:
101 - The state of the configuration after module completion
102 type: str
103 """
104
105 EXAMPLES = """
106 # Using merged
107
108 # Before state:
109 # -------------
110 #
111 # viosxr#show running-config interface
112 # interface Loopback888
113 # !
114 # interface Loopback999
115 # !
116 # interface MgmtEth0/RP0/CPU0/0
117 # ipv4 address dhcp
118 # !
119
120 - name: Configure Ethernet interfaces
121 cisco.iosxr.iosxr_interfaces:
122 config:
123 - name: GigabitEthernet0/0/0/2
124 description: Configured by Ansible
125 enabled: true
126 - name: GigabitEthernet0/0/0/3
127 description: Configured by Ansible Network
128 enabled: false
129 duplex: full
130 state: merged
131
132 # Task Output
133 # -----------
134 #
135 # before:
136 # - enabled: true
137 # name: Loopback888
138 # - enabled: true
139 # name: Loopback999
140 # commands:
141 # - interface GigabitEthernet0/0/0/2
142 # - description Configured by Ansible
143 # - no shutdown
144 # - interface GigabitEthernet0/0/0/3
145 # - description Configured by Ansible Network
146 # - duplex full
147 # - shutdown
148 # after:
149 # - enabled: true
150 # name: Loopback888
151 # - enabled: true
152 # name: Loopback999
153 # - description: Configured by Ansible
154 # enabled: true
155 # name: GigabitEthernet0/0/0/2
156 # - description: Configured by Ansible Network
157 # duplex: full
158 # enabled: false
159 # name: GigabitEthernet0/0/0/3
160
161 # After state:
162 # ------------
163 #
164 # viosxr#show running-config interface
165 # interface Loopback888
166 # !
167 # interface Loopback999
168 # !
169 # interface MgmtEth0/RP0/CPU0/0
170 # ipv4 address dhcp
171 # !
172 # interface preconfigure GigabitEthernet0/0/0/2
173 # description Configured by Ansible
174 # !
175 # interface preconfigure GigabitEthernet0/0/0/3
176 # description Configured by Ansible Network
177 # duplex full
178 # shutdown
179 # !
180
181 # Using replaced
182
183 # Before state:
184 # ------------
185 #
186 # viosxr#show running-config interface
187 # interface Loopback888
188 # !
189 # interface Loopback999
190 # !
191 # interface MgmtEth0/RP0/CPU0/0
192 # ipv4 address dhcp
193 # !
194 # interface preconfigure GigabitEthernet0/0/0/2
195 # description Configured by Ansible
196 # !
197 # interface preconfigure GigabitEthernet0/0/0/3
198 # description Configured by Ansible Network
199 # duplex full
200 # shutdown
201 # !
202
203 - name: Replace their existing configuration per interface
204 cisco.iosxr.iosxr_interfaces:
205 config:
206 - name: GigabitEthernet0/0/0/2
207 description: Configured by Ansible
208 enabled: true
209 mtu: 2000
210 - name: GigabitEthernet0/0/0/3
211 description: Configured by Ansible Network
212 enabled: false
213 duplex: auto
214 state: replaced
215
216 # Task Output
217 # -----------
218 #
219 # before:
220 # - enabled: true
221 # name: Loopback888
222 # - enabled: true
223 # name: Loopback999
224 # - description: Configured by Ansible
225 # enabled: true
226 # name: GigabitEthernet0/0/0/2
227 # - description: Configured by Ansible Network
228 # duplex: full
229 # enabled: false
230 # name: GigabitEthernet0/0/0/3
231 # commands:
232 # - interface GigabitEthernet0/0/0/2
233 # - mtu 2000
234 # - interface GigabitEthernet0/0/0/3
235 # - duplex half
236 # after:
237 # - enabled: true
238 # name: Loopback888
239 # - enabled: true
240 # name: Loopback999
241 # - description: Configured by Ansible
242 # enabled: true
243 # mtu: 2000
244 # name: GigabitEthernet0/0/0/2
245 # - description: Configured by Ansible Network
246 # duplex: half
247 # enabled: false
248 # name: GigabitEthernet0/0/0/3
249
250 # After state:
251 # ------------
252 #
253 # viosxr#show running-config interface
254 # interface Loopback888
255 # !
256 # interface Loopback999
257 # !
258 # interface MgmtEth0/RP0/CPU0/0
259 # ipv4 address dhcp
260 # !
261 # interface preconfigure GigabitEthernet0/0/0/2
262 # description Configured by Ansible
263 # mtu 2000
264 # !
265 # interface preconfigure GigabitEthernet0/0/0/3
266 # description Configured by Ansible Network
267 # duplex half
268 # shutdown
269 # !
270
271 # Using overridden
272
273 # Before state:
274 # ------------
275 #
276 # viosxr#show running-config interface
277 # interface Loopback888
278 # !
279 # interface Loopback999
280 # !
281 # interface MgmtEth0/RP0/CPU0/0
282 # ipv4 address dhcp
283 # !
284 # interface preconfigure GigabitEthernet0/0/0/2
285 # description Configured by Ansible
286 # mtu 2000
287 # !
288 # interface preconfigure GigabitEthernet0/0/0/3
289 # description Configured by Ansible Network
290 # duplex half
291 # shutdown
292 # !
293
294 - name: Override interfaces configuration
295 cisco.iosxr.iosxr_interfaces:
296 config:
297 - name: GigabitEthernet0/0/0/2
298 description: Configured by Ansible
299 enabled: true
300 duplex: auto
301 - name: GigabitEthernet0/0/0/3
302 description: Configured by Ansible Network
303 enabled: false
304 speed: 1000
305 state: overridden
306
307 # Task Output
308 # -----------
309 #
310 # before:
311 # - enabled: true
312 # name: Loopback888
313 # - enabled: true
314 # name: Loopback999
315 # - description: Configured by Ansible
316 # enabled: true
317 # mtu: 2000
318 # name: GigabitEthernet0/0/0/2
319 # - description: Configured by Ansible Network
320 # duplex: half
321 # enabled: false
322 # name: GigabitEthernet0/0/0/3
323 # commands:
324 # - interface GigabitEthernet0/0/0/2
325 # - no mtu
326 # - duplex half
327 # - interface GigabitEthernet0/0/0/3
328 # - no description
329 # - no shutdown
330 # - no duplex
331 # after:
332 # - enabled: true
333 # name: Loopback888
334 # - enabled: true
335 # name: Loopback999
336 # - description: Configured by Ansible
337 # duplex: half
338 # enabled: true
339 # name: GigabitEthernet0/0/0/2
340 # - enabled: true
341 # name: GigabitEthernet0/0/0/3
342
343 # After state:
344 # ------------
345 #
346 # viosxr#show running-config interface
347 # interface Loopback888
348 # !
349 # interface Loopback999
350 # !
351 # interface MgmtEth0/RP0/CPU0/0
352 # ipv4 address dhcp
353 # !
354 # interface preconfigure GigabitEthernet0/0/0/2
355 # description Configured by Ansible
356 # duplex half
357 # !
358 # interface preconfigure GigabitEthernet0/0/0/3
359 # !
360
361 # Using deleted
362
363 # Before state:
364 # ------------
365 #
366 # viosxr#show running-config interface
367 # interface Loopback888
368 # !
369 # interface Loopback999
370 # !
371 # interface MgmtEth0/RP0/CPU0/0
372 # ipv4 address dhcp
373 # !
374 # interface preconfigure GigabitEthernet0/0/0/2
375 # description Configured by Ansible
376 # duplex half
377 # !
378 # interface preconfigure GigabitEthernet0/0/0/3
379 # !
380
381 - name: Delete interfaces arguments
382 cisco.iosxr.iosxr_interfaces:
383 config:
384 - name: GigabitEthernet0/0/0/2
385 - name: GigabitEthernet0/0/0/3
386 state: deleted
387
388 # Task Output
389 # -----------
390 #
391 # before:
392 # - enabled: true
393 # name: Loopback888
394 # - enabled: true
395 # name: Loopback999
396 # - description: Configured by Ansible
397 # duplex: half
398 # enabled: true
399 # name: GigabitEthernet0/0/0/2
400 # - enabled: true
401 # name: GigabitEthernet0/0/0/3
402 # commands:
403 # - interface GigabitEthernet0/0/0/2
404 # - no description
405 # - no duplex
406 # after:
407 # - enabled: true
408 # name: Loopback888
409 # - enabled: true
410 # name: Loopback999
411 # - enabled: true
412 # name: GigabitEthernet0/0/0/2
413 # - enabled: true
414 # name: GigabitEthernet0/0/0/3
415
416 # After state:
417 # ------------
418 #
419 # viosxr#show running-config interface
420 # interface Loopback888
421 # !
422 # interface Loopback999
423 # !
424 # interface MgmtEth0/RP0/CPU0/0
425 # ipv4 address dhcp
426 # !
427 # interface preconfigure GigabitEthernet0/0/0/2
428 # !
429 # interface preconfigure GigabitEthernet0/0/0/3
430 # !
431
432 # Using parsed
433
434 # File: parsed.cfg
435 # ----------------
436 #
437 # interface Loopback888
438 # description test for ansible
439 # shutdown
440 # !
441 # interface MgmtEth0/0/CPU0/0
442 # ipv4 address 10.8.38.70 255.255.255.0
443 # !
444 # interface GigabitEthernet0/0/0/0
445 # description Configured and Merged by Ansible-Network
446 # mtu 110
447 # ipv4 address 172.31.1.1 255.255.255.0
448 # duplex half
449 # !
450 # interface GigabitEthernet0/0/0/3
451 # shutdown
452 # !
453 # interface GigabitEthernet0/0/0/4
454 # shutdown
455 # !
456
457 # - name: Parse provided configuration
458 # cisco.iosxr.iosxr_interfaces:
459 # running_config: "{{ lookup('file', './parsed.cfg') }}"
460 # state: parsed
461
462 # Task Output
463 # -----------
464 #
465 # parsed:
466 # - name: MgmtEth0/RP0/CPU0/0
467 # - access_groups:
468 # - acls:
469 # - direction: in
470 # name: acl_1
471 # - direction: out
472 # name: acl_2
473 # afi: ipv4
474 # - acls:
475 # - direction: in
476 # name: acl6_1
477 # - direction: out
478 # name: acl6_2
479 # afi: ipv6
480 # name: GigabitEthernet0/0/0/0
481 # - access_groups:
482 # - acls:
483 # - direction: out
484 # name: acl_1
485 # afi: ipv4
486 # name: GigabitEthernet0/0/0/1
487
488
489 # Using rendered
490
491 - name: Render platform specific commands from task input using rendered state
492 cisco.iosxr.iosxr_interfaces:
493 config:
494 - name: GigabitEthernet0/0/0/0
495 description: Configured and Merged by Ansible-Network
496 mtu: 110
497 enabled: true
498 duplex: half
499 - name: GigabitEthernet0/0/0/1
500 description: Configured and Merged by Ansible-Network
501 mtu: 2800
502 enabled: false
503 speed: 100
504 duplex: full
505 state: rendered
506
507 # Task Output
508 # -----------
509 #
510 # rendered:
511 # - interface GigabitEthernet0/0/0/0
512 # - description Configured and Merged by Ansible-Network
513 # - mtu 110
514 # - duplex half
515 # - no shutdown
516 # - interface GigabitEthernet0/0/0/1
517 # - description Configured and Merged by Ansible-Network
518 # - mtu 2800
519 # - speed 100
520 # - duplex full
521 # - shutdown
522
523
524 # Using gathered
525
526 # Before state:
527 # ------------
528 #
529 # RP/0/0/CPU0:an-iosxr-02#show running-config interface
530 # interface Loopback888
531 # description test for ansible
532 # shutdown
533 # !
534 # interface MgmtEth0/0/CPU0/0
535 # ipv4 address 10.8.38.70 255.255.255.0
536 # !
537 # interface GigabitEthernet0/0/0/0
538 # description Configured and Merged by Ansible-Network
539 # mtu 110
540 # ipv4 address 172.31.1.1 255.255.255.0
541 # duplex half
542 # !
543 # interface GigabitEthernet0/0/0/3
544 # shutdown
545 # !
546 # interface GigabitEthernet0/0/0/4
547 # shutdown
548 # !
549
550 - name: Gather facts for interfaces
551 cisco.iosxr.iosxr_interfaces:
552 config:
553 state: gathered
554
555 # Task Output
556 # -----------
557 #
558 # gathered:
559 # - description: test for ansible
560 # enabled: false
561 # name: Loopback888
562 # - description: Configured and Merged by Ansible-Network
563 # duplex: half
564 # enabled: true
565 # mtu: 110
566 # name: GigabitEthernet0/0/0/0
567 # - enabled: false
568 # name: GigabitEthernet0/0/0/3
569 # - enabled: false
570 # name: GigabitEthernet0/0/0/4
571 """
572
573 RETURN = """
574 before:
575 description: The configuration as structured data prior to module invocation.
576 returned: always
577 type: list
578 sample: The configuration returned will always be in the same format of the parameters above.
579 after:
580 description: The configuration as structured data after module completion.
581 returned: when changed
582 type: list
583 sample: The configuration returned will always be in the same format of the parameters above.
584 commands:
585 description: The set of commands pushed to the remote device
586 returned: always
587 type: list
588 sample: ['interface GigabitEthernet0/0/0/2', 'description: Configured by Ansible', 'shutdown']
589 """
590
591 from ansible.module_utils.basic import AnsibleModule
592
593 from ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.argspec.interfaces.interfaces import (
594 InterfacesArgs,
595 )
596 from ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.config.interfaces.interfaces import (
597 Interfaces,
598 )
599
600
601 def main():
602 """
603 Main entry point for module execution
604 :returns: the result form module invocation
605 """
606 required_if = [
607 ("state", "merged", ("config",)),
608 ("state", "replaced", ("config",)),
609 ("state", "rendered", ("config",)),
610 ("state", "overridden", ("config",)),
611 ("state", "parsed", ("running_config",)),
612 ]
613 mutually_exclusive = [("config", "running_config")]
614 module = AnsibleModule(
615 argument_spec=InterfacesArgs.argument_spec,
616 required_if=required_if,
617 supports_check_mode=True,
618 mutually_exclusive=mutually_exclusive,
619 )
620
621 result = Interfaces(module).execute_module()
622 module.exit_json(**result)
623
624
625 if __name__ == "__main__":
626 main()