| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# (c) 2012, Michael DeHaan <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
|
| 9 |
DOCUMENTATION = """ |
| 10 |
--- |
| 11 |
module: setup |
| 12 |
version_added: historical |
| 13 |
short_description: Gathers facts about remote hosts |
| 14 |
options: |
| 15 |
gather_subset: |
| 16 |
version_added: "2.1" |
| 17 |
description: |
| 18 |
- "If supplied, restrict the additional facts collected to the given subset. |
| 19 |
Possible values: V(all), V(all_ipv4_addresses), V(all_ipv6_addresses), V(apparmor), V(architecture), |
| 20 |
V(caps), V(chroot),V(cmdline), V(date_time), V(default_ipv4), V(default_ipv6), V(devices), |
| 21 |
V(distribution), V(distribution_cpe_name), V(distribution_major_version), V(distribution_release), V(distribution_version), |
| 22 |
V(dns), V(effective_group_ids), V(effective_user_id), V(env), V(facter), V(fips), V(hardware), |
| 23 |
V(interfaces), V(is_chroot), V(iscsi), V(kernel), V(local), V(lsb), V(machine), V(machine_id), |
| 24 |
V(mounts), V(network), V(ohai), V(os_family), V(pkg_mgr), V(platform), V(processor), V(processor_cores), |
| 25 |
V(processor_count), V(python), V(python_version), V(real_user_id), V(selinux), V(service_mgr), |
| 26 |
V(ssh_host_key_dsa_public), V(ssh_host_key_ecdsa_public), V(ssh_host_key_ed25519_public), |
| 27 |
V(ssh_host_key_rsa_public), V(ssh_host_pub_keys), V(ssh_pub_keys), V(system), V(system_capabilities), |
| 28 |
V(system_capabilities_enforced), V(systemd), V(user), V(user_dir), V(user_gecos), V(user_gid), V(user_id), |
| 29 |
V(user_shell), V(user_uid), V(virtual), V(virtualization_role), V(virtualization_type). |
| 30 |
Can specify a list of values to specify a larger subset. |
| 31 |
Values can also be used with an initial C(!) to specify that |
| 32 |
that specific subset should not be collected. For instance: |
| 33 |
V(!hardware,!network,!virtual,!ohai,!facter). If V(!all) is specified |
| 34 |
then only the min subset is collected. To avoid collecting even the |
| 35 |
min subset, specify V(!all,!min). To collect only specific facts, |
| 36 |
use V(!all,!min), and specify the particular fact subsets. |
| 37 |
Use the filter parameter if you do not want to display some collected |
| 38 |
facts." |
| 39 |
type: list |
| 40 |
elements: str |
| 41 |
default: "all" |
| 42 |
gather_timeout: |
| 43 |
version_added: "2.2" |
| 44 |
description: |
| 45 |
- Set the default timeout in seconds for individual fact gathering. |
| 46 |
type: int |
| 47 |
default: 10 |
| 48 |
filter: |
| 49 |
version_added: "1.1" |
| 50 |
description: |
| 51 |
- If supplied, only return facts that match one of the shell-style |
| 52 |
(fnmatch) pattern. An empty list basically means 'no filter'. |
| 53 |
As of Ansible 2.11, the type has changed from string to list |
| 54 |
and the default has became an empty list. A simple string is |
| 55 |
still accepted and works as a single pattern. The behaviour |
| 56 |
prior to Ansible 2.11 remains. |
| 57 |
type: list |
| 58 |
elements: str |
| 59 |
default: [] |
| 60 |
fact_path: |
| 61 |
version_added: "1.3" |
| 62 |
description: |
| 63 |
- Path used for local ansible facts (C(*.fact)) - files in this dir |
| 64 |
will be run (if executable) and their results be added to C(ansible_local) facts. |
| 65 |
If a file is not executable it is read instead. |
| 66 |
File/results format can be JSON or INI-format. The default O(fact_path) can be |
| 67 |
specified in C(ansible.cfg) for when setup is automatically called as part of |
| 68 |
C(gather_facts). |
| 69 |
NOTE - For windows clients, the results will be added to a variable named after the |
| 70 |
local file (without extension suffix), rather than C(ansible_local). |
| 71 |
- Since Ansible 2.1, Windows hosts can use O(fact_path). Make sure that this path |
| 72 |
exists on the target host. Files in this path MUST be PowerShell scripts C(.ps1) |
| 73 |
which outputs an object. This object will be formatted by Ansible as json so the |
| 74 |
script should be outputting a raw hashtable, array, or other primitive object. |
| 75 |
type: path |
| 76 |
default: /etc/ansible/facts.d |
| 77 |
description: |
| 78 |
- This module is automatically called by playbooks to gather useful |
| 79 |
variables about remote hosts that can be used in playbooks. It can also be |
| 80 |
executed directly by C(/usr/bin/ansible) to check what variables are |
| 81 |
available to a host. Ansible provides many I(facts) about the system, |
| 82 |
automatically. |
| 83 |
- This module is also supported for Windows targets. |
| 84 |
extends_documentation_fragment: |
| 85 |
- action_common_attributes |
| 86 |
- action_common_attributes.facts |
| 87 |
attributes: |
| 88 |
check_mode: |
| 89 |
support: full |
| 90 |
diff_mode: |
| 91 |
support: none |
| 92 |
facts: |
| 93 |
support: full |
| 94 |
platform: |
| 95 |
platforms: posix, windows |
| 96 |
notes: |
| 97 |
- More ansible facts will be added with successive releases. If I(facter) or |
| 98 |
I(ohai) are installed, variables from these programs will also be snapshotted |
| 99 |
into the JSON file for usage in templating. These variables are prefixed |
| 100 |
with C(facter_) and C(ohai_) so it's easy to tell their source. All variables are |
| 101 |
bubbled up to the caller. Using the ansible facts and choosing to not |
| 102 |
install I(facter) and I(ohai) means you can avoid Ruby-dependencies on your |
| 103 |
remote systems. (See also M(community.general.facter) and M(community.general.ohai).) |
| 104 |
- The filter option filters only the first level subkey below ansible_facts. |
| 105 |
- If the target host is Windows, you will not currently have the ability to use |
| 106 |
O(filter) as this is provided by a simpler implementation of the module. |
| 107 |
- This module should be run with elevated privileges on BSD systems to gather facts like ansible_product_version. |
| 108 |
- For more information about delegated facts, |
| 109 |
please check U(https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html#delegating-facts). |
| 110 |
author: |
| 111 |
- "Ansible Core Team" |
| 112 |
- "Michael DeHaan" |
| 113 |
""" |
| 114 |
|
| 115 |
EXAMPLES = r""" |
| 116 |
# Display facts from all hosts and store them indexed by `hostname` at `/tmp/facts`. |
| 117 |
# ansible all -m ansible.builtin.setup --tree /tmp/facts |
| 118 |
|
| 119 |
# Display only facts regarding memory found by ansible on all hosts and output them. |
| 120 |
# ansible all -m ansible.builtin.setup -a 'filter=ansible_*_mb' |
| 121 |
|
| 122 |
# Display only facts returned by facter. |
| 123 |
# ansible all -m ansible.builtin.setup -a 'filter=facter_*' |
| 124 |
|
| 125 |
# Collect only facts returned by facter. |
| 126 |
# ansible all -m ansible.builtin.setup -a 'gather_subset=!all,facter' |
| 127 |
|
| 128 |
- name: Collect only facts returned by facter |
| 129 |
ansible.builtin.setup: |
| 130 |
gather_subset: |
| 131 |
- '!all' |
| 132 |
- '!<any valid subset>' |
| 133 |
- facter |
| 134 |
|
| 135 |
- name: Filter and return only selected facts |
| 136 |
ansible.builtin.setup: |
| 137 |
filter: |
| 138 |
- 'ansible_distribution' |
| 139 |
- 'ansible_machine_id' |
| 140 |
- 'ansible_*_mb' |
| 141 |
|
| 142 |
# Display only facts about certain interfaces. |
| 143 |
# ansible all -m ansible.builtin.setup -a 'filter=ansible_eth[0-2]' |
| 144 |
|
| 145 |
# Restrict additional gathered facts to network and virtual (includes default minimum facts) |
| 146 |
# ansible all -m ansible.builtin.setup -a 'gather_subset=network,virtual' |
| 147 |
|
| 148 |
# Collect only network and virtual (excludes default minimum facts) |
| 149 |
# ansible all -m ansible.builtin.setup -a 'gather_subset=!all,network,virtual' |
| 150 |
|
| 151 |
# Do not call puppet facter or ohai even if present. |
| 152 |
# ansible all -m ansible.builtin.setup -a 'gather_subset=!facter,!ohai' |
| 153 |
|
| 154 |
# Only collect the default minimum amount of facts: |
| 155 |
# ansible all -m ansible.builtin.setup -a 'gather_subset=!all' |
| 156 |
|
| 157 |
# Collect no facts, even the default minimum subset of facts: |
| 158 |
# ansible all -m ansible.builtin.setup -a 'gather_subset=!all,!min' |
| 159 |
|
| 160 |
# Display facts from Windows hosts with custom facts stored in C:\custom_facts. |
| 161 |
# ansible windows -m ansible.builtin.setup -a "fact_path='c:\custom_facts'" |
| 162 |
|
| 163 |
# Gathers facts for the machines in the dbservers group (a.k.a Delegating facts) |
| 164 |
- hosts: app_servers |
| 165 |
tasks: |
| 166 |
- name: Gather facts from db servers |
| 167 |
ansible.builtin.setup: |
| 168 |
delegate_to: "{{ item }}" |
| 169 |
delegate_facts: true |
| 170 |
loop: "{{ groups['dbservers'] }}" |
| 171 |
""" |
| 172 |
|
| 173 |
# import module snippets |
| 174 |
from ..module_utils.basic import AnsibleModule |
| 175 |
|
| 176 |
from ansible.module_utils.common.text.converters import to_text |
| 177 |
from ansible.module_utils.facts import ansible_collector, default_collectors |
| 178 |
from ansible.module_utils.facts.collector import CollectorNotFoundError, CycleFoundInFactDeps, UnresolvedFactDep |
| 179 |
from ansible.module_utils.facts.namespace import PrefixFactNamespace |
| 180 |
|
| 181 |
|
| 182 |
def main(): |
| 183 |
module = AnsibleModule( |
| 184 |
argument_spec=dict( |
| 185 |
gather_subset=dict(default=["all"], required=False, type='list', elements='str'), |
| 186 |
gather_timeout=dict(default=10, required=False, type='int'), |
| 187 |
filter=dict(default=[], required=False, type='list', elements='str'), |
| 188 |
fact_path=dict(default='/etc/ansible/facts.d', required=False, type='path'), |
| 189 |
), |
| 190 |
supports_check_mode=True, |
| 191 |
) |
| 192 |
|
| 193 |
gather_subset = module.params['gather_subset'] |
| 194 |
gather_timeout = module.params['gather_timeout'] |
| 195 |
filter_spec = module.params['filter'] |
| 196 |
|
| 197 |
# TODO: this mimics existing behavior where gather_subset=["!all"] actually means |
| 198 |
# to collect nothing except for the below list |
| 199 |
# TODO: decide what '!all' means, I lean towards making it mean none, but likely needs |
| 200 |
# some tweaking on how gather_subset operations are performed |
| 201 |
minimal_gather_subset = frozenset(['apparmor', 'caps', 'cmdline', 'date_time', |
| 202 |
'distribution', 'dns', 'env', 'fips', 'local', |
| 203 |
'lsb', 'pkg_mgr', 'platform', 'python', 'selinux', |
| 204 |
'service_mgr', 'ssh_pub_keys', 'user']) |
| 205 |
|
| 206 |
all_collector_classes = default_collectors.collectors |
| 207 |
|
| 208 |
# rename namespace_name to root_key? |
| 209 |
namespace = PrefixFactNamespace(namespace_name='ansible', |
| 210 |
prefix='ansible_') |
| 211 |
|
| 212 |
try: |
| 213 |
fact_collector = ansible_collector.get_ansible_collector(all_collector_classes=all_collector_classes, |
| 214 |
namespace=namespace, |
| 215 |
filter_spec=filter_spec, |
| 216 |
gather_subset=gather_subset, |
| 217 |
gather_timeout=gather_timeout, |
| 218 |
minimal_gather_subset=minimal_gather_subset) |
| 219 |
except (TypeError, CollectorNotFoundError, CycleFoundInFactDeps, UnresolvedFactDep) as e: |
| 220 |
# bad subset given, collector, idk, deps declared but not found |
| 221 |
module.fail_json(msg=to_text(e)) |
| 222 |
|
| 223 |
facts_dict = fact_collector.collect(module=module) |
| 224 |
|
| 225 |
module.exit_json(ansible_facts=facts_dict) |
| 226 |
|
| 227 |
|
| 228 |
if __name__ == '__main__': |
| 229 |
main() |