From bb599620612e504024a5b8636da59ce6316b59df Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Mon, 23 Oct 2023 05:11:00 +1300 Subject: [PATCH] facter_facts: new module (#7356) * facter: add option to return facts instead of output variables * Update plugins/modules/facter.py Co-authored-by: Felix Fontein * add output doc * add changelog frag * reversed facter.py, created facter_facts.py * remove chglog fragment for new module * add BOTMETA entry * adjust facters code to facters_fact, add unit test * Update plugins/modules/facter_facts.py Co-authored-by: Felix Fontein * Update plugins/modules/facter_facts.py Co-authored-by: Felix Fontein * Update plugins/modules/facter_facts.py Co-authored-by: Felix Fontein --------- Co-authored-by: Felix Fontein --- .github/BOTMETA.yml | 3 + plugins/modules/facter_facts.py | 90 +++++++++++++++++++ .../unit/plugins/modules/test_facter_facts.py | 16 ++++ .../plugins/modules/test_facter_facts.yaml | 40 +++++++++ 4 files changed, 149 insertions(+) create mode 100644 plugins/modules/facter_facts.py create mode 100644 tests/unit/plugins/modules/test_facter_facts.py create mode 100644 tests/unit/plugins/modules/test_facter_facts.yaml diff --git a/.github/BOTMETA.yml b/.github/BOTMETA.yml index a6470f4950..fcde2f0608 100644 --- a/.github/BOTMETA.yml +++ b/.github/BOTMETA.yml @@ -497,6 +497,9 @@ files: $modules/facter.py: labels: facter maintainers: $team_ansible_core gamethis + $modules/facter_facts.py: + labels: facter + maintainers: russoz $team_ansible_core gamethis $modules/filesize.py: maintainers: quidame $modules/filesystem.py: diff --git a/plugins/modules/facter_facts.py b/plugins/modules/facter_facts.py new file mode 100644 index 0000000000..abc3f87ebe --- /dev/null +++ b/plugins/modules/facter_facts.py @@ -0,0 +1,90 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Copyright (c) 2023, Alexei Znamensky +# Copyright (c) 2012, Michael DeHaan +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +DOCUMENTATION = ''' +--- +module: facter_facts +short_description: Runs the discovery program C(facter) on the remote system and return Ansible facts +version_added: 8.0.0 +description: + - Runs the C(facter) discovery program + (U(https://github.com/puppetlabs/facter)) on the remote system, returning Ansible facts from the + JSON data that can be useful for inventory purposes. +extends_documentation_fragment: + - community.general.attributes + - community.general.attributes.facts + - community.general.attributes.facts_module +options: + arguments: + description: + - Specifies arguments for facter. + type: list + elements: str +requirements: + - facter + - ruby-json +author: + - Ansible Core Team + - Michael DeHaan +''' + +EXAMPLES = ''' +- name: Execute facter no arguments + community.general.facter_facts: + +- name: Execute facter with arguments + community.general.facter_facts: + arguments: + - -p + - system_uptime + - timezone + - is_virtual +''' + +RETURN = r''' +ansible_facts: + description: Dictionary with one key C(facter). + returned: always + type: dict + contains: + facter: + description: Dictionary containing facts discovered in the remote system. + returned: always + type: dict +''' + +import json + +from ansible.module_utils.basic import AnsibleModule + + +def main(): + module = AnsibleModule( + argument_spec=dict( + arguments=dict(type='list', elements='str'), + ), + supports_check_mode=True, + ) + + facter_path = module.get_bin_path( + 'facter', + opt_dirs=['/opt/puppetlabs/bin']) + + cmd = [facter_path, "--json"] + if module.params['arguments']: + cmd += module.params['arguments'] + + rc, out, err = module.run_command(cmd, check_rc=True) + module.exit_json(ansible_facts=dict(facter=json.loads(out))) + + +if __name__ == '__main__': + main() diff --git a/tests/unit/plugins/modules/test_facter_facts.py b/tests/unit/plugins/modules/test_facter_facts.py new file mode 100644 index 0000000000..311030465f --- /dev/null +++ b/tests/unit/plugins/modules/test_facter_facts.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +# Copyright (c) Alexei Znamensky (russoz@gmail.com) +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + + +from ansible_collections.community.general.plugins.modules import facter_facts +from .helper import Helper + + +helper = Helper.from_file(facter_facts.main, "tests/unit/plugins/modules/test_facter_facts.yaml") +patch_bin = helper.cmd_fixture +test_module = helper.test_module diff --git a/tests/unit/plugins/modules/test_facter_facts.yaml b/tests/unit/plugins/modules/test_facter_facts.yaml new file mode 100644 index 0000000000..c287fdcfda --- /dev/null +++ b/tests/unit/plugins/modules/test_facter_facts.yaml @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +# Copyright (c) Alexei Znamensky (russoz@gmail.com) +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +--- +- id: simple run + output: + ansible_facts: + facter: + a: 1 + b: 2 + c: 3 + run_command_calls: + - command: [/testbin/facter, --json] + environ: &env-def {check_rc: true} + rc: 0 + out: > + { "a": 1, "b": 2, "c": 3 } + err: "" +- id: with args + input: + arguments: + - -p + - system_uptime + - timezone + - is_virtual + output: + ansible_facts: + facter: + a: 1 + b: 2 + c: 3 + run_command_calls: + - command: [/testbin/facter, --json, -p, system_uptime, timezone, is_virtual] + environ: *env-def + rc: 0 + out: > + { "a": 1, "b": 2, "c": 3 } + err: ""