Debian Codegraph @

ansible_collections/community/general/plugins/modules/facter_facts.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2023, Alexei Znamensky
4 # Copyright (c) 2012, Michael DeHaan <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import annotations
9
10 DOCUMENTATION = r"""
11 module: facter_facts
12 short_description: Runs the discovery program C(facter) on the remote system and return Ansible facts
13 version_added: 8.0.0
14 description:
15 - Runs the C(facter) discovery program (U(https://github.com/puppetlabs/facter)) on the remote system, returning Ansible
16 facts from the JSON data that can be useful for inventory purposes.
17 extends_documentation_fragment:
18 - community.general._attributes
19 - community.general._attributes.facts
20 - community.general._attributes.facts_module
21 options:
22 arguments:
23 description:
24 - Specifies arguments for facter.
25 type: list
26 elements: str
27 requirements:
28 - facter
29 - ruby-json
30 author:
31 - Ansible Core Team
32 - Michael DeHaan
33 """
34
35 EXAMPLES = r"""
36 - name: Execute facter no arguments
37 community.general.facter_facts:
38
39 - name: Execute facter with arguments
40 community.general.facter_facts:
41 arguments:
42 - -p
43 - system_uptime
44 - timezone
45 - is_virtual
46 """
47
48 RETURN = r"""
49 ansible_facts:
50 description: Dictionary with one key C(facter).
51 returned: success
52 type: dict
53 contains:
54 facter:
55 description: Dictionary containing facts discovered in the remote system.
56 returned: success
57 type: dict
58 """
59
60 import json
61
62 from ansible.module_utils.basic import AnsibleModule
63
64
65 def main():
66 module = AnsibleModule(
67 argument_spec=dict(
68 arguments=dict(type="list", elements="str"),
69 ),
70 supports_check_mode=True,
71 )
72 module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"}
73
74 facter_path = module.get_bin_path("facter", opt_dirs=["/opt/puppetlabs/bin"])
75
76 cmd = [facter_path, "--json"]
77 if module.params["arguments"]:
78 cmd += module.params["arguments"]
79
80 rc, out, err = module.run_command(cmd, check_rc=True)
81 module.exit_json(ansible_facts=dict(facter=json.loads(out)))
82
83
84 if __name__ == "__main__":
85 main()