Debian Codegraph @

lib/ansible/module_utils/facts/system/local.py

raw
1 # Copyright: Contributors to the Ansible project
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 from __future__ import annotations
5
6 import configparser
7 import glob
8 import json
9 import os
10 import stat
11 import typing as t
12
13 from io import StringIO
14
15 from ansible.module_utils.common.text.converters import to_text
16 from ansible.module_utils.facts.utils import get_file_content
17 from ansible.module_utils.facts.collector import BaseFactCollector
18
19
20 class LocalFactCollector(BaseFactCollector):
21 name = 'local'
22 _fact_ids = set() # type: t.Set[str]
23
24 def collect(self, module=None, collected_facts=None):
25 local_facts = {}
26 local_facts['local'] = {}
27
28 if not module:
29 return local_facts
30
31 fact_path = module.params.get('fact_path', None)
32
33 if not fact_path or not os.path.exists(fact_path):
34 return local_facts
35
36 local = {}
37 # go over .fact files, run executables, read rest, skip bad with warning and note
38 for fn in sorted(glob.glob(fact_path + '/*.fact')):
39 # use filename for key where it will sit under local facts
40 fact_base = os.path.basename(fn).replace('.fact', '')
41 failed = None
42 try:
43 executable_fact = stat.S_IXUSR & os.stat(fn)[stat.ST_MODE]
44 except OSError as e:
45 failed = 'Could not stat fact (%s): %s' % (fn, to_text(e))
46 local[fact_base] = failed
47 module.warn(failed)
48 continue
49 if executable_fact:
50 try:
51 # run it
52 rc, out, err = module.run_command(fn)
53 if rc != 0:
54 failed = 'Failure executing fact script (%s), rc: %s, err: %s' % (fn, rc, err)
55 except OSError as e:
56 failed = 'Could not execute fact script (%s): %s' % (fn, to_text(e))
57
58 if failed is not None:
59 local[fact_base] = failed
60 module.warn(failed)
61 continue
62 else:
63 # ignores exceptions and returns empty
64 out = get_file_content(fn, default='')
65
66 try:
67 # ensure we have unicode
68 out = to_text(out, errors='surrogate_or_strict')
69 except UnicodeError:
70 fact = 'error loading fact - output of running "%s" was not utf-8' % fn
71 local[fact_base] = fact
72 module.warn(fact)
73 continue
74
75 # try to read it as json first
76 try:
77 fact = json.loads(out)
78 except ValueError:
79 # if that fails read it with ConfigParser
80 cp = configparser.ConfigParser()
81 try:
82 cp.read_file(StringIO(out))
83 except configparser.Error:
84 fact = f"error loading facts as JSON or ini - please check content: {fn}"
85 module.warn(fact)
86 else:
87 fact = {}
88 for sect in cp.sections():
89 if sect not in fact:
90 fact[sect] = {}
91 for opt in cp.options(sect):
92 try:
93 val = cp.get(sect, opt)
94 except configparser.Error as ex:
95 fact = f"error loading facts as ini - please check content: {fn} ({ex})"
96 module.warn(fact)
97 continue
98 else:
99 fact[sect][opt] = val
100 except Exception as e:
101 fact = "Failed to convert (%s) to JSON: %s" % (fn, to_text(e))
102 module.warn(fact)
103
104 local[fact_base] = fact
105
106 local_facts['local'] = local
107 return local_facts