From d304fd86c2ce1e3f2fd70a8e9f22a9b6b240311d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Knecht?= Date: Tue, 9 Apr 2024 22:11:43 +0200 Subject: [PATCH] modules/dnf: Substitute variables in DNF cache path (#80094) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The cache directory can be specified with variables that are expanded by DNF, for example, ``` cachedir=/var/cache/yum/$basearch/$releasever ``` But the `dnf` module would use that path literally, instead of replacing `$basearch` and `$releasever` with their values. This commit ensures that variables in `cachedir` are properly substituted. Signed-off-by: BenoƮt Knecht --- changelogs/fragments/dnf_cache_path.yml | 3 +++ lib/ansible/modules/dnf.py | 6 ++++++ .../_util/controller/sanity/mypy/ansible-core.ini | 3 +++ .../ansible_test/_util/controller/sanity/mypy/modules.ini | 3 +++ 4 files changed, 15 insertions(+) create mode 100644 changelogs/fragments/dnf_cache_path.yml diff --git a/changelogs/fragments/dnf_cache_path.yml b/changelogs/fragments/dnf_cache_path.yml new file mode 100644 index 00000000000..50b72f9a109 --- /dev/null +++ b/changelogs/fragments/dnf_cache_path.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - dnf - Substitute variables in DNF cache path (https://github.com/ansible/ansible/pull/80094). diff --git a/lib/ansible/modules/dnf.py b/lib/ansible/modules/dnf.py index fb9fa6ae5d2..16b3bcc2b41 100644 --- a/lib/ansible/modules/dnf.py +++ b/lib/ansible/modules/dnf.py @@ -403,6 +403,7 @@ from ansible.module_utils.yumdnf import YumDnf, yumdnf_argument_spec # to set proper locale before importing dnf to be able to scrape # the output in some cases (FIXME?). dnf = None +libdnf = None class DnfModule(YumDnf): @@ -483,6 +484,7 @@ class DnfModule(YumDnf): os.environ['LANGUAGE'] = os.environ['LANG'] = locale global dnf + global libdnf try: import dnf import dnf.const @@ -490,6 +492,7 @@ class DnfModule(YumDnf): import dnf.package import dnf.subject import dnf.util + import libdnf HAS_DNF = True except ImportError: HAS_DNF = False @@ -557,6 +560,9 @@ class DnfModule(YumDnf): # Load substitutions from the filesystem conf.substitutions.update_from_etc(installroot) + # Substitute variables in cachedir path + conf.cachedir = libdnf.conf.ConfigParser.substitute(conf.cachedir, conf.substitutions) + # Handle different DNF versions immutable mutable datatypes and # dnf v1/v2/v3 # diff --git a/test/lib/ansible_test/_util/controller/sanity/mypy/ansible-core.ini b/test/lib/ansible_test/_util/controller/sanity/mypy/ansible-core.ini index 0251f674b51..0a8a3e6b40f 100644 --- a/test/lib/ansible_test/_util/controller/sanity/mypy/ansible-core.ini +++ b/test/lib/ansible_test/_util/controller/sanity/mypy/ansible-core.ini @@ -58,6 +58,9 @@ ignore_missing_imports = True [mypy-dnf.*] ignore_missing_imports = True +[mypy-libdnf.*] +ignore_missing_imports = True + [mypy-apt.*] ignore_missing_imports = True diff --git a/test/lib/ansible_test/_util/controller/sanity/mypy/modules.ini b/test/lib/ansible_test/_util/controller/sanity/mypy/modules.ini index b4e7b05eb9f..ddabecdabb6 100644 --- a/test/lib/ansible_test/_util/controller/sanity/mypy/modules.ini +++ b/test/lib/ansible_test/_util/controller/sanity/mypy/modules.ini @@ -25,6 +25,9 @@ ignore_missing_imports = True [mypy-dnf.*] ignore_missing_imports = True +[mypy-libdnf.*] +ignore_missing_imports = True + [mypy-apt.*] ignore_missing_imports = True