From 6541bb03150ef62b8f9364789e7bef800c4e5c43 Mon Sep 17 00:00:00 2001 From: Will Medlar <5326769+wmedlar@users.noreply.github.com> Date: Fri, 20 Sep 2019 01:30:25 -0500 Subject: [PATCH] pids: Search for process name matches in the cmdline (#59318) * Also search for process name matches in the cmdline * Return integer pids * Use correct variable name * Fix linter concerns --- lib/ansible/modules/system/pids.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/system/pids.py b/lib/ansible/modules/system/pids.py index 80b48a1a2d4..4cbf45a9690 100644 --- a/lib/ansible/modules/system/pids.py +++ b/lib/ansible/modules/system/pids.py @@ -52,8 +52,23 @@ except ImportError: HAS_PSUTIL = False +def compare_lower(a, b): + if a is None or b is None: + # this could just be "return False" but would lead to surprising behavior if both a and b are None + return a == b + + return a.lower() == b.lower() + + def get_pid(name): - return [p.info['pid'] for p in psutil.process_iter(attrs=['pid', 'name']) if p.info and p.info.get('name', None) and p.info['name'].lower() == name.lower()] + pids = [] + + for proc in psutil.process_iter(attrs=['name', 'cmdline']): + if compare_lower(proc.info['name'], name) or \ + proc.info['cmdline'] and compare_lower(proc.info['cmdline'][0], name): + pids.append(proc.pid) + + return pids def main():