Debian Codegraph @

ansible_collections/cisco/ios/plugins/modules/ios_lag_interfaces.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright 2022 Red Hat
4 # GNU General Public License v3.0+
5 # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 """
8 The module file for ios_lag_interfaces
9 """
10
11 from __future__ import absolute_import, division, print_function
12
13
14 __metaclass__ = type
15
16 DOCUMENTATION = """
17 module: ios_lag_interfaces
18 short_description: Resource module to configure LAG interfaces.
19 description:
20 This module manages properties of Link Aggregation Group on Cisco IOS
21 devices.
22 version_added: 1.0.0
23 author:
24 - Sagar Paul (@KB-perByte)
25 - Sumit Jaiswal (@justjais)
26 notes:
27 - Tested against Cisco IOSXE Version 17.3 on CML.
28 - This module works with connection C(network_cli).
29 See U(https://docs.ansible.com/ansible/latest/network/user_guide/platform_ios.html)
30 options:
31 config:
32 description: A list of link aggregation group configurations.
33 type: list
34 elements: dict
35 suboptions:
36 name:
37 description:
38 - ID of Ethernet Channel of interfaces.
39 - Refer to vendor documentation for valid port values.
40 type: str
41 required: true
42 members:
43 description:
44 - Interface options for the link aggregation group.
45 type: list
46 elements: dict
47 suboptions:
48 member:
49 description:
50 - Interface member of the link aggregation group.
51 type: str
52 mode:
53 description:
54 - Etherchannel Mode of the interface for link aggregation.
55 - On mode has to be quoted as 'on' or else pyyaml will convert
56 to True before it gets to Ansible.
57 type: str
58 choices:
59 - auto
60 - "on"
61 - desirable
62 - active
63 - passive
64 link:
65 description:
66 - Assign a link identifier used for load-balancing.
67 - Refer to vendor documentation for valid values.
68 - NOTE, parameter only supported on Cisco IOS XE platform.
69 type: int
70 running_config:
71 description:
72 - This option is used only with state I(parsed).
73 - The value of this option should be the output received from the IOS device
74 by executing the command B(show running-config | section ^interface).
75 - The state I(parsed) reads the configuration from C(running_config) option and
76 transforms it into Ansible structured data as per the resource module's argspec
77 and the value is then returned in the I(parsed) key within the result.
78 type: str
79 state:
80 description:
81 - The state the configuration should be left in
82 - The states I(rendered), I(gathered) and I(parsed) does not perform any change
83 on the device.
84 - The state I(rendered) will transform the configuration in C(config) option to
85 platform specific CLI commands which will be returned in the I(rendered) key
86 within the result. For state I(rendered) active connection to remote host is
87 not required.
88 - The state I(gathered) will fetch the running configuration from device and transform
89 it into structured data in the format as per the resource module argspec and
90 the value is returned in the I(gathered) key within the result.
91 - The state I(parsed) reads the configuration from C(running_config) option and
92 transforms it into JSON format as per the resource module parameters and the
93 value is returned in the I(parsed) key within the result. The value of C(running_config)
94 option should be the same format as the output of command I(show running-config
95 | include ip route|ipv6 route) executed on device. For state I(parsed) active
96 connection to remote host is not required.
97 type: str
98 choices:
99 - merged
100 - replaced
101 - overridden
102 - deleted
103 - rendered
104 - parsed
105 - gathered
106 default: merged
107 """
108
109 EXAMPLES = """
110 # Using merged
111 #
112 # Before state:
113 # -------------
114 #
115 # vios#show running-config | section ^interface
116 # interface Port-channel10
117 # interface GigabitEthernet0/1
118 # shutdown
119 # interface GigabitEthernet0/2
120 # shutdown
121 # interface GigabitEthernet0/3
122 # shutdown
123 # interface GigabitEthernet0/4
124 # shutdown
125
126 - name: Merge provided configuration with device configuration
127 cisco.ios.ios_lag_interfaces:
128 config:
129 - name: Port-channel10
130 members:
131 - member: GigabitEthernet0/1
132 mode: auto
133 - member: GigabitEthernet0/2
134 mode: auto
135 - name: Port-channel20
136 members:
137 - member: GigabitEthernet0/3
138 mode: "on"
139 - name: Port-channel30
140 members:
141 - member: GigabitEthernet0/4
142 mode: active
143 state: merged
144
145 # Task Output:
146 # ---------------
147
148 # commands:
149 # - interface GigabitEthernet0/1
150 # - channel-group 10 mode auto
151 # - interface GigabitEthernet0/2
152 # - channel-group 10 mode auto
153 # - interface GigabitEthernet0/3
154 # - channel-group 20 mode on
155 # - interface GigabitEthernet0/4
156 # - channel-group 30 mode active
157
158 # After state:
159 # ------------
160 #
161 # vios#show running-config | section ^interface
162 # interface Port-channel10
163 # interface Port-channel20
164 # interface Port-channel30
165 # interface GigabitEthernet0/1
166 # shutdown
167 # channel-group 10 mode auto
168 # interface GigabitEthernet0/2
169 # shutdown
170 # channel-group 10 mode auto
171 # interface GigabitEthernet0/3
172 # shutdown
173 # channel-group 20 mode on
174 # interface GigabitEthernet0/4
175 # shutdown
176 # channel-group 30 mode active
177
178 # Using overridden
179 #
180 # Before state:
181 # -------------
182 #
183 # vios#show running-config | section ^interface
184 # interface Port-channel10
185 # interface Port-channel20
186 # interface Port-channel30
187 # interface GigabitEthernet0/1
188 # shutdown
189 # channel-group 10 mode auto
190 # interface GigabitEthernet0/2
191 # shutdown
192 # channel-group 10 mode auto
193 # interface GigabitEthernet0/3
194 # shutdown
195 # channel-group 20 mode on
196 # interface GigabitEthernet0/4
197 # shutdown
198 # channel-group 30 mode active
199
200 - name: Override device configuration of all interfaces with provided configuration
201 cisco.ios.ios_lag_interfaces:
202 config:
203 - name: Port-channel20
204 members:
205 - member: GigabitEthernet0/2
206 mode: auto
207 - member: GigabitEthernet0/3
208 mode: auto
209 state: overridden
210
211 # Task Output:
212 # ---------------
213
214 # commands:
215 # - interface GigabitEthernet0/1
216 # - no channel-group 10 mode auto
217 # - interface GigabitEthernet0/2
218 # - no channel-group 10 mode auto
219 # - interface GigabitEthernet0/4
220 # - no channel-group 30 mode active
221 # - interface GigabitEthernet0/2
222 # - channel-group 20 mode auto
223 # - interface GigabitEthernet0/3
224 # - channel-group 20 mode auto
225
226 # After state:
227 # ------------
228 #
229 # vios#show running-config | section ^interface
230 # interface Port-channel10
231 # interface Port-channel20
232 # interface Port-channel30
233 # interface GigabitEthernet0/1
234 # shutdown
235 # interface GigabitEthernet0/2
236 # shutdown
237 # channel-group 20 mode auto
238 # interface GigabitEthernet0/3
239 # shutdown
240 # channel-group 20 mode auto
241 # interface GigabitEthernet0/4
242 # shutdown
243
244 # Using replaced
245 #
246 # Before state:
247 # -------------
248 #
249 # vios#show running-config | section ^interface
250 # interface Port-channel10
251 # interface Port-channel20
252 # interface Port-channel30
253 # interface GigabitEthernet0/1
254 # shutdown
255 # channel-group 10 mode auto
256 # interface GigabitEthernet0/2
257 # shutdown
258 # channel-group 10 mode auto
259 # interface GigabitEthernet0/3
260 # shutdown
261 # channel-group 20 mode on
262 # interface GigabitEthernet0/4
263 # shutdown
264 # channel-group 30 mode active
265
266 - name: Replaces device configuration of listed interfaces with provided configuration
267 cisco.ios.ios_lag_interfaces:
268 config:
269 - name: Port-channel30
270 members:
271 - member: GigabitEthernet0/3
272 mode: auto
273 state: replaced
274
275 # Task Output:
276 # ---------------
277
278 # commands:
279 # - interface GigabitEthernet0/3
280 # - channel-group 30 mode auto
281 # - interface GigabitEthernet0/4
282 # - no channel-group 30 mode active
283
284 # After state:
285 # ------------
286 #
287 # vios#show running-config | section ^interface
288 # interface Port-channel10
289 # interface Port-channel20
290 # interface Port-channel30
291 # interface GigabitEthernet0/1
292 # shutdown
293 # channel-group 10 mode auto
294 # interface GigabitEthernet0/2
295 # shutdown
296 # channel-group 10 mode auto
297 # interface GigabitEthernet0/3
298 # shutdown
299 # channel-group 30 mode auto
300 # interface GigabitEthernet0/4
301 # shutdown
302
303 # Using Deleted
304 #
305 # Before state:
306 # -------------
307 #
308 # vios#show running-config | section ^interface
309 # interface Port-channel10
310 # interface Port-channel20
311 # interface Port-channel30
312 # interface GigabitEthernet0/1
313 # shutdown
314 # channel-group 10 mode auto
315 # interface GigabitEthernet0/2
316 # shutdown
317 # channel-group 10 mode auto
318 # interface GigabitEthernet0/3
319 # shutdown
320 # channel-group 20 mode on
321 # interface GigabitEthernet0/4
322 # shutdown
323 # channel-group 30 mode active
324
325 - name: "Delete LAG attributes of given interfaces (Note: This won't delete the interface itself)"
326 cisco.ios.ios_lag_interfaces:
327 config:
328 - name: Port-channel10
329 - name: Port-channel20
330 state: deleted
331
332 # Task Output:
333 # ---------------
334
335 # commands:
336 # - interface GigabitEthernet0/1
337 # - no channel-group 10 mode auto
338 # - interface GigabitEthernet0/2
339 # - no channel-group 10 mode auto
340 # - interface GigabitEthernet0/3
341 # - no channel-group 20 mode on
342
343 # After state:
344 # -------------
345 #
346 # vios#show running-config | section ^interface
347 # interface Port-channel10
348 # interface Port-channel20
349 # interface Port-channel30
350 # interface GigabitEthernet0/1
351 # shutdown
352 # interface GigabitEthernet0/2
353 # shutdown
354 # interface GigabitEthernet0/3
355 # shutdown
356 # interface GigabitEthernet0/4
357 # shutdown
358 # channel-group 30 mode active
359
360 # Using Deleted without any config passed
361 # "(NOTE: This will delete all of configured LLDP module attributes)"
362
363 #
364 # Before state:
365 # -------------
366 #
367 # vios#show running-config | section ^interface
368 # interface Port-channel10
369 # interface Port-channel20
370 # interface Port-channel30
371 # interface GigabitEthernet0/1
372 # shutdown
373 # channel-group 10 mode auto
374 # interface GigabitEthernet0/2
375 # shutdown
376 # channel-group 10 mode auto
377 # interface GigabitEthernet0/3
378 # shutdown
379 # channel-group 20 mode on
380 # interface GigabitEthernet0/4
381 # shutdown
382 # channel-group 30 mode active
383
384 - name: "Delete all configured LAG attributes for interfaces (Note: This won't delete the interface itself)"
385 cisco.ios.ios_lag_interfaces:
386 state: deleted
387
388 # Task Output:
389 # ---------------
390
391 # commands:
392 # - interface GigabitEthernet0/1
393 # - no channel-group 10 mode auto
394 # - interface GigabitEthernet0/2
395 # - no channel-group 10 mode auto
396 # - interface GigabitEthernet0/3
397 # - no channel-group 20 mode on
398 # - interface GigabitEthernet0/4
399 # - no channel-group 30 mode active
400
401 # After state:
402 # -------------
403 #
404 # vios#show running-config | section ^interface
405 # interface Port-channel10
406 # interface Port-channel20
407 # interface Port-channel30
408 # interface GigabitEthernet0/1
409 # shutdown
410 # interface GigabitEthernet0/2
411 # shutdown
412 # interface GigabitEthernet0/3
413 # shutdown
414 # interface GigabitEthernet0/4
415 # shutdown
416
417 # Using Gathered
418
419 # Before state:
420 # -------------
421 #
422 # vios#show running-config | section ^interface
423 # interface Port-channel10
424 # interface Port-channel20
425 # interface Port-channel30
426 # interface GigabitEthernet0/1
427 # shutdown
428 # channel-group 10 mode auto
429 # interface GigabitEthernet0/2
430 # shutdown
431 # channel-group 10 mode auto
432 # interface GigabitEthernet0/3
433 # shutdown
434 # channel-group 20 mode on
435 # interface GigabitEthernet0/4
436 # shutdown
437 # channel-group 30 mode active
438
439 - name: Gather listed LAG interfaces with provided configurations
440 cisco.ios.ios_lag_interfaces:
441 config:
442 state: gathered
443
444 # Module Execution Result:
445 # ------------------------
446 #
447 # "gathered": [
448 # {
449 # "members": [
450 # {
451 # "member": "GigabitEthernet0/1",
452 # "mode": "auto"
453 # },
454 # {
455 # "member": "GigabitEthernet0/2",
456 # "mode": "auto"
457 # }
458 # ],
459 # "name": "Port-channel10"
460 # },
461 # {
462 # "members": [
463 # {
464 # "member": "GigabitEthernet0/3",
465 # "mode": "on"
466 # }
467 # ],
468 # "name": "Port-channel20"
469 # },
470 # {
471 # "members": [
472 # {
473 # "member": "GigabitEthernet0/4",
474 # "mode": "active"
475 # }
476 # ],
477 # "name": "Port-channel30"
478 # }
479 # ]
480
481 # After state:
482 # ------------
483 #
484 # vios#sh running-config | section ^interface
485 # interface Port-channel10
486 # interface Port-channel20
487 # interface Port-channel30
488 # interface GigabitEthernet0/1
489 # shutdown
490 # channel-group 10 mode auto
491 # interface GigabitEthernet0/2
492 # shutdown
493 # channel-group 10 mode auto
494 # interface GigabitEthernet0/3
495 # shutdown
496 # channel-group 20 mode on
497 # interface GigabitEthernet0/4
498 # shutdown
499 # channel-group 30 mode active
500
501 # Using Rendered
502
503 - name: Render the commands for provided configuration
504 cisco.ios.ios_lag_interfaces:
505 config:
506 - name: Port-channel11
507 members:
508 - member: GigabitEthernet0/1
509 mode: active
510 - name: Port-channel22
511 members:
512 - member: GigabitEthernet0/2
513 mode: passive
514 state: rendered
515
516 # Module Execution Result:
517 # ------------------------
518 #
519 # "rendered": [
520 # "interface GigabitEthernet0/1",
521 # "channel-group 11 mode active",
522 # "interface GigabitEthernet0/2",
523 # "channel-group 22 mode passive",
524 # ]
525
526 # Using Parsed
527
528 # File: parsed.cfg
529 # ----------------
530 #
531 # interface GigabitEthernet0/1
532 # channel-group 11 mode active
533 # interface GigabitEthernet0/2
534 # channel-group 22 mode passive
535
536 - name: Parse the commands for provided configuration
537 cisco.ios.ios_lag_interfaces:
538 running_config: "{{ lookup('file', 'parsed.cfg') }}"
539 state: parsed
540
541 # Module Execution Result:
542 # ------------------------
543 #
544 # "parsed": [
545 # {
546 # "members": [
547 # {
548 # "member": "GigabitEthernet0/1",
549 # "mode": "active"
550 # }
551 # ],
552 # "name": "Port-channel11"
553 # },
554 # {
555 # "members": [
556 # {
557 # "member": "GigabitEthernet0/2",
558 # "mode": "passive"
559 # }
560 # ],
561 # "name": "Port-channel22"
562 # }
563 # ]
564 """
565
566 RETURN = """
567 before:
568 description: The configuration prior to the module execution.
569 returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) or C(purged)
570 type: dict
571 sample: >
572 This output will always be in the same format as the
573 module argspec.
574 after:
575 description: The resulting configuration after module execution.
576 returned: when changed
577 type: dict
578 sample: >
579 This output will always be in the same format as the
580 module argspec.
581 commands:
582 description: The set of commands pushed to the remote device.
583 returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) or C(purged)
584 type: list
585 sample:
586 - interface GigabitEthernet0/1
587 - channel-group 10 mode auto
588 - channel-group 10 mode active link 20
589 rendered:
590 description: The provided configuration in the task rendered in device-native format (offline).
591 returned: when I(state) is C(rendered)
592 type: list
593 sample:
594 - interface GigabitEthernet0/2
595 - channel-group 20 mode auto
596 - channel-group 20 mode active link 60
597 gathered:
598 description: Facts about the network resource gathered from the remote device as structured data.
599 returned: when I(state) is C(gathered)
600 type: list
601 sample: >
602 This output will always be in the same format as the
603 module argspec.
604 parsed:
605 description: The device native config provided in I(running_config) option parsed into structured data as per module argspec.
606 returned: when I(state) is C(parsed)
607 type: list
608 sample: >
609 This output will always be in the same format as the
610 module argspec.
611 """
612
613 from ansible.module_utils.basic import AnsibleModule
614
615 from ansible_collections.cisco.ios.plugins.module_utils.network.ios.argspec.lag_interfaces.lag_interfaces import (
616 Lag_InterfacesArgs,
617 )
618 from ansible_collections.cisco.ios.plugins.module_utils.network.ios.config.lag_interfaces.lag_interfaces import (
619 Lag_interfaces,
620 )
621
622
623 def main():
624 """
625 Main entry point for module execution
626
627 :returns: the result form module invocation
628 """
629 module = AnsibleModule(
630 argument_spec=Lag_InterfacesArgs.argument_spec,
631 mutually_exclusive=[["config", "running_config"]],
632 required_if=[
633 ["state", "merged", ["config"]],
634 ["state", "replaced", ["config"]],
635 ["state", "overridden", ["config"]],
636 ["state", "rendered", ["config"]],
637 ["state", "parsed", ["running_config"]],
638 ],
639 supports_check_mode=True,
640 )
641
642 result = Lag_interfaces(module).execute_module()
643 module.exit_json(**result)
644
645
646 if __name__ == "__main__":
647 main()