| 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 |
DOCUMENTATION = """ |
| 23 |
module: ios_facts |
| 24 |
author: |
| 25 |
- Peter Sprygada (@privateip) |
| 26 |
- Sumit Jaiswal (@justjais) |
| 27 |
short_description: Module to collect facts from remote devices. |
| 28 |
description: |
| 29 |
- Collects a base set of device facts from a remote device that is running IOS. This |
| 30 |
module prepends all of the base network fact keys with C(ansible_net_<fact>). The |
| 31 |
facts module will always collect a base set of facts from the device and can enable |
| 32 |
or disable collection of additional facts. |
| 33 |
version_added: 1.0.0 |
| 34 |
extends_documentation_fragment: |
| 35 |
- cisco.ios.ios |
| 36 |
notes: |
| 37 |
- Tested against Cisco IOSXE Version 17.3 on CML and IOS 15.6 for L2 specific resource. |
| 38 |
- Facts gathering for L3 devices are supposed to produce blank output for unsupported |
| 39 |
resources like vlan. |
| 40 |
- This module works with connection C(network_cli). |
| 41 |
See U(https://docs.ansible.com/ansible/latest/network/user_guide/platform_ios.html) |
| 42 |
options: |
| 43 |
gather_subset: |
| 44 |
description: |
| 45 |
- When supplied, this argument restricts the facts collected to a given subset. |
| 46 |
- Possible values for this argument include C(all), C(min), C(default), C(hardware), C(config), |
| 47 |
and C(interfaces). |
| 48 |
- Specify a list of values to include a larger subset. |
| 49 |
- Use a value with an initial C(!) to collect all facts except that subset. |
| 50 |
required: false |
| 51 |
default: "min" |
| 52 |
type: list |
| 53 |
elements: str |
| 54 |
gather_network_resources: |
| 55 |
description: |
| 56 |
- When supplied, this argument will restrict the facts collected to a given subset. |
| 57 |
Possible values for this argument include all and the resources like interfaces, |
| 58 |
vlans etc. Can specify a list of values to include a larger subset. Values can |
| 59 |
also be used with an initial C(!) to specify that a specific subset should |
| 60 |
not be collected. Valid subsets are 'hsrp_interfaces', 'bgp_global', 'l3_interfaces', 'lag_interfaces', |
| 61 |
'ntp_global', 'acls', 'hostname', 'interfaces', 'lldp_interfaces', 'logging_global', |
| 62 |
'ospf_interfaces', 'ospfv2', 'prefix_lists', 'static_routes', 'acl_interfaces', |
| 63 |
'all', 'bgp_address_family', 'l2_interfaces', 'lacp', 'lacp_interfaces', 'lldp_global', |
| 64 |
'ospfv3', 'snmp_server', 'vlans', 'service'. |
| 65 |
type: list |
| 66 |
elements: str |
| 67 |
available_network_resources: |
| 68 |
description: When '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.ios.ios_facts: |
| 76 |
gather_subset: all |
| 77 |
|
| 78 |
- name: Gather only the config and default facts |
| 79 |
cisco.ios.ios_facts: |
| 80 |
gather_subset: |
| 81 |
- config |
| 82 |
|
| 83 |
- name: Do not gather hardware facts |
| 84 |
cisco.ios.ios_facts: |
| 85 |
gather_subset: |
| 86 |
- "!hardware" |
| 87 |
|
| 88 |
- name: Gather legacy and resource facts |
| 89 |
cisco.ios.ios_facts: |
| 90 |
gather_subset: all |
| 91 |
gather_network_resources: all |
| 92 |
|
| 93 |
- name: Gather only the interfaces resource facts and no legacy facts |
| 94 |
cisco.ios.ios_facts: |
| 95 |
gather_subset: |
| 96 |
- "!all" |
| 97 |
- "!min" |
| 98 |
gather_network_resources: |
| 99 |
- interfaces |
| 100 |
|
| 101 |
- name: Gather interfaces resource and minimal legacy facts |
| 102 |
cisco.ios.ios_facts: |
| 103 |
gather_subset: min |
| 104 |
gather_network_resources: interfaces |
| 105 |
|
| 106 |
- name: Gather L2 interfaces resource and minimal legacy facts |
| 107 |
cisco.ios.ios_facts: |
| 108 |
gather_subset: min |
| 109 |
gather_network_resources: l2_interfaces |
| 110 |
|
| 111 |
- name: Gather L3 interfaces resource and minimal legacy facts |
| 112 |
cisco.ios.ios_facts: |
| 113 |
gather_subset: min |
| 114 |
gather_network_resources: l3_interfaces |
| 115 |
""" |
| 116 |
|
| 117 |
RETURN = """ |
| 118 |
ansible_net_gather_subset: |
| 119 |
description: The list of fact subsets collected from the device |
| 120 |
returned: always |
| 121 |
type: list |
| 122 |
|
| 123 |
ansible_net_gather_network_resources: |
| 124 |
description: The list of fact for network resource subsets collected from the device |
| 125 |
returned: when the resource is configured |
| 126 |
type: list |
| 127 |
|
| 128 |
# default |
| 129 |
ansible_net_model: |
| 130 |
description: The model name returned from the device |
| 131 |
returned: always |
| 132 |
type: str |
| 133 |
ansible_net_serialnum: |
| 134 |
description: The serial number of the remote device |
| 135 |
returned: always |
| 136 |
type: str |
| 137 |
ansible_net_version: |
| 138 |
description: The operating system version running on the remote device |
| 139 |
returned: always |
| 140 |
type: str |
| 141 |
ansible_net_iostype: |
| 142 |
description: The operating system type (IOS or IOS-XE) running on the remote device |
| 143 |
returned: always |
| 144 |
type: str |
| 145 |
ansible_net_hostname: |
| 146 |
description: The configured hostname of the device |
| 147 |
returned: always |
| 148 |
type: str |
| 149 |
ansible_net_image: |
| 150 |
description: The image file the device is running |
| 151 |
returned: always |
| 152 |
type: str |
| 153 |
ansible_net_stacked_models: |
| 154 |
description: The model names of each device in the stack |
| 155 |
returned: when multiple devices are configured in a stack |
| 156 |
type: list |
| 157 |
ansible_net_stacked_serialnums: |
| 158 |
description: The serial numbers of each device in the stack |
| 159 |
returned: when multiple devices are configured in a stack |
| 160 |
type: list |
| 161 |
ansible_net_api: |
| 162 |
description: The name of the transport |
| 163 |
returned: always |
| 164 |
type: str |
| 165 |
ansible_net_python_version: |
| 166 |
description: The Python version Ansible controller is using |
| 167 |
returned: always |
| 168 |
type: str |
| 169 |
|
| 170 |
# hardware |
| 171 |
ansible_net_filesystems: |
| 172 |
description: All file system names available on the device |
| 173 |
returned: when hardware is configured |
| 174 |
type: list |
| 175 |
ansible_net_filesystems_info: |
| 176 |
description: A hash of all file systems containing info about each file system (e.g. free and total space) |
| 177 |
returned: when hardware is configured |
| 178 |
type: dict |
| 179 |
ansible_net_memfree_mb: |
| 180 |
description: The available free memory on the remote device in MiB |
| 181 |
returned: when hardware is configured |
| 182 |
type: int |
| 183 |
ansible_net_memtotal_mb: |
| 184 |
description: The total memory on the remote device in MiB |
| 185 |
returned: when hardware is configured |
| 186 |
type: int |
| 187 |
ansible_net_cpu_utilization: |
| 188 |
description: The current CPU utilization of the device |
| 189 |
returned: when hardware is configured |
| 190 |
type: dict |
| 191 |
|
| 192 |
# config |
| 193 |
ansible_net_config: |
| 194 |
description: The current active config from the device |
| 195 |
returned: when config is configured |
| 196 |
type: str |
| 197 |
|
| 198 |
# interfaces |
| 199 |
ansible_net_all_ipv4_addresses: |
| 200 |
description: All IPv4 addresses configured on the device |
| 201 |
returned: when interfaces is configured |
| 202 |
type: list |
| 203 |
ansible_net_all_ipv6_addresses: |
| 204 |
description: All IPv6 addresses configured on the device |
| 205 |
returned: when interfaces is configured |
| 206 |
type: list |
| 207 |
ansible_net_interfaces: |
| 208 |
description: A hash of all interfaces running on the system |
| 209 |
returned: when interfaces is configured |
| 210 |
type: dict |
| 211 |
ansible_net_neighbors: |
| 212 |
description: |
| 213 |
- The list of CDP and LLDP neighbors from the remote device. If both, |
| 214 |
CDP and LLDP neighbor data is present on one port, CDP is preferred. |
| 215 |
returned: when interfaces is configured |
| 216 |
type: dict |
| 217 |
""" |
| 218 |
from ansible.module_utils.basic import AnsibleModule |
| 219 |
|
| 220 |
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.argspec.facts.facts import ( |
| 221 |
FactsArgs, |
| 222 |
) |
| 223 |
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.facts import ( |
| 224 |
FACT_RESOURCE_SUBSETS, |
| 225 |
Facts, |
| 226 |
) |
| 227 |
|
| 228 |
|
| 229 |
def main(): |
| 230 |
""" |
| 231 |
Main entry point for module execution |
| 232 |
|
| 233 |
:returns: ansible_facts |
| 234 |
""" |
| 235 |
argument_spec = FactsArgs.argument_spec |
| 236 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 237 |
warnings = [] |
| 238 |
|
| 239 |
ansible_facts = {} |
| 240 |
if module.params.get("available_network_resources"): |
| 241 |
ansible_facts["available_network_resources"] = sorted(FACT_RESOURCE_SUBSETS.keys()) |
| 242 |
result = Facts(module).get_facts() |
| 243 |
additional_facts, additional_warnings = result |
| 244 |
ansible_facts.update(additional_facts) |
| 245 |
warnings.extend(additional_warnings) |
| 246 |
module.exit_json(ansible_facts=ansible_facts, warnings=warnings) |
| 247 |
|
| 248 |
|
| 249 |
if __name__ == "__main__": |
| 250 |
main() |