Debian Codegraph @

ansible_collections/cisco/nxos/plugins/modules/nxos_facts.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_facts
26 extends_documentation_fragment:
27 - cisco.nxos.nxos
28 short_description: Gets facts about NX-OS switches
29 description:
30 - Collects facts from Cisco Nexus devices running the NX-OS operating system. Fact
31 collection is supported over both C(network_cli) and C(httpapi). This module prepends
32 all of the base network fact keys with C(ansible_net_<fact>). The facts module
33 will always collect a base set of facts from the device and can enable or disable
34 collection of additional facts.
35 version_added: 1.0.0
36 author:
37 - Jason Edelman (@jedelman8)
38 - Gabriele Gerbino (@GGabriele)
39 notes:
40 - Tested against NXOSv 7.3.(0)D1(1) on VIRL
41 - Unsupported for Cisco MDS
42 options:
43 gather_subset:
44 description:
45 - When supplied, this argument will gather operational facts only for the given subset. Possible
46 values for this argument include C(all), C(hardware), C(config), C(legacy), C(interfaces), and C(min). Can
47 specify a list of values to include a larger subset. Values can also be used
48 with an initial C(!) to specify that a specific subset should not be collected.
49 required: false
50 default: 'min'
51 type: list
52 elements: str
53 gather_network_resources:
54 description:
55 - When supplied, this argument will gather configuration facts only for the given subset.
56 Can specify a list of values to include a larger subset. Values can
57 also be used with an initial C(!) to specify that a specific subset should
58 not be collected.
59 - Valid subsets are C(all), C(bfd_interfaces), C(lag_interfaces),
60 C(telemetry), C(vlans), C(lacp), C(lacp_interfaces), C(interfaces), C(l3_interfaces),
61 C(l2_interfaces), C(lldp_global), C(acls), C(acl_interfaces), C(ospfv2), C(ospfv3), C(ospf_interfaces),
62 C(bgp_global), C(bgp_address_family), C(route_maps), C(prefix_lists), C(logging_global), C(ntp_global),
63 C(snmp_server), C(hostname).
64 required: false
65 type: list
66 elements: str
67 available_network_resources:
68 description: When set to C(true) a list of network resources for which resource modules are available will be provided.
69 type: bool
70 default: false
71 """
72
73 EXAMPLES = """
74 - name: Gather all legacy facts
75 cisco.nxos.nxos_facts:
76 gather_subset: all
77 - name: Gather only the config and default facts
78 cisco.nxos.nxos_facts:
79 gather_subset:
80 - config
81 - name: Do not gather hardware facts
82 cisco.nxos.nxos_facts:
83 gather_subset:
84 - '!hardware'
85 - name: Gather legacy and resource facts
86 cisco.nxos.nxos_facts:
87 gather_subset: all
88 gather_network_resources: all
89 - name: Gather only the interfaces resource facts and no legacy facts
90 cisco.nxos.nxos_facts:
91 gather_subset:
92 - '!all'
93 - '!min'
94 gather_network_resources:
95 - interfaces
96 - name: Gather interfaces resource and minimal legacy facts
97 cisco.nxos.nxos_facts:
98 gather_subset: min
99 gather_network_resources: interfaces
100 """
101
102 RETURN = """
103 ansible_net_gather_subset:
104 description: The list of fact subsets collected from the device
105 returned: always
106 type: list
107 ansible_net_gather_network_resources:
108 description: The list of fact for network resource subsets collected from the device
109 returned: when the resource is configured
110 type: list
111 # default
112 ansible_net_model:
113 description: The model name returned from the device
114 returned: always
115 type: str
116 ansible_net_serialnum:
117 description: The serial number of the remote device
118 returned: always
119 type: str
120 ansible_net_version:
121 description: The operating system version running on the remote device
122 returned: always
123 type: str
124 ansible_net_hostname:
125 description: The configured hostname of the device
126 returned: always
127 type: str
128 ansible_net_image:
129 description: The image file the device is running
130 returned: always
131 type: str
132 ansible_net_api:
133 description: The name of the transport
134 returned: always
135 type: str
136 ansible_net_license_hostid:
137 description: The License host id of the device
138 returned: always
139 type: str
140 ansible_net_python_version:
141 description: The Python version Ansible controller is using
142 returned: always
143 type: str
144 # hardware
145 ansible_net_filesystems:
146 description: All file system names available on the device
147 returned: when hardware is configured
148 type: list
149 ansible_net_memfree_mb:
150 description: The available free memory on the remote device in Mb
151 returned: when hardware is configured
152 type: int
153 ansible_net_memtotal_mb:
154 description: The total memory on the remote device in Mb
155 returned: when hardware is configured
156 type: int
157 # config
158 ansible_net_config:
159 description: The current active config from the device
160 returned: when config is configured
161 type: str
162 # interfaces
163 ansible_net_all_ipv4_addresses:
164 description: All IPv4 addresses configured on the device
165 returned: when interfaces is configured
166 type: list
167 ansible_net_all_ipv6_addresses:
168 description: All IPv6 addresses configured on the device
169 returned: when interfaces is configured
170 type: list
171 ansible_net_interfaces:
172 description: A hash of all interfaces running on the system
173 returned: when interfaces is configured
174 type: dict
175 ansible_net_neighbors:
176 description:
177 - The list of LLDP and CDP neighbors from the device. If both,
178 CDP and LLDP neighbor data is present on one port, CDP is preferred.
179 returned: when interfaces is configured
180 type: dict
181 # legacy (pre Ansible 2.2)
182 fan_info:
183 description: A hash of facts about fans in the remote device
184 returned: when legacy is configured
185 type: dict
186 hostname:
187 description: The configured hostname of the remote device
188 returned: when legacy is configured
189 type: dict
190 interfaces_list:
191 description: The list of interface names on the remote device
192 returned: when legacy is configured
193 type: dict
194 kickstart:
195 description: The software version used to boot the system
196 returned: when legacy is configured
197 type: str
198 module:
199 description: A hash of facts about the modules in a remote device
200 returned: when legacy is configured
201 type: dict
202 platform:
203 description: The hardware platform reported by the remote device
204 returned: when legacy is configured
205 type: str
206 power_supply_info:
207 description: A hash of facts about the power supplies in the remote device
208 returned: when legacy is configured
209 type: str
210 vlan_list:
211 description: The list of VLAN IDs configured on the remote device
212 returned: when legacy is configured
213 type: list
214 """
215
216 from ansible.module_utils.basic import AnsibleModule
217 from ansible.module_utils.connection import Connection
218
219 from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.argspec.facts.facts import (
220 FactsArgs,
221 )
222 from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.facts.facts import Facts
223
224
225 def get_chassis_type(connection):
226 """Return facts resource subsets based on
227 chassis model.
228 """
229 target_type = "nexus"
230
231 device_info = connection.get_device_info()
232 model = device_info.get("network_os_model", "")
233 platform = device_info.get("network_os_platform", "")
234
235 if platform.startswith("DS-") and "MDS" in model:
236 target_type = "mds"
237
238 return target_type
239
240
241 def main():
242 """
243 Main entry point for module execution
244
245 :returns: ansible_facts
246 """
247 argument_spec = FactsArgs.argument_spec
248
249 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
250 connection = Connection(module._socket_path)
251 facts = Facts(module, chassis_type=get_chassis_type(connection))
252
253 warnings = []
254
255 ansible_facts = {}
256 if module.params.get("available_network_resources"):
257 ansible_facts["available_network_resources"] = sorted(facts.get_resource_subsets().keys())
258
259 result = facts.get_facts()
260 additional_facts, additional_warnings = result
261 ansible_facts.update(additional_facts)
262 warnings.extend(additional_warnings)
263
264 module.exit_json(ansible_facts=ansible_facts, warnings=warnings)
265
266
267 if __name__ == "__main__":
268 main()