| 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 |
import fnmatch |
| 32 |
import sys |
| 33 |
import typing as t |
| 34 |
|
| 35 |
from ansible.module_utils.facts import timeout |
| 36 |
from ansible.module_utils.facts import collector |
| 37 |
from ansible.module_utils.common.collections import is_string |
| 38 |
|
| 39 |
|
| 40 |
class AnsibleFactCollector(collector.BaseFactCollector): |
| 41 |
"""A FactCollector that returns results under 'ansible_facts' top level key. |
| 42 |
|
| 43 |
If a namespace if provided, facts will be collected under that namespace. |
| 44 |
For ex, a ansible.module_utils.facts.namespace.PrefixFactNamespace(prefix='ansible_') |
| 45 |
|
| 46 |
Has a 'from_gather_subset() constructor that populates collectors based on a |
| 47 |
gather_subset specifier.""" |
| 48 |
|
| 49 |
def __init__(self, collectors=None, namespace=None, filter_spec=None): |
| 50 |
|
| 51 |
super(AnsibleFactCollector, self).__init__(collectors=collectors, |
| 52 |
namespace=namespace) |
| 53 |
|
| 54 |
self.filter_spec = filter_spec |
| 55 |
|
| 56 |
def _filter(self, facts_dict, filter_spec): |
| 57 |
# assume filter_spec='' or filter_spec=[] is equivalent to filter_spec='*' |
| 58 |
if not filter_spec or filter_spec == '*': |
| 59 |
return facts_dict |
| 60 |
|
| 61 |
if is_string(filter_spec): |
| 62 |
filter_spec = [filter_spec] |
| 63 |
|
| 64 |
found = [] |
| 65 |
for f in filter_spec: |
| 66 |
for x, y in facts_dict.items(): |
| 67 |
if not f or fnmatch.fnmatch(x, f): |
| 68 |
found.append((x, y)) |
| 69 |
elif not f.startswith(('ansible_', 'facter', 'ohai')): |
| 70 |
# try to match with ansible_ prefix added when non empty |
| 71 |
g = 'ansible_%s' % f |
| 72 |
if fnmatch.fnmatch(x, g): |
| 73 |
found.append((x, y)) |
| 74 |
return found |
| 75 |
|
| 76 |
def collect(self, module=None, collected_facts=None): |
| 77 |
collected_facts = collected_facts or {} |
| 78 |
|
| 79 |
facts_dict = {} |
| 80 |
|
| 81 |
for collector_obj in self.collectors: |
| 82 |
info_dict = {} |
| 83 |
|
| 84 |
try: |
| 85 |
|
| 86 |
# Note: this collects with namespaces, so collected_facts also includes namespaces |
| 87 |
info_dict = collector_obj.collect_with_namespace(module=module, |
| 88 |
collected_facts=collected_facts) |
| 89 |
except Exception as e: |
| 90 |
sys.stderr.write(repr(e)) |
| 91 |
sys.stderr.write('\n') |
| 92 |
|
| 93 |
# shallow copy of the new facts to pass to each collector in collected_facts so facts |
| 94 |
# can reference other facts they depend on. |
| 95 |
collected_facts.update(info_dict.copy()) |
| 96 |
|
| 97 |
# NOTE: If we want complicated fact dict merging, this is where it would hook in |
| 98 |
facts_dict.update(self._filter(info_dict, self.filter_spec)) |
| 99 |
|
| 100 |
return facts_dict |
| 101 |
|
| 102 |
|
| 103 |
class CollectorMetaDataCollector(collector.BaseFactCollector): |
| 104 |
"""Collector that provides a facts with the gather_subset metadata.""" |
| 105 |
|
| 106 |
name = 'gather_subset' |
| 107 |
_fact_ids = set() # type: t.Set[str] |
| 108 |
|
| 109 |
def __init__(self, collectors=None, namespace=None, gather_subset=None, module_setup=None): |
| 110 |
super(CollectorMetaDataCollector, self).__init__(collectors, namespace) |
| 111 |
self.gather_subset = gather_subset |
| 112 |
self.module_setup = module_setup |
| 113 |
|
| 114 |
def collect(self, module=None, collected_facts=None): |
| 115 |
# NOTE: deprecate/remove once DT lands |
| 116 |
# we can return this data, but should not be top level key |
| 117 |
meta_facts = {'gather_subset': self.gather_subset} |
| 118 |
|
| 119 |
# NOTE: this is just a boolean indicator that 'facts were gathered' |
| 120 |
# and should be moved to the 'gather_facts' action plugin |
| 121 |
# probably revised to handle modules/subsets combos |
| 122 |
if self.module_setup: |
| 123 |
meta_facts['module_setup'] = self.module_setup |
| 124 |
return meta_facts |
| 125 |
|
| 126 |
|
| 127 |
def get_ansible_collector(all_collector_classes, |
| 128 |
namespace=None, |
| 129 |
filter_spec=None, |
| 130 |
gather_subset=None, |
| 131 |
gather_timeout=None, |
| 132 |
minimal_gather_subset=None): |
| 133 |
|
| 134 |
filter_spec = filter_spec or [] |
| 135 |
gather_subset = gather_subset or ['all'] |
| 136 |
gather_timeout = gather_timeout or timeout.DEFAULT_GATHER_TIMEOUT |
| 137 |
minimal_gather_subset = minimal_gather_subset or frozenset() |
| 138 |
|
| 139 |
collector_classes = \ |
| 140 |
collector.collector_classes_from_gather_subset( |
| 141 |
all_collector_classes=all_collector_classes, |
| 142 |
minimal_gather_subset=minimal_gather_subset, |
| 143 |
gather_subset=gather_subset, |
| 144 |
gather_timeout=gather_timeout) |
| 145 |
|
| 146 |
collectors = [] |
| 147 |
for collector_class in collector_classes: |
| 148 |
collector_obj = collector_class(namespace=namespace) |
| 149 |
collectors.append(collector_obj) |
| 150 |
|
| 151 |
# Add a collector that knows what gather_subset we used so it it can provide a fact |
| 152 |
collector_meta_data_collector = \ |
| 153 |
CollectorMetaDataCollector(gather_subset=gather_subset, |
| 154 |
module_setup=True) |
| 155 |
collectors.append(collector_meta_data_collector) |
| 156 |
|
| 157 |
fact_collector = \ |
| 158 |
AnsibleFactCollector(collectors=collectors, |
| 159 |
filter_spec=filter_spec, |
| 160 |
namespace=namespace) |
| 161 |
|
| 162 |
return fact_collector |