diff --git a/changelogs/fragments/8987-legacycrypt.yml b/changelogs/fragments/8987-legacycrypt.yml new file mode 100644 index 0000000000..ce955f3564 --- /dev/null +++ b/changelogs/fragments/8987-legacycrypt.yml @@ -0,0 +1,3 @@ +bugfixes: + - "homectl - the module now tries to use ``legacycrypt`` on Python 3.13+ (https://github.com/ansible-collections/community.general/issues/4691, https://github.com/ansible-collections/community.general/pull/8987)." + - "udm_user - the module now tries to use ``legacycrypt`` on Python 3.13+ (https://github.com/ansible-collections/community.general/issues/4690, https://github.com/ansible-collections/community.general/pull/8987)." diff --git a/plugins/modules/homectl.py b/plugins/modules/homectl.py index 7751651c85..58176f3389 100644 --- a/plugins/modules/homectl.py +++ b/plugins/modules/homectl.py @@ -18,11 +18,11 @@ version_added: 4.4.0 description: - Manages a user's home directory managed by systemd-homed. notes: - - This module does B(not) work with Python 3.13 or newer. It uses the deprecated L(crypt Python module, - https://docs.python.org/3.12/library/crypt.html) from the Python standard library, which was removed - from Python 3.13. + - This module requires the deprecated L(crypt Python module, + https://docs.python.org/3.12/library/crypt.html) library which was removed from Python 3.13. + For Python 3.13 or newer, you need to install L(legacycrypt, https://pypi.org/project/legacycrypt/). requirements: - - Python 3.12 or earlier + - legacycrypt (on Python 3.13 or newer) extends_documentation_fragment: - community.general.attributes attributes: @@ -284,6 +284,17 @@ else: HAS_CRYPT = True CRYPT_IMPORT_ERROR = None +try: + import legacycrypt + if not HAS_CRYPT: + crypt = legacycrypt +except ImportError: + HAS_LEGACYCRYPT = False + LEGACYCRYPT_IMPORT_ERROR = traceback.format_exc() +else: + HAS_LEGACYCRYPT = True + LEGACYCRYPT_IMPORT_ERROR = None + class Homectl(object): '''#TODO DOC STRINGS''' @@ -606,9 +617,9 @@ def main(): ] ) - if not HAS_CRYPT: + if not HAS_CRYPT and not HAS_LEGACYCRYPT: module.fail_json( - msg=missing_required_lib('crypt (part of Python 3.13 standard library)'), + msg=missing_required_lib('crypt (part of standard library up to Python 3.12) or legacycrypt (PyPI)'), exception=CRYPT_IMPORT_ERROR, ) diff --git a/plugins/modules/udm_user.py b/plugins/modules/udm_user.py index 5a2e090497..5257a22028 100644 --- a/plugins/modules/udm_user.py +++ b/plugins/modules/udm_user.py @@ -21,11 +21,11 @@ description: server (UCS). It uses the python API of the UCS to create a new object or edit it." notes: - - This module does B(not) work with Python 3.13 or newer. It uses the deprecated L(crypt Python module, - https://docs.python.org/3.12/library/crypt.html) from the Python standard library, which was removed - from Python 3.13. + - This module requires the deprecated L(crypt Python module, + https://docs.python.org/3.12/library/crypt.html) library which was removed from Python 3.13. + For Python 3.13 or newer, you need to install L(legacycrypt, https://pypi.org/project/legacycrypt/). requirements: - - Python 3.12 or earlier + - legacycrypt (on Python 3.13 or newer) extends_documentation_fragment: - community.general.attributes attributes: @@ -350,6 +350,17 @@ else: HAS_CRYPT = True CRYPT_IMPORT_ERROR = None +try: + import legacycrypt + if not HAS_CRYPT: + crypt = legacycrypt +except ImportError: + HAS_LEGACYCRYPT = False + LEGACYCRYPT_IMPORT_ERROR = traceback.format_exc() +else: + HAS_LEGACYCRYPT = True + LEGACYCRYPT_IMPORT_ERROR = None + def main(): expiry = date.strftime(date.today() + timedelta(days=365), "%Y-%m-%d") @@ -467,10 +478,10 @@ def main(): ]) ) - if not HAS_CRYPT: + if not HAS_CRYPT and not HAS_LEGACYCRYPT: module.fail_json( - msg=missing_required_lib('crypt (part of Python 3.13 standard library)'), - exception=CRYPT_IMPORT_ERROR, + msg=missing_required_lib('crypt (part of standard library up to Python 3.12) or legacycrypt (PyPI)'), + exception=LEGACYCRYPT_IMPORT_ERROR, ) username = module.params['username'] diff --git a/tests/integration/targets/homectl/tasks/main.yml b/tests/integration/targets/homectl/tasks/main.yml index 93c1089b47..aa924293e3 100644 --- a/tests/integration/targets/homectl/tasks/main.yml +++ b/tests/integration/targets/homectl/tasks/main.yml @@ -15,6 +15,11 @@ ignore_errors: true - block: + - name: Install legacycrypt on Python 3.13+ + pip: + name: legacycrypt + when: ansible_python_version is version("3.13", ">=") + - name: Check and start systemd-homed service service: name: systemd-homed.service