mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
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 <felix@fontein.de> * 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 <felix@fontein.de> * Update plugins/modules/facter_facts.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/facter_facts.py Co-authored-by: Felix Fontein <felix@fontein.de> --------- Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
932b657ab8
commit
bb59962061
4 changed files with 149 additions and 0 deletions
3
.github/BOTMETA.yml
vendored
3
.github/BOTMETA.yml
vendored
|
@ -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:
|
||||
|
|
90
plugins/modules/facter_facts.py
Normal file
90
plugins/modules/facter_facts.py
Normal file
|
@ -0,0 +1,90 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright (c) 2023, Alexei Znamensky
|
||||
# Copyright (c) 2012, Michael DeHaan <michael.dehaan@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
|
||||
|
||||
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()
|
16
tests/unit/plugins/modules/test_facter_facts.py
Normal file
16
tests/unit/plugins/modules/test_facter_facts.py
Normal file
|
@ -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
|
40
tests/unit/plugins/modules/test_facter_facts.yaml
Normal file
40
tests/unit/plugins/modules/test_facter_facts.yaml
Normal file
|
@ -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: ""
|
Loading…
Reference in a new issue