From 45252b79be478b9c897724cb5b21b2bcc1eaec93 Mon Sep 17 00:00:00 2001 From: Indrajit Raychaudhuri Date: Mon, 24 Apr 2017 17:19:13 -0500 Subject: [PATCH] pacman: Fail fast when 'rc != 0' We fail-fast and display 'stderr' in case 'pacman' returns with 'rc != 0'. There is no point computing 'module._diff' in such case anyway. Fixes #23910 (cherry picked from commit 8c6a2a848cf6a6d6522c8f5be56decf8df1ed6ab) --- lib/ansible/modules/packaging/os/pacman.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/ansible/modules/packaging/os/pacman.py b/lib/ansible/modules/packaging/os/pacman.py index 787781bb37..8a0af877a7 100644 --- a/lib/ansible/modules/packaging/os/pacman.py +++ b/lib/ansible/modules/packaging/os/pacman.py @@ -258,6 +258,9 @@ def remove_packages(module, pacman_path, packages): cmd = "%s -%s %s --noconfirm --noprogressbar" % (pacman_path, args, package) rc, stdout, stderr = module.run_command(cmd, check_rc=False) + if rc != 0: + module.fail_json(msg="failed to remove %s" % (package)) + if module._diff: d = stdout.split('\n')[2].split(' ')[2:] for i, pkg in enumerate(d): @@ -265,9 +268,6 @@ def remove_packages(module, pacman_path, packages): diff['before'] += "%s\n" % pkg data.append('\n'.join(d)) - if rc != 0: - module.fail_json(msg="failed to remove %s" % (package)) - remove_c += 1 if remove_c > 0: @@ -305,6 +305,10 @@ def install_packages(module, pacman_path, state, packages, package_files): if to_install_repos: cmd = "%s -S %s --noconfirm --noprogressbar --needed" % (pacman_path, " ".join(to_install_repos)) rc, stdout, stderr = module.run_command(cmd, check_rc=False) + + if rc != 0: + module.fail_json(msg="failed to install %s: %s" % (" ".join(to_install_repos), stderr)) + data = stdout.split('\n')[3].split(' ')[2:] data = [ i for i in data if i != '' ] for i, pkg in enumerate(data): @@ -312,14 +316,15 @@ def install_packages(module, pacman_path, state, packages, package_files): if module._diff: diff['after'] += "%s\n" % pkg - if rc != 0: - module.fail_json(msg="failed to install %s: %s" % (" ".join(to_install_repos), stderr)) - install_c += len(to_install_repos) if to_install_files: cmd = "%s -U %s --noconfirm --noprogressbar --needed" % (pacman_path, " ".join(to_install_files)) rc, stdout, stderr = module.run_command(cmd, check_rc=False) + + if rc != 0: + module.fail_json(msg="failed to install %s: %s" % (" ".join(to_install_files), stderr)) + data = stdout.split('\n')[3].split(' ')[2:] data = [ i for i in data if i != '' ] for i, pkg in enumerate(data): @@ -327,9 +332,6 @@ def install_packages(module, pacman_path, state, packages, package_files): if module._diff: diff['after'] += "%s\n" % pkg - if rc != 0: - module.fail_json(msg="failed to install %s: %s" % (" ".join(to_install_files), stderr)) - install_c += len(to_install_files) if state == 'latest' and len(package_err) > 0: