From 1b99ef8028bb865636f3f23400721989c94894ee Mon Sep 17 00:00:00 2001 From: Giorgos Drosos <56369797+gdrosos@users.noreply.github.com> Date: Sun, 27 Jul 2025 12:51:13 +0300 Subject: [PATCH] listen_ports_facts: Avoid crash when required commands are missing (#10458) * Fix listen-port-facts crash * Update changelog * Update tests/integration/targets/listen_ports_facts/tasks/main.yml Co-authored-by: Felix Fontein * Fix sanity tests * Update changelogs/fragments/10458-listen_port_facts-prevent-type-error.yml Co-authored-by: Felix Fontein --------- Co-authored-by: Felix Fontein (cherry picked from commit fe59c6d29ebb2e1389643d8b78d4bb6a237aaaef) --- ...8-listen_port_facts-prevent-type-error.yml | 2 ++ plugins/modules/listen_ports_facts.py | 2 +- .../targets/listen_ports_facts/tasks/main.yml | 29 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/10458-listen_port_facts-prevent-type-error.yml diff --git a/changelogs/fragments/10458-listen_port_facts-prevent-type-error.yml b/changelogs/fragments/10458-listen_port_facts-prevent-type-error.yml new file mode 100644 index 0000000000..70af0932b3 --- /dev/null +++ b/changelogs/fragments/10458-listen_port_facts-prevent-type-error.yml @@ -0,0 +1,2 @@ +bugfixes: + - "listen_port_facts - avoid crash when required commands are missing (https://github.com/ansible-collections/community.general/issues/10457, https://github.com/ansible-collections/community.general/pull/10458)." \ No newline at end of file diff --git a/plugins/modules/listen_ports_facts.py b/plugins/modules/listen_ports_facts.py index 8734c679f9..a33c78be3c 100644 --- a/plugins/modules/listen_ports_facts.py +++ b/plugins/modules/listen_ports_facts.py @@ -397,7 +397,7 @@ def main(): break if bin_path is None: - raise EnvironmentError(msg='Unable to find any of the supported commands in PATH: {0}'.format(", ".join(sorted(commands_map)))) + raise EnvironmentError('Unable to find any of the supported commands in PATH: {0}'.format(", ".join(sorted(commands_map)))) # which ports are listening for connections? args = commands_map[command]['args'] diff --git a/tests/integration/targets/listen_ports_facts/tasks/main.yml b/tests/integration/targets/listen_ports_facts/tasks/main.yml index 0e583e7a13..5da5b03784 100644 --- a/tests/integration/targets/listen_ports_facts/tasks/main.yml +++ b/tests/integration/targets/listen_ports_facts/tasks/main.yml @@ -110,3 +110,32 @@ loop: "{{ [tcp_listen, udp_listen]|flatten }}" when: item.name == 'nc' ignore_errors: true + + +- when: ansible_os_family == "Debian" + block: + - name: Remove netstat and ss dependencies to simulate missing executables + ansible.builtin.package: + name: + - net-tools + - iproute2 + state: absent + ignore_errors: true + + - name: Trigger listen_ports_facts with missing tools + community.general.listen_ports_facts: + register: listen_ports_failure_result + ignore_errors: true + + - name: Assert graceful failure when dependencies are missing + ansible.builtin.assert: + that: + - listen_ports_failure_result is failed + - "'Unable to find any of the supported commands' in listen_ports_failure_result.msg" + + - name: Reinstall netstat and ss dependencies after test + ansible.builtin.package: + name: + - net-tools + - iproute2 + state: present