| 1 |
# This code is part of Ansible, but is an independent component. |
| 2 |
# This particular file snippet, and this file snippet only, is BSD licensed. |
| 3 |
# Modules you write using this snippet, which is embedded dynamically by Ansible |
| 4 |
# still belong to the author of the module, and may assign their own license |
| 5 |
# to the complete work. |
| 6 |
# |
| 7 |
# (c) 2017 Red Hat Inc. |
| 8 |
# |
| 9 |
# Redistribution and use in source and binary forms, with or without modification, |
| 10 |
# are permitted provided that the following conditions are met: |
| 11 |
# |
| 12 |
# * Redistributions of source code must retain the above copyright |
| 13 |
# notice, this list of conditions and the following disclaimer. |
| 14 |
# * Redistributions in binary form must reproduce the above copyright notice, |
| 15 |
# this list of conditions and the following disclaimer in the documentation |
| 16 |
# and/or other materials provided with the distribution. |
| 17 |
# |
| 18 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 |
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 |
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 26 |
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
# |
| 28 |
|
| 29 |
from __future__ import annotations |
| 30 |
|
| 31 |
from ansible.module_utils.facts.namespace import PrefixFactNamespace |
| 32 |
from ansible.module_utils.facts import default_collectors |
| 33 |
from ansible.module_utils.facts import ansible_collector |
| 34 |
|
| 35 |
|
| 36 |
def get_all_facts(module): |
| 37 |
"""compat api for ansible 2.2/2.3 module_utils.facts.get_all_facts method |
| 38 |
|
| 39 |
Expects module to be an instance of AnsibleModule, with a 'gather_subset' param. |
| 40 |
|
| 41 |
returns a dict mapping the bare fact name ('default_ipv4' with no 'ansible_' namespace) to |
| 42 |
the fact value.""" |
| 43 |
|
| 44 |
gather_subset = module.params['gather_subset'] |
| 45 |
return ansible_facts(module, gather_subset=gather_subset) |
| 46 |
|
| 47 |
|
| 48 |
def ansible_facts(module, gather_subset=None): |
| 49 |
"""Compat api for ansible 2.0/2.2/2.3 module_utils.facts.ansible_facts method |
| 50 |
|
| 51 |
2.3/2.3 expects a gather_subset arg. |
| 52 |
2.0/2.1 does not except a gather_subset arg |
| 53 |
|
| 54 |
So make gather_subsets an optional arg, defaulting to configured DEFAULT_GATHER_TIMEOUT |
| 55 |
|
| 56 |
'module' should be an instance of an AnsibleModule. |
| 57 |
|
| 58 |
returns a dict mapping the bare fact name ('default_ipv4' with no 'ansible_' namespace) to |
| 59 |
the fact value. |
| 60 |
""" |
| 61 |
|
| 62 |
gather_subset = gather_subset or module.params.get('gather_subset', ['all']) |
| 63 |
gather_timeout = module.params.get('gather_timeout', 10) |
| 64 |
filter_spec = module.params.get('filter', '*') |
| 65 |
|
| 66 |
minimal_gather_subset = frozenset(['apparmor', 'caps', 'cmdline', 'date_time', |
| 67 |
'distribution', 'dns', 'env', 'fips', 'local', |
| 68 |
'lsb', 'pkg_mgr', 'platform', 'python', 'selinux', |
| 69 |
'service_mgr', 'ssh_pub_keys', 'user']) |
| 70 |
|
| 71 |
all_collector_classes = default_collectors.collectors |
| 72 |
|
| 73 |
# don't add a prefix |
| 74 |
namespace = PrefixFactNamespace(namespace_name='ansible', prefix='') |
| 75 |
|
| 76 |
fact_collector = \ |
| 77 |
ansible_collector.get_ansible_collector(all_collector_classes=all_collector_classes, |
| 78 |
namespace=namespace, |
| 79 |
filter_spec=filter_spec, |
| 80 |
gather_subset=gather_subset, |
| 81 |
gather_timeout=gather_timeout, |
| 82 |
minimal_gather_subset=minimal_gather_subset) |
| 83 |
|
| 84 |
facts_dict = fact_collector.collect(module=module) |
| 85 |
|
| 86 |
return facts_dict |