From 5e80355057c7642cb9089461361701edfa71e896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Bompard?= Date: Wed, 10 May 2023 17:45:57 +0200 Subject: [PATCH] Add a fact module to get information about python3 installations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Aurélien Bompard --- library/python3_fact.py | 66 +++++++++++++++++++ roles/ansible-server/templates/ansible.cfg.j2 | 1 + 2 files changed, 67 insertions(+) create mode 100644 library/python3_fact.py diff --git a/library/python3_fact.py b/library/python3_fact.py new file mode 100644 index 0000000000..ca5b7a4aaf --- /dev/null +++ b/library/python3_fact.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 + +from distutils.sysconfig import get_python_lib + +from ansible.module_utils.basic import AnsibleModule + + +DOCUMENTATION = r''' +--- +module: python3_fact + +short_description: Add Ansible facts about the Python3 installation + +# If this is part of a collection, you need to use semantic versioning, +# i.e. the version is of the form "2.5.0" and not "2.4". +version_added: "1.0.0" + +description: Ansible facts will be added about the following Python3 + +author: + - Aurelien Bompard (@abompard) +''' + +EXAMPLES = r''' +# In ansible.cfg + +facts_modules = smart, python3_fact + +# Ansible facts dump: + +$ ansible -m debug -a var=ansible_facts hostname +"ansible_facts": { + ... + "python3": { + "sitelib": "/usr/lib/python3.11/site-packages" + }, + ... +} +''' + +RETURN = r''' +sitelib: + description: The full path to the site-packages directory. + type: str + returned: always + sample: '/usr/lib/python3.11/site-packages' +''' + + +def run_module(): + result = { + "sitelib": get_python_lib(), + } + module = AnsibleModule( + argument_spec={}, + supports_check_mode=True + ) + module.exit_json(changed=False, ansible_facts=dict(python3=result)) + + +def main(): + run_module() + + +if __name__ == '__main__': + main() diff --git a/roles/ansible-server/templates/ansible.cfg.j2 b/roles/ansible-server/templates/ansible.cfg.j2 index 133b63afd6..19e02cba96 100644 --- a/roles/ansible-server/templates/ansible.cfg.j2 +++ b/roles/ansible-server/templates/ansible.cfg.j2 @@ -333,6 +333,7 @@ vars_plugins={{ ansible_base }}/ansible/vars_plugins:~/.ansible/plugins/vars:/us # (list) Which modules to run during a play's fact gathering stage, using the default of 'smart' will try to figure it out based on connection type. ;facts_modules=smart +facts_modules=smart, python3_fact # (boolean) Set this to "False" if you want to avoid host key checking by the underlying tools Ansible uses to connect to the host ;host_key_checking=True