mirror of
https://github.com/ansible-collections/ansible.posix.git
synced 2026-06-09 18:15:54 +00:00
Fix all deprecated module_utils imports before ansible-core 2.24 removal
SUMMARY
Fixes all deprecated ansible.module_utils imports across the entire collection that will be removed in ansible-core 2.24.
This PR comprehensively addresses deprecation warnings reported in #686 by updating import statements in 20 files to use the new recommended import paths, and removes 8 unused test utility files that contained deprecated imports.
Deprecated imports replaced:
Deprecated import
Replacement
ansible.module_utils._text
ansible.module_utils.common.text.converters
ansible.module_utils.common._collections_compat
collections.abc
ansible.module_utils.six.moves.shlex_quote
shlex.quote
ansible.module_utils.six.moves.reduce
functools.reduce
ansible.module_utils.six.moves.urllib.parse.urlparse
urllib.parse.urlparse
ansible.module_utils.six.string_types
basestring/str (Python 2/3 compatible)
ansible.module_utils.six.text_type
str
ansible.module_utils.six.PY3
Removed (simplified Python 2/3 conditionals)
ansible.module_utils.six.with_metaclass
Native metaclass= syntax
ansible.module_utils.six.iteritems
dict.items()
Files fixed (20 files, 1 commit per file for easier review):
plugins/action/patch.py
plugins/action/synchronize.py
plugins/callback/cgroup_perf_recap.py
plugins/callback/json.py
plugins/callback/jsonl.py
plugins/callback/profile_roles.py
plugins/callback/profile_tasks.py
plugins/modules/acl.py
plugins/modules/authorized_key.py
plugins/modules/firewalld_info.py
plugins/modules/mount.py
plugins/modules/patch.py
plugins/modules/rhel_rpm_ostree.py
plugins/modules/rpm_ostree_upgrade.py
plugins/modules/seboolean.py
plugins/modules/synchronize.py
plugins/modules/sysctl.py
plugins/shell/csh.py
plugins/shell/fish.py
tests/unit/modules/system/test_mount.py
Files deleted (8 unused test utility files):
These files are dead code - none of them are imported or used anywhere in the test suite or the collection. Removing them also addresses Python 2.7 compatibility concerns raised in code review, as several contained deprecated imports that would be incorrect to fix for Python 2.
tests/unit/compat/builtins.py
tests/unit/mock/loader.py
tests/unit/mock/path.py
tests/unit/mock/procenv.py
tests/unit/mock/vault_helper.py
tests/unit/mock/yaml_helper.py
tests/unit/modules/conftest.py
tests/unit/modules/utils.py
Completeness verified with:
git grep -n -P '_compat|utils._text|utils.six' -- '*.py' | grep -v yml
This command returns no results, confirming all deprecated imports have been replaced.
Notes on Python 2.7 compatibility:
For modules that may run on Python 2.7 managed hosts (e.g., authorized_key.py, synchronize.py, sysctl.py), Python 2/3 compatible fallbacks were used instead of direct Python 3 replacements:
authorized_key.py: try/except ImportError for urllib.parse.urlparse (falls back to urlparse on Python 2)
synchronize.py: try/except ImportError for shlex.quote (falls back to pipes.quote on Python 2)
sysctl.py: uses sys.version_info to set string_types to str on Python 3 (basestring on Python 2)
Also removes corresponding pylint:ansible-bad-import-from entries from tests/sanity/ignore-2.21.txt and tests/sanity/ignore-2.22.txt where applicable.
Fixes #686
ISSUE TYPE
Bugfix Pull Request
ADDITIONAL INFORMATION
Approach:
Each file is fixed in a separate commit for easier code review. The changelog fragment is added in a final commit. Corresponding pylint:ansible-bad-import-from ignore entries in tests/sanity/ignore-2.21.txt and tests/sanity/ignore-2.22.txt are removed in the same commit as the file fix (or the file removal commit).
CI results:
All 59 checks passing (Azure Pipelines sanity, units, lint, Docker, Remote across ansible-core 2.17 through devel, and Zuul ansible/check).
Reviewed-by: Felix Fontein <felix@fontein.de>
Reviewed-by: Pavel Bar
Reviewed-by: Abhijeet Kasurde
(cherry picked from commit 2022c1bd86)
Co-authored-by: centosinfra-prod-github-app[bot] <161850885+centosinfra-prod-github-app[bot]@users.noreply.github.com>
123 lines
3.2 KiB
Python
123 lines
3.2 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright: Red Hat Inc.
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
__metaclass__ = type
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
---
|
|
module: rpm_ostree_upgrade
|
|
short_description: Manage rpm-ostree upgrade transactions
|
|
description:
|
|
- Manage an rpm-ostree upgrade transactions.
|
|
version_added: 1.5.0
|
|
author:
|
|
- Adam Miller (@maxamillion)
|
|
requirements:
|
|
- rpm-ostree
|
|
options:
|
|
os:
|
|
description:
|
|
- The OSNAME upon which to operate.
|
|
type: str
|
|
default: ""
|
|
required: false
|
|
cache_only:
|
|
description:
|
|
- Perform the transaction using only pre-cached data, do not download.
|
|
type: bool
|
|
default: false
|
|
required: false
|
|
allow_downgrade:
|
|
description:
|
|
- Allow for the upgrade to be a chronologically older tree.
|
|
type: bool
|
|
default: false
|
|
required: false
|
|
peer:
|
|
description:
|
|
- Force peer-to-peer connection instead of using a system message bus.
|
|
type: bool
|
|
default: false
|
|
required: false
|
|
|
|
'''
|
|
|
|
EXAMPLES = '''
|
|
- name: Upgrade the rpm-ostree image without options, accept all defaults
|
|
ansible.posix.rpm_ostree_upgrade:
|
|
|
|
- name: Upgrade the rpm-ostree image allowing downgrades
|
|
ansible.posix.rpm_ostree_upgrade:
|
|
allow_downgrade: true
|
|
'''
|
|
|
|
RETURN = '''
|
|
msg:
|
|
description: The command standard output
|
|
returned: always
|
|
type: str
|
|
sample: 'No upgrade available.'
|
|
'''
|
|
|
|
import os
|
|
import traceback
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
from ansible.module_utils.common.text.converters import to_native, to_text
|
|
|
|
|
|
def rpm_ostree_transaction(module):
|
|
cmd = []
|
|
cmd.append(module.get_bin_path("rpm-ostree"))
|
|
cmd.append('upgrade')
|
|
|
|
if module.params['os']:
|
|
cmd += ['--os', module.params['os']]
|
|
if module.params['cache_only']:
|
|
cmd += ['--cache-only']
|
|
if module.params['allow_downgrade']:
|
|
cmd += ['--allow-downgrade']
|
|
if module.params['peer']:
|
|
cmd += ['--peer']
|
|
|
|
module.run_command_environ_update = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C')
|
|
|
|
rc, out, err = module.run_command(cmd)
|
|
|
|
if rc != 0:
|
|
module.fail_json(rc=rc, msg=err)
|
|
else:
|
|
if to_text("No upgrade available.") in to_text(out):
|
|
module.exit_json(msg=out, changed=False)
|
|
else:
|
|
module.exit_json(msg=out, changed=True)
|
|
|
|
|
|
def main():
|
|
module = AnsibleModule(
|
|
argument_spec=dict(
|
|
os=dict(type='str', default=''),
|
|
cache_only=dict(type='bool', default=False),
|
|
allow_downgrade=dict(type='bool', default=False),
|
|
peer=dict(type='bool', default=False),
|
|
),
|
|
)
|
|
|
|
# Verify that the platform is an rpm-ostree based system
|
|
if not os.path.exists("/run/ostree-booted"):
|
|
module.fail_json(msg="Module rpm_ostree_upgrade is only applicable for rpm-ostree based systems.")
|
|
|
|
try:
|
|
rpm_ostree_transaction(module)
|
|
except Exception as e:
|
|
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|