From 29f89b33ba9cad13de80dd986166c0266546bee7 Mon Sep 17 00:00:00 2001 From: Grey Christoforo Date: Thu, 8 Aug 2019 15:48:57 +0100 Subject: [PATCH] pacman: fix package state detection (#51311) --- lib/ansible/modules/packaging/os/pacman.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/packaging/os/pacman.py b/lib/ansible/modules/packaging/os/pacman.py index 46aefc5690d..6a6ef126bb9 100644 --- a/lib/ansible/modules/packaging/os/pacman.py +++ b/lib/ansible/modules/packaging/os/pacman.py @@ -171,11 +171,20 @@ def get_version(pacman_output): """Take pacman -Qi or pacman -Si output and get the Version""" lines = pacman_output.split('\n') for line in lines: - if 'Version' in line: + if line.startswith('Version '): return line.split(':')[1].strip() return None +def get_name(module, pacman_output): + """Take pacman -Qi or pacman -Si output and get the package name""" + lines = pacman_output.split('\n') + for line in lines: + if line.startswith('Name '): + return line.split(':')[1].strip() + module.fail_json(msg="get_name: fail to retrieve package name from pacman output") + + def query_package(module, pacman_path, name, state="present"): """Query the package status in both the local system and the repository. Returns a boolean to indicate if the package is installed, a second boolean to indicate if the package is up-to-date and a third boolean to indicate whether online information were available @@ -186,6 +195,12 @@ def query_package(module, pacman_path, name, state="present"): if lrc != 0: # package is not installed locally return False, False, False + else: + # a non-zero exit code doesn't always mean the package is installed + # for example, if the package name queried is "provided" by another package + installed_name = get_name(module, lstdout) + if installed_name != name: + return False, False, False # get the version installed locally (if any) lversion = get_version(lstdout)