1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

pacman: add support for remove_nosave (#4316) (#4329)

* pacman: add support for remove_nosave

New parameter: remove_nosave
When enabled, will pass --nosave to pacman when removing packages.
--nosave cannot be used with --print-format and thus it couldn't be
passed via extra_args. See #4315

The code adds the option right before the actual removal of the pkgs.

(This is based on an initial diff from MorphBonehunter)

* changelog

* Update plugins/modules/packaging/os/pacman.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* wording

* ssss

* remove_package: simplify {force,extra,nosave}_args

Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit c698ecd201)

Co-authored-by: Jean Raby <jean@raby.sh>
This commit is contained in:
patchback[bot] 2022-03-08 07:39:31 +01:00 committed by GitHub
parent 77b7a65002
commit 012f684133
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 94 additions and 13 deletions

View file

@ -0,0 +1,3 @@
minor_changes:
- pacman - add ``remove_nosave`` parameter to avoid saving modified configuration files as ``.pacsave`` files.
(https://github.com/ansible-collections/community.general/pull/4316, https://github.com/ansible-collections/community.general/issues/4315).

View file

@ -43,10 +43,18 @@ options:
force: force:
description: description:
- When removing package, force remove package, without any checks. - When removing packages, forcefully remove them, without any checks.
Same as `extra_args="--nodeps --nodeps"`. Same as C(extra_args="--nodeps --nodeps").
When update_cache, force redownload repo databases. When combined with I(update_cache), force a refresh of all package databases.
Same as `update_cache_extra_args="--refresh --refresh"`. Same as C(update_cache_extra_args="--refresh --refresh").
default: no
type: bool
remove_nosave:
description:
- When removing packages, do not save modified configuration files as C(.pacsave) files.
(passes C(--nosave) to pacman)
version_added: 4.6.0
default: no default: no
type: bool type: bool
@ -339,7 +347,7 @@ class Pacman(object):
for p in name_ver: for p in name_ver:
# With Pacman v6.0.1 - libalpm v13.0.1, --upgrade outputs "loading packages..." on stdout. strip that. # With Pacman v6.0.1 - libalpm v13.0.1, --upgrade outputs "loading packages..." on stdout. strip that.
# When installing from URLs, pacman can also output a 'nothing to do' message. strip that too. # When installing from URLs, pacman can also output a 'nothing to do' message. strip that too.
if "loading packages" in p or 'there is nothing to do' in p: if "loading packages" in p or "there is nothing to do" in p:
continue continue
name, version = p.split() name, version = p.split()
if name in self.inventory["installed_pkgs"]: if name in self.inventory["installed_pkgs"]:
@ -398,8 +406,6 @@ class Pacman(object):
self.add_exit_infos("Installed %d package(s)" % len(installed_pkgs)) self.add_exit_infos("Installed %d package(s)" % len(installed_pkgs))
def remove_packages(self, pkgs): def remove_packages(self, pkgs):
force_args = ["--nodeps", "--nodeps"] if self.m.params["force"] else []
# filter out pkgs that are already absent # filter out pkgs that are already absent
pkg_names_to_remove = [p.name for p in pkgs if p.name in self.inventory["installed_pkgs"]] pkg_names_to_remove = [p.name for p in pkgs if p.name in self.inventory["installed_pkgs"]]
@ -411,10 +417,10 @@ class Pacman(object):
self.changed = True self.changed = True
cmd_base = [self.pacman_path, "--remove", "--noconfirm", "--noprogressbar"] cmd_base = [self.pacman_path, "--remove", "--noconfirm", "--noprogressbar"]
if self.m.params["extra_args"]: cmd_base += self.m.params["extra_args"]
cmd_base.extend(self.m.params["extra_args"]) cmd_base += ["--nodeps", "--nodeps"] if self.m.params["force"] else []
if force_args: # nosave_args conflicts with --print-format. Added later.
cmd_base.extend(force_args) # https://github.com/ansible-collections/community.general/issues/4315
# This is a bit of a TOCTOU but it is better than parsing the output of # This is a bit of a TOCTOU but it is better than parsing the output of
# pacman -R, which is different depending on the user config (VerbosePkgLists) # pacman -R, which is different depending on the user config (VerbosePkgLists)
@ -437,8 +443,8 @@ class Pacman(object):
self.add_exit_infos("Would have removed %d packages" % len(removed_pkgs)) self.add_exit_infos("Would have removed %d packages" % len(removed_pkgs))
return return
# actually do it nosave_args = ["--nosave"] if self.m.params["remove_nosave"] else []
cmd = cmd_base + pkg_names_to_remove cmd = cmd_base + nosave_args + pkg_names_to_remove
rc, stdout, stderr = self.m.run_command(cmd, check_rc=False) rc, stdout, stderr = self.m.run_command(cmd, check_rc=False)
if rc != 0: if rc != 0:
@ -678,6 +684,7 @@ def setup_module():
choices=["present", "installed", "latest", "absent", "removed"], choices=["present", "installed", "latest", "absent", "removed"],
), ),
force=dict(type="bool", default=False), force=dict(type="bool", default=False),
remove_nosave=dict(type="bool", default=False),
executable=dict(type="str", default="pacman"), executable=dict(type="str", default="pacman"),
extra_args=dict(type="str", default=""), extra_args=dict(type="str", default=""),
upgrade=dict(type="bool"), upgrade=dict(type="bool"),

View file

@ -9,3 +9,4 @@
# Add more tests here by including more task files: # Add more tests here by including more task files:
- include: 'basic.yml' - include: 'basic.yml'
- include: 'package_urls.yml' - include: 'package_urls.yml'
- include: 'remove_nosave.yml'

View file

@ -0,0 +1,70 @@
---
- vars:
package_name: xinetd
config_file: /etc/xinetd.conf
block:
- name: Make sure that {{ package_name }} is not installed
pacman:
name: '{{ package_name }}'
state: absent
- name: Make sure {{config_file}}.pacsave file doesn't exist
file:
path: '{{config_file}}.pacsave'
state: absent
- name: Install {{ package_name }}
pacman:
name: '{{ package_name }}'
state: present
- name: Modify {{config_file}}
blockinfile:
path: '{{config_file}}'
block: |
# something something
# on 2 lines
- name: Remove {{ package_name }} - generate pacsave
pacman:
name: '{{ package_name }}'
state: absent
- name: Make sure {{config_file}}.pacsave exists
stat:
path: '{{config_file}}.pacsave'
register: pacsave_st_1
- assert:
that:
- pacsave_st_1.stat.exists
- name: Delete {{config_file}}.pacsave
file:
path: '{{config_file}}.pacsave'
state: absent
- name: Install {{ package_name }}
pacman:
name: '{{ package_name }}'
state: present
- name: Modify {{config_file}}
blockinfile:
path: '{{config_file}}'
block: |
# something something
# on 2 lines
- name: Remove {{ package_name }} - nosave
pacman:
name: '{{ package_name }}'
remove_nosave: yes
state: absent
- name: Make sure {{config_file}}.pacsave does not exist
stat:
path: '{{config_file}}.pacsave'
register: pacsave_st_2
- assert:
that:
- not pacsave_st_2.stat.exists