Debian Codegraph @

ansible_collections/community/routeros/plugins/modules/api_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2022, Felix Fontein (@felixfontein) <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import (absolute_import, division, print_function)
9 __metaclass__ = type
10
11 DOCUMENTATION = r"""
12 module: api_info
13 author:
14 - "Felix Fontein (@felixfontein)"
15 short_description: Retrieve information from API
16 version_added: 2.2.0
17 description:
18 - Allows to retrieve information for a path using the API.
19 - This can be used to backup a path to restore it with the M(community.routeros.api_modify) module.
20 - Entries are normalized, dynamic and builtin entries are not returned. Use the O(handle_disabled) and O(hide_defaults)
21 options to control normalization, the O(include_dynamic) and O(include_builtin) options to also return dynamic resp. builtin
22 entries, and use O(unfiltered) to return all fields including counters.
23 - B(Note) that this module is still heavily in development, and only supports B(some) paths. If you want to support new
24 paths, or think you found problems with existing paths, please first L(create an issue in the community.routeros Issue
25 Tracker,https://github.com/ansible-collections/community.routeros/issues/).
26 extends_documentation_fragment:
27 - community.routeros.api
28 - community.routeros.api.restrict
29 - community.routeros.attributes
30 - community.routeros.attributes.actiongroup_api
31 - community.routeros.attributes.idempotent_not_modify_state
32 - community.routeros.attributes.info_module
33 attributes:
34 platform:
35 support: full
36 platforms: RouterOS
37 options:
38 path:
39 description:
40 - Path to query.
41 - An example value is V(ip address). This is equivalent to running C(/ip address print) in the RouterOS CLI.
42 required: true
43 type: str
44 choices:
45 # BEGIN PATH LIST
46 - app
47 - app settings
48 - caps-man aaa
49 - caps-man access-list
50 - caps-man actual-interface-configuration
51 - caps-man channel
52 - caps-man configuration
53 - caps-man datapath
54 - caps-man interface
55 - caps-man manager
56 - caps-man manager interface
57 - caps-man provisioning
58 - caps-man rates
59 - caps-man security
60 - certificate
61 - certificate crl
62 - certificate scep-server
63 - certificate scep-server ra
64 - certificate settings
65 - console settings
66 - container
67 - container config
68 - container envs
69 - container mounts
70 - disk
71 - disk btrfs filesystem
72 - disk btrfs subvolume
73 - disk btrfs transfer
74 - disk settings
75 - dude
76 - dude agent
77 - dude device
78 - dude device-type
79 - dude notification
80 - dude probe
81 - dude ros address
82 - dude ros arp
83 - dude ros health
84 - dude ros interface
85 - dude ros lease
86 - dude ros neighbor
87 - dude ros queue
88 - dude ros resource
89 - dude ros route
90 - dude ros routerboard
91 - dude service
92 - file
93 - file rsync-daemon
94 - file sync
95 - interface
96 - interface 6to4
97 - interface amt
98 - interface bonding
99 - interface bridge
100 - interface bridge calea
101 - interface bridge filter
102 - interface bridge host
103 - interface bridge mdb
104 - interface bridge mlag
105 - interface bridge msti
106 - interface bridge nat
107 - interface bridge port
108 - interface bridge port mst-override
109 - interface bridge port-controller
110 - interface bridge port-controller device
111 - interface bridge port-controller port
112 - interface bridge port-extender
113 - interface bridge settings
114 - interface bridge vlan
115 - interface detect-internet
116 - interface dot1x client
117 - interface dot1x server
118 - interface dot1x server active
119 - interface eoip
120 - interface eoipv6
121 - interface ethernet
122 - interface ethernet poe
123 - interface ethernet switch
124 - interface ethernet switch acl
125 - interface ethernet switch acl policer
126 - interface ethernet switch dscp-qos-map
127 - interface ethernet switch dscp-to-dscp
128 - interface ethernet switch egress-vlan-tag
129 - interface ethernet switch egress-vlan-translation
130 - interface ethernet switch host
131 - interface ethernet switch ingress-port-policer
132 - interface ethernet switch ingress-vlan-translation
133 - interface ethernet switch l3hw-settings
134 - interface ethernet switch l3hw-settings advanced
135 - interface ethernet switch mac-based-vlan
136 - interface ethernet switch multicast-fdb
137 - interface ethernet switch one2one-vlan-switching
138 - interface ethernet switch policer-qos-map
139 - interface ethernet switch port
140 - interface ethernet switch port-isolation
141 - interface ethernet switch port-leakage
142 - interface ethernet switch protocol-based-vlan
143 - interface ethernet switch qos map
144 - interface ethernet switch qos map ip
145 - interface ethernet switch qos map vlan
146 - interface ethernet switch qos port
147 - interface ethernet switch qos priority-flow-control
148 - interface ethernet switch qos profile
149 - interface ethernet switch qos settings
150 - interface ethernet switch qos tx-manager
151 - interface ethernet switch qos tx-manager queue
152 - interface ethernet switch qos-group
153 - interface ethernet switch reserved-fdb
154 - interface ethernet switch rule
155 - interface ethernet switch shaper
156 - interface ethernet switch stats
157 - interface ethernet switch trunk
158 - interface ethernet switch unicast-fdb
159 - interface ethernet switch vlan
160 - interface gre
161 - interface gre6
162 - interface ipip
163 - interface ipipv6
164 - interface l2tp-client
165 - interface l2tp-ether
166 - interface l2tp-server
167 - interface l2tp-server server
168 - interface list
169 - interface list member
170 - interface lte
171 - interface lte apn
172 - interface lte settings
173 - interface macsec
174 - interface macsec profile
175 - interface macvlan
176 - interface mesh
177 - interface mesh port
178 - interface ovpn-client
179 - interface ovpn-server
180 - interface ovpn-server server
181 - interface ppp-client
182 - interface ppp-server
183 - interface pppoe-client
184 - interface pppoe-server
185 - interface pppoe-server server
186 - interface pptp-client
187 - interface pptp-server
188 - interface pptp-server server
189 - interface sstp-client
190 - interface sstp-server
191 - interface sstp-server server
192 - interface veth
193 - interface vlan
194 - interface vpls
195 - interface vrrp
196 - interface vxlan
197 - interface vxlan vteps
198 - interface wifi
199 - interface wifi aaa
200 - interface wifi access-list
201 - interface wifi cap
202 - interface wifi capsman
203 - interface wifi channel
204 - interface wifi configuration
205 - interface wifi datapath
206 - interface wifi interworking
207 - interface wifi network
208 - interface wifi network radio
209 - interface wifi provisioning
210 - interface wifi radio settings
211 - interface wifi security
212 - interface wifi security multi-passphrase
213 - interface wifi steering
214 - interface wifi steering neighbor-group
215 - interface wifiwave2
216 - interface wifiwave2 aaa
217 - interface wifiwave2 access-list
218 - interface wifiwave2 cap
219 - interface wifiwave2 capsman
220 - interface wifiwave2 channel
221 - interface wifiwave2 configuration
222 - interface wifiwave2 datapath
223 - interface wifiwave2 interworking
224 - interface wifiwave2 provisioning
225 - interface wifiwave2 security
226 - interface wifiwave2 steering
227 - interface wireguard
228 - interface wireguard peers
229 - interface wireless
230 - interface wireless access-list
231 - interface wireless align
232 - interface wireless cap
233 - interface wireless channels
234 - interface wireless connect-list
235 - interface wireless interworking-profiles
236 - interface wireless manual-tx-power-table
237 - interface wireless nstreme
238 - interface wireless nstreme-dual
239 - interface wireless security-profiles
240 - interface wireless sniffer
241 - interface wireless snooper
242 - interface wireless wds
243 - iot bluetooth
244 - iot bluetooth advertisers
245 - iot bluetooth advertisers ad-structures
246 - iot bluetooth peripheral-devices
247 - iot bluetooth scanners
248 - iot bluetooth whitelist
249 - iot lora
250 - iot lora channels
251 - iot lora joineui
252 - iot lora netid
253 - iot lora radios
254 - iot lora servers
255 - iot lora traffic options
256 - iot modbus
257 - iot modbus security-rules
258 - iot mqtt brokers
259 - iot mqtt subscriptions
260 - ip accounting
261 - ip accounting web-access
262 - ip address
263 - ip arp
264 - ip cloud
265 - ip cloud advanced
266 - ip cloud back-to-home-file
267 - ip cloud back-to-home-file settings
268 - ip cloud back-to-home-user
269 - ip cloud back-to-home-users
270 - ip dhcp-client
271 - ip dhcp-client option
272 - ip dhcp-relay
273 - ip dhcp-server
274 - ip dhcp-server alert
275 - ip dhcp-server config
276 - ip dhcp-server lease
277 - ip dhcp-server matcher
278 - ip dhcp-server network
279 - ip dhcp-server option
280 - ip dhcp-server option sets
281 - ip dns
282 - ip dns adlist
283 - ip dns forwarders
284 - ip dns static
285 - ip firewall address-list
286 - ip firewall calea
287 - ip firewall connection tracking
288 - ip firewall filter
289 - ip firewall layer7-protocol
290 - ip firewall mangle
291 - ip firewall nat
292 - ip firewall raw
293 - ip firewall service-port
294 - ip hotspot
295 - ip hotspot active
296 - ip hotspot ip-binding
297 - ip hotspot profile
298 - ip hotspot service-port
299 - ip hotspot user
300 - ip hotspot user profile
301 - ip hotspot walled-garden
302 - ip hotspot walled-garden ip
303 - ip ipsec active-peers
304 - ip ipsec identity
305 - ip ipsec key
306 - ip ipsec key psk
307 - ip ipsec key qkd
308 - ip ipsec key rsa
309 - ip ipsec mode-config
310 - ip ipsec peer
311 - ip ipsec policy
312 - ip ipsec policy group
313 - ip ipsec profile
314 - ip ipsec proposal
315 - ip ipsec settings
316 - ip kid-control
317 - ip kid-control device
318 - ip media
319 - ip media settings
320 - ip nat-pmp
321 - ip nat-pmp interfaces
322 - ip neighbor discovery-settings
323 - ip packing
324 - ip pool
325 - ip pool used
326 - ip proxy
327 - ip proxy access
328 - ip proxy cache
329 - ip proxy cache-contents
330 - ip proxy connections
331 - ip proxy direct
332 - ip reverse-proxy
333 - ip route
334 - ip route rule
335 - ip route vrf
336 - ip service
337 - ip service webserver
338 - ip settings
339 - ip smb
340 - ip smb shares
341 - ip smb users
342 - ip socks
343 - ip socks access
344 - ip socks connections
345 - ip socks users
346 - ip socksify
347 - ip ssh
348 - ip tftp
349 - ip tftp settings
350 - ip traffic-flow
351 - ip traffic-flow ipfix
352 - ip traffic-flow target
353 - ip upnp
354 - ip upnp interfaces
355 - ip vrf
356 - ipv6 address
357 - ipv6 dhcp-client
358 - ipv6 dhcp-client option
359 - ipv6 dhcp-relay
360 - ipv6 dhcp-relay option
361 - ipv6 dhcp-server
362 - ipv6 dhcp-server binding
363 - ipv6 dhcp-server option
364 - ipv6 dhcp-server option sets
365 - ipv6 firewall address-list
366 - ipv6 firewall filter
367 - ipv6 firewall mangle
368 - ipv6 firewall nat
369 - ipv6 firewall raw
370 - ipv6 nd
371 - ipv6 nd prefix
372 - ipv6 nd prefix default
373 - ipv6 nd proxy
374 - ipv6 neighbor
375 - ipv6 pool
376 - ipv6 route
377 - ipv6 settings
378 - lcd
379 - lcd interface
380 - lcd interface pages
381 - lcd pin
382 - lcd screen
383 - lora
384 - lora channels
385 - lora joineui
386 - lora netid
387 - lora radios
388 - lora servers
389 - lora traffic options
390 - mpls
391 - mpls interface
392 - mpls ldp
393 - mpls ldp accept-filter
394 - mpls ldp advertise-filter
395 - mpls ldp interface
396 - mpls ldp local-mapping
397 - mpls ldp neighbor
398 - mpls ldp remote-mapping
399 - mpls mangle
400 - mpls settings
401 - mpls traffic-eng interface
402 - mpls traffic-eng path
403 - mpls traffic-eng tunnel
404 - openflow
405 - openflow port
406 - partitions
407 - port
408 - port firmware
409 - port remote-access
410 - ppp aaa
411 - ppp l2tp-secret
412 - ppp profile
413 - ppp secret
414 - queue interface
415 - queue simple
416 - queue tree
417 - queue type
418 - radius
419 - radius incoming
420 - routing bfd configuration
421 - routing bgp aggregate
422 - routing bgp connection
423 - routing bgp evpn
424 - routing bgp instance
425 - routing bgp network
426 - routing bgp peer
427 - routing bgp template
428 - routing bgp vpls
429 - routing bgp vpn
430 - routing fantasy
431 - routing filter
432 - routing filter community-ext-list
433 - routing filter community-large-list
434 - routing filter community-list
435 - routing filter num-list
436 - routing filter rule
437 - routing filter select-rule
438 - routing gmp
439 - routing id
440 - routing igmp-proxy
441 - routing igmp-proxy interface
442 - routing igmp-proxy mfc
443 - routing isis instance
444 - routing isis interface
445 - routing isis interface-template
446 - routing isis lsp
447 - routing isis neighbor
448 - routing mme
449 - routing ospf area
450 - routing ospf area range
451 - routing ospf instance
452 - routing ospf interface-template
453 - routing ospf neighbor
454 - routing ospf static-neighbor
455 - routing pimsm bsr candidate
456 - routing pimsm bsr rp-candidate
457 - routing pimsm igmp-interface-template
458 - routing pimsm instance
459 - routing pimsm interface-template
460 - routing pimsm static-rp
461 - routing rip
462 - routing rip instance
463 - routing rip interface-template
464 - routing rip keys
465 - routing rip static-neighbor
466 - routing ripng
467 - routing route
468 - routing route rule
469 - routing rpki
470 - routing rule
471 - routing settings
472 - routing table
473 - rsync-daemon
474 - snmp
475 - snmp community
476 - special-login
477 - system clock
478 - system clock manual
479 - system console
480 - system console screen
481 - system gps
482 - system hardware
483 - system health
484 - system health settings
485 - system identity
486 - system leds
487 - system leds settings
488 - system logging
489 - system logging action
490 - system note
491 - system ntp client
492 - system ntp client servers
493 - system ntp key
494 - system ntp server
495 - system package local-update
496 - system package local-update mirror
497 - system package local-update update-package-source
498 - system package update
499 - system resource hardware usb-settings
500 - system resource irq
501 - system resource irq rps
502 - system resource usb
503 - system resource usb settings
504 - system routerboard mode-button
505 - system routerboard reset-button
506 - system routerboard settings
507 - system routerboard usb
508 - system routerboard wps-button
509 - system scheduler
510 - system script
511 - system script environment
512 - system script job
513 - system swos
514 - system upgrade
515 - system upgrade mirror
516 - system upgrade upgrade-package-source
517 - system ups
518 - system watchdog
519 - task
520 - tool bandwidth-server
521 - tool calea
522 - tool e-mail
523 - tool graphing
524 - tool graphing interface
525 - tool graphing queue
526 - tool graphing resource
527 - tool mac-server
528 - tool mac-server mac-winbox
529 - tool mac-server ping
530 - tool mac-server sessions
531 - tool netwatch
532 - tool romon
533 - tool romon port
534 - tool sms
535 - tool sniffer
536 - tool traffic-generator
537 - tool traffic-generator packet-template
538 - tool traffic-generator port
539 - tool traffic-generator raw-packet-template
540 - tool traffic-generator stream
541 - tool traffic-monitor
542 - tr069-client
543 - user
544 - user aaa
545 - user active
546 - user group
547 - user settings
548 - user ssh-keys
549 - user-manager
550 - user-manager advanced
551 - user-manager attribute
552 - user-manager database
553 - user-manager limitation
554 - user-manager payment
555 - user-manager profile
556 - user-manager profile-limitation
557 - user-manager router
558 - user-manager session
559 - user-manager user
560 - user-manager user group
561 - user-manager user-profile
562 - zerotier
563 - zerotier controller
564 - zerotier controller member
565 - zerotier interface
566 # END PATH LIST
567 unfiltered:
568 description:
569 - Whether to output all fields, and not just the ones supported as input for M(community.routeros.api_modify).
570 - Unfiltered output can contain counters and other state information.
571 type: bool
572 default: false
573 handle_disabled:
574 description:
575 - How to handle unset values.
576 - V(exclamation) prepends the keys with V(!) in the output with value V(null).
577 - V(null-value) uses the regular key with value V(null).
578 - V(omit) omits these values from the result.
579 type: str
580 choices:
581 - exclamation
582 - null-value
583 - omit
584 default: exclamation
585 hide_defaults:
586 description:
587 - Whether to hide default values.
588 type: bool
589 default: true
590 include_dynamic:
591 description:
592 - Whether to include dynamic values.
593 - By default, they are not returned, and the C(dynamic) keys are omitted.
594 - If set to V(true), they are returned as well, and the C(dynamic) keys are returned as well.
595 type: bool
596 default: false
597 include_builtin:
598 description:
599 - Whether to include builtin values.
600 - By default, they are not returned, and the C(builtin) keys are omitted.
601 - If set to V(true), they are returned as well, and the C(builtin) keys are returned as well.
602 type: bool
603 default: false
604 version_added: 2.4.0
605 include_read_only:
606 description:
607 - Whether to include read-only fields.
608 - By default, they are not returned.
609 type: bool
610 default: false
611 version_added: 2.10.0
612 restrict:
613 description:
614 - Restrict output to entries matching the following criteria.
615 version_added: 2.18.0
616 seealso:
617 - module: community.routeros.api
618 - module: community.routeros.api_facts
619 - module: community.routeros.api_find_and_modify
620 - module: community.routeros.api_modify
621 """
622
623 EXAMPLES = r"""
624 ---
625 - name: Get IP addresses
626 community.routeros.api_info:
627 hostname: "{{ hostname }}"
628 password: "{{ password }}"
629 username: "{{ username }}"
630 path: ip address
631 register: ip_addresses
632
633 - name: Print data for IP addresses
634 ansible.builtin.debug:
635 var: ip_addresses.result
636
637 - name: Get IP addresses
638 community.routeros.api_info:
639 hostname: "{{ hostname }}"
640 password: "{{ password }}"
641 username: "{{ username }}"
642 path: ip address
643 register: ip_addresses
644
645 - name: Print data for IP addresses
646 ansible.builtin.debug:
647 var: ip_addresses.result
648 """
649
650 RETURN = r"""
651 result:
652 description: A list of all elements for the current path.
653 sample:
654 - '.id': '*1'
655 actual-interface: bridge
656 address: "192.168.88.1/24"
657 comment: defconf
658 disabled: false
659 dynamic: false
660 interface: bridge
661 invalid: false
662 network: 192.168.88.0
663 type: list
664 elements: dict
665 returned: always
666 """
667
668 from ansible.module_utils.basic import AnsibleModule
669 from ansible.module_utils.common.text.converters import to_native
670
671 from ansible_collections.community.routeros.plugins.module_utils.api import (
672 api_argument_spec,
673 check_has_library,
674 create_api,
675 get_api_version,
676 )
677
678 from ansible_collections.community.routeros.plugins.module_utils._api_data import (
679 PATHS,
680 join_path,
681 split_path,
682 )
683
684 from ansible_collections.community.routeros.plugins.module_utils._api_helper import (
685 restrict_argument_spec,
686 restrict_entry_accepted,
687 validate_and_prepare_restrict,
688 )
689
690 from ansible_collections.community.routeros.plugins.module_utils._tagging import deprecate_value
691
692 from ansible_collections.community.routeros.plugins.module_utils._hardware_detect import (
693 get_cached_or_detect,
694 )
695
696 try:
697 from librouteros.exceptions import LibRouterosError
698 except Exception:
699 # Handled in api module_utils
700 pass
701
702
703 def compose_api_path(api, path):
704 api_path = api.path()
705 for p in path:
706 api_path = api_path.join(p)
707 return api_path
708
709
710 def main():
711 module_args = dict(
712 path=dict(type='str', required=True, choices=sorted([join_path(path) for path in PATHS if PATHS[path].fully_understood])),
713 unfiltered=dict(type='bool', default=False),
714 handle_disabled=dict(type='str', choices=['exclamation', 'null-value', 'omit'], default='exclamation'),
715 hide_defaults=dict(type='bool', default=True),
716 include_dynamic=dict(type='bool', default=False),
717 include_builtin=dict(type='bool', default=False),
718 include_read_only=dict(type='bool', default=False),
719 )
720 module_args.update(api_argument_spec())
721 module_args.update(restrict_argument_spec())
722
723 module = AnsibleModule(
724 argument_spec=module_args,
725 supports_check_mode=True,
726 )
727
728 check_has_library(module)
729 api = create_api(module)
730
731 path = split_path(module.params['path'])
732 versioned_path_info = PATHS.get(tuple(path))
733 if versioned_path_info is None:
734 module.fail_json(msg='Path /{path} is not yet supported'.format(path='/'.join(path)))
735 if versioned_path_info.hardware_detect:
736 hardware_variant_key = get_cached_or_detect(versioned_path_info.hardware_detect, api)
737 if hardware_variant_key not in versioned_path_info.hardware_variants:
738 module.fail_json(
739 msg='Path /{path} is not supported for detected hardware variant {variant}'.format(
740 path='/'.join(path), variant=hardware_variant_key))
741 versioned_path_info = versioned_path_info.hardware_variants[hardware_variant_key]
742 if versioned_path_info.needs_version:
743 api_version = get_api_version(api)
744 supported, not_supported_msg = versioned_path_info.provide_version(api_version)
745 if not supported:
746 msg = 'Path /{path} is not supported for API version {api_version}'.format(path='/'.join(path), api_version=api_version)
747 if not_supported_msg:
748 msg = '{0}: {1}'.format(msg, not_supported_msg)
749 module.fail_json(msg=msg)
750 path_info = versioned_path_info.get_data()
751
752 handle_disabled = module.params['handle_disabled']
753 hide_defaults = module.params['hide_defaults']
754 include_dynamic = module.params['include_dynamic']
755 include_builtin = module.params['include_builtin']
756 include_read_only = module.params['include_read_only']
757 restrict_data = validate_and_prepare_restrict(module, path_info)
758 try:
759 api_path = compose_api_path(api, path)
760
761 result = []
762 unfiltered = module.params['unfiltered']
763 for entry in api_path:
764 if not include_dynamic:
765 if entry.get('dynamic', False):
766 continue
767 if not include_builtin:
768 if entry.get('builtin', False):
769 continue
770 if not restrict_entry_accepted(entry, path_info, restrict_data):
771 continue
772 if not unfiltered:
773 for k in list(entry):
774 if k == '.id':
775 continue
776 if k == 'dynamic' and include_dynamic:
777 continue
778 if k == 'builtin' and include_builtin:
779 continue
780 if k not in path_info.fields:
781 entry.pop(k)
782 if handle_disabled != 'omit':
783 for k, field_info in path_info.fields.items():
784 if field_info.write_only:
785 entry.pop(k, None)
786 continue
787 if k not in entry:
788 if handle_disabled == 'exclamation':
789 k = '!%s' % k
790 entry[k] = None
791 for k, field_info in path_info.fields.items():
792 if hide_defaults:
793 if field_info.default is not None and entry.get(k) == field_info.default:
794 entry.pop(k)
795 if field_info.absent_value and k not in entry:
796 entry[k] = field_info.absent_value
797 if not include_read_only and k in entry and field_info.read_only:
798 entry.pop(k)
799 if field_info.depr and field_info.depr.applies("read") and k in entry:
800 entry[k] = deprecate_value(entry[k], field_info.depr.msg, field_info.depr.version)
801 result.append(entry)
802
803 module.exit_json(result=result)
804 except (LibRouterosError, UnicodeEncodeError) as e:
805 module.fail_json(msg=to_native(e))
806
807
808 if __name__ == '__main__':
809 main()