From 8ece0d3609175c91e2a5fbcff1b0d196e90a08f3 Mon Sep 17 00:00:00 2001 From: Aoife Finch Date: Sun, 10 Oct 2021 21:55:09 -0700 Subject: [PATCH] macports: add stdout and stderr to status (#3499) (#3500) * macports: add stdout and stderr to status (#3499) * Add changelog fragment * Update changelogs/fragments/3500-macports-add-stdout-and-stderr-to-status.yaml Co-authored-by: Felix Fontein Co-authored-by: Aoife Finch Co-authored-by: Felix Fontein --- ...ports-add-stdout-and-stderr-to-status.yaml | 3 + plugins/modules/packaging/os/macports.py | 76 +++++++++++-------- 2 files changed, 47 insertions(+), 32 deletions(-) create mode 100644 changelogs/fragments/3500-macports-add-stdout-and-stderr-to-status.yaml diff --git a/changelogs/fragments/3500-macports-add-stdout-and-stderr-to-status.yaml b/changelogs/fragments/3500-macports-add-stdout-and-stderr-to-status.yaml new file mode 100644 index 0000000000..f39466d876 --- /dev/null +++ b/changelogs/fragments/3500-macports-add-stdout-and-stderr-to-status.yaml @@ -0,0 +1,3 @@ +bugfixes: + - macports - add ``stdout`` and ``stderr`` to return values + (https://github.com/ansible-collections/community.general/issues/3499). diff --git a/plugins/modules/packaging/os/macports.py b/plugins/modules/packaging/os/macports.py index a865a8f339..1d3f47a240 100644 --- a/plugins/modules/packaging/os/macports.py +++ b/plugins/modules/packaging/os/macports.py @@ -120,7 +120,7 @@ def selfupdate(module, port_path): changed = False msg = "Macports already up-to-date" - return (changed, msg) + return (changed, msg, out, err) else: module.fail_json(msg="Failed to update Macports", stdout=out, stderr=err) @@ -134,11 +134,11 @@ def upgrade(module, port_path): if out.strip() == "Nothing to upgrade.": changed = False msg = "Ports already upgraded" - return (changed, msg) + return (changed, msg, out, err) elif rc == 0: changed = True msg = "Outdated ports upgraded successfully" - return (changed, msg) + return (changed, msg, out, err) else: module.fail_json(msg="Failed to upgrade outdated ports", stdout=out, stderr=err) @@ -165,7 +165,7 @@ def query_port(module, port_path, name, state="present"): return False -def remove_ports(module, port_path, ports): +def remove_ports(module, port_path, ports, stdout, stderr): """ Uninstalls one or more ports if installed. """ remove_c = 0 @@ -176,20 +176,21 @@ def remove_ports(module, port_path, ports): continue rc, out, err = module.run_command("%s uninstall %s" % (port_path, port)) - + stdout += out + stderr += err if query_port(module, port_path, port): - module.fail_json(msg="Failed to remove %s: %s" % (port, err)) + module.fail_json(msg="Failed to remove %s: %s" % (port, err), stdout=stdout, stderr=stderr) remove_c += 1 if remove_c > 0: - module.exit_json(changed=True, msg="Removed %s port(s)" % remove_c) + module.exit_json(changed=True, msg="Removed %s port(s)" % remove_c, stdout=stdout, stderr=stderr) - module.exit_json(changed=False, msg="Port(s) already absent") + module.exit_json(changed=False, msg="Port(s) already absent", stdout=stdout, stderr=stderr) -def install_ports(module, port_path, ports, variant): +def install_ports(module, port_path, ports, variant, stdout, stderr): """ Installs one or more ports if not already installed. """ install_c = 0 @@ -199,66 +200,70 @@ def install_ports(module, port_path, ports, variant): continue rc, out, err = module.run_command("%s install %s %s" % (port_path, port, variant)) - + stdout += out + stderr += err if not query_port(module, port_path, port): - module.fail_json(msg="Failed to install %s: %s" % (port, err)) + module.fail_json(msg="Failed to install %s: %s" % (port, err), stdout=stdout, stderr=stderr) install_c += 1 if install_c > 0: - module.exit_json(changed=True, msg="Installed %s port(s)" % (install_c)) + module.exit_json(changed=True, msg="Installed %s port(s)" % (install_c), stdout=stdout, stderr=stderr) - module.exit_json(changed=False, msg="Port(s) already present") + module.exit_json(changed=False, msg="Port(s) already present", stdout=stdout, stderr=stderr) -def activate_ports(module, port_path, ports): +def activate_ports(module, port_path, ports, stdout, stderr): """ Activate a port if it's inactive. """ activate_c = 0 for port in ports: if not query_port(module, port_path, port): - module.fail_json(msg="Failed to activate %s, port(s) not present" % (port)) + module.fail_json(msg="Failed to activate %s, port(s) not present" % (port), stdout=stdout, stderr=stderr) if query_port(module, port_path, port, state="active"): continue rc, out, err = module.run_command("%s activate %s" % (port_path, port)) + stdout += out + stderr += err if not query_port(module, port_path, port, state="active"): - module.fail_json(msg="Failed to activate %s: %s" % (port, err)) + module.fail_json(msg="Failed to activate %s: %s" % (port, err), stdout=stdout, stderr=stderr) activate_c += 1 if activate_c > 0: - module.exit_json(changed=True, msg="Activated %s port(s)" % (activate_c)) + module.exit_json(changed=True, msg="Activated %s port(s)" % (activate_c), stdout=stdout, stderr=stderr) - module.exit_json(changed=False, msg="Port(s) already active") + module.exit_json(changed=False, msg="Port(s) already active", stdout=stdout, stderr=stderr) -def deactivate_ports(module, port_path, ports): +def deactivate_ports(module, port_path, ports, stdout, stderr): """ Deactivate a port if it's active. """ deactivated_c = 0 for port in ports: if not query_port(module, port_path, port): - module.fail_json(msg="Failed to deactivate %s, port(s) not present" % (port)) + module.fail_json(msg="Failed to deactivate %s, port(s) not present" % (port), stdout=stdout, stderr=stderr) if not query_port(module, port_path, port, state="active"): continue rc, out, err = module.run_command("%s deactivate %s" % (port_path, port)) - + stdout += out + stderr += err if query_port(module, port_path, port, state="active"): - module.fail_json(msg="Failed to deactivate %s: %s" % (port, err)) + module.fail_json(msg="Failed to deactivate %s: %s" % (port, err), stdout=stdout, stderr=stderr) deactivated_c += 1 if deactivated_c > 0: - module.exit_json(changed=True, msg="Deactivated %s port(s)" % (deactivated_c)) + module.exit_json(changed=True, msg="Deactivated %s port(s)" % (deactivated_c), stdout=stdout, stderr=stderr) - module.exit_json(changed=False, msg="Port(s) already inactive") + module.exit_json(changed=False, msg="Port(s) already inactive", stdout=stdout, stderr=stderr) def main(): @@ -272,35 +277,42 @@ def main(): ) ) + stdout = "" + stderr = "" + port_path = module.get_bin_path('port', True, ['/opt/local/bin']) p = module.params if p["selfupdate"]: - (changed, msg) = selfupdate(module, port_path) + (changed, msg, out, err) = selfupdate(module, port_path) + stdout += out + stderr += err if not (p["name"] or p["upgrade"]): - module.exit_json(changed=changed, msg=msg) + module.exit_json(changed=changed, msg=msg, stdout=stdout, stderr=stderr) if p["upgrade"]: - (changed, msg) = upgrade(module, port_path) + (changed, msg, out, err) = upgrade(module, port_path) + stdout += out + stderr += err if not p["name"]: - module.exit_json(changed=changed, msg=msg) + module.exit_json(changed=changed, msg=msg, stdout=stdout, stderr=stderr) pkgs = p["name"] variant = p["variant"] if p["state"] in ["present", "installed"]: - install_ports(module, port_path, pkgs, variant) + install_ports(module, port_path, pkgs, variant, stdout, stderr) elif p["state"] in ["absent", "removed"]: - remove_ports(module, port_path, pkgs) + remove_ports(module, port_path, pkgs, stdout, stderr) elif p["state"] == "active": - activate_ports(module, port_path, pkgs) + activate_ports(module, port_path, pkgs, stdout, stderr) elif p["state"] == "inactive": - deactivate_ports(module, port_path, pkgs) + deactivate_ports(module, port_path, pkgs, stdout, stderr) if __name__ == '__main__':