From 6a76fe38e69e313986886325a491d2a88d73c980 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Fri, 4 Jan 2019 06:25:58 +1000 Subject: [PATCH] dnf - fix conf_file loading (#50515) (cherry picked from commit 63e454a4b25827f0c953999cb2f2c9f226f8a8c0) --- changelogs/fragments/dnf-conf-file.yaml | 2 + lib/ansible/modules/packaging/os/dnf.py | 22 +++---- test/integration/targets/dnf/tasks/dnf.yml | 69 +++++++++++----------- 3 files changed, 47 insertions(+), 46 deletions(-) create mode 100644 changelogs/fragments/dnf-conf-file.yaml diff --git a/changelogs/fragments/dnf-conf-file.yaml b/changelogs/fragments/dnf-conf-file.yaml new file mode 100644 index 00000000000..682d1efc679 --- /dev/null +++ b/changelogs/fragments/dnf-conf-file.yaml @@ -0,0 +1,2 @@ +bugfixes: +- dnf - fix issue where ``conf_file`` was not being loaded properly diff --git a/lib/ansible/modules/packaging/os/dnf.py b/lib/ansible/modules/packaging/os/dnf.py index 6483fe43dbc..45abb40c0b3 100644 --- a/lib/ansible/modules/packaging/os/dnf.py +++ b/lib/ansible/modules/packaging/os/dnf.py @@ -490,6 +490,17 @@ class DnfModule(YumDnf): conf = base.conf + # Change the configuration file path if provided, this must be done before conf.read() is called + if conf_file: + # Fail if we can't read the configuration file. + if not os.access(conf_file, os.R_OK): + self.module.fail_json( + msg="cannot read configuration file", conf_file=conf_file, + results=[], + ) + else: + conf.config_file_path = conf_file + # Read the configuration file conf.read() @@ -538,17 +549,6 @@ class DnfModule(YumDnf): if self.download_only: conf.downloadonly = True - # Change the configuration file path if provided - if conf_file: - # Fail if we can't read the configuration file. - if not os.access(conf_file, os.R_OK): - self.module.fail_json( - msg="cannot read configuration file", conf_file=conf_file, - results=[], - ) - else: - conf.config_file_path = conf_file - # Default in dnf upstream is true conf.clean_requirements_on_remove = self.autoremove diff --git a/test/integration/targets/dnf/tasks/dnf.yml b/test/integration/targets/dnf/tasks/dnf.yml index 7b9306af277..5ed4fb1a302 100644 --- a/test/integration/targets/dnf/tasks/dnf.yml +++ b/test/integration/targets/dnf/tasks/dnf.yml @@ -626,41 +626,40 @@ that: - "rpm_lsof_result is failed" -- name: exclude lsof - lineinfile: - dest: /etc/dnf/dnf.conf - regexp: (^exclude=)(.)* - line: "exclude=lsof*" - state: present - -# begin test case where disable_excludes is supported -- name: Try install lsof without disable_excludes - dnf: name=lsof state=latest - register: dnf_lsof_result - ignore_errors: True - -- name: verify lsof did not install because it is in exclude list - assert: - that: - - "dnf_lsof_result is failed" - -- name: install lsof with disable_excludes - dnf: name=lsof state=latest disable_excludes=all - register: dnf_lsof_result_using_excludes - -- name: verify lsof did install using disable_excludes=all - assert: - that: - - "dnf_lsof_result_using_excludes is success" - - "dnf_lsof_result_using_excludes is changed" - - "dnf_lsof_result_using_excludes is not failed" - -- name: remove exclude lsof (cleanup dnf.conf) - lineinfile: - dest: /etc/dnf/dnf.conf - regexp: (^exclude=lsof*) - line: "exclude=" - state: present +- name: create conf file that excludes lsof + copy: + content: | + [main] + exclude=lsof* + dest: '{{ output_dir }}/test-dnf.conf' + register: test_dnf_copy +- block: + # begin test case where disable_excludes is supported + - name: Try install lsof without disable_excludes + dnf: name=lsof state=latest conf_file={{ test_dnf_copy.dest }} + register: dnf_lsof_result + ignore_errors: True + + - name: verify lsof did not install because it is in exclude list + assert: + that: + - "dnf_lsof_result is failed" + + - name: install lsof with disable_excludes + dnf: name=lsof state=latest disable_excludes=all conf_file={{ test_dnf_copy.dest }} + register: dnf_lsof_result_using_excludes + + - name: verify lsof did install using disable_excludes=all + assert: + that: + - "dnf_lsof_result_using_excludes is success" + - "dnf_lsof_result_using_excludes is changed" + - "dnf_lsof_result_using_excludes is not failed" + always: + - name: remove exclude lsof conf file + file: + path: '{{ output_dir }}/test-dnf.conf' + state: absent # end test case where disable_excludes is supported