1#!/usr/local/bin/python3.8
2# Copyright: (c) 2019, Saranya Sridharan
3# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4from __future__ import (absolute_import, division, print_function)
5__metaclass__ = type
6
7ANSIBLE_METADATA = {'metadata_version': '1.1',
8                    'status': ['preview'],
9                    'supported_by': 'community'}
10
11DOCUMENTATION = '''
12module: pids
13version_added: 2.8
14description: "Retrieves a list of PIDs of given process name in Ansible controller/controlled machines.Returns an empty list if no process in that name exists."
15short_description: "Retrieves process IDs list if the process is running otherwise return empty list"
16author:
17  - Saranya Sridharan (@saranyasridharan)
18requirements:
19  - psutil(python module)
20options:
21  name:
22    description: the name of the process you want to get PID for.
23    required: true
24    type: str
25'''
26
27EXAMPLES = '''
28# Pass the process name
29- name: Getting process IDs of the process
30  pids:
31      name: python
32  register: pids_of_python
33
34- name: Printing the process IDs obtained
35  debug:
36    msg: "PIDS of python:{{pids_of_python.pids|join(',')}}"
37'''
38
39RETURN = '''
40pids:
41  description: Process IDs of the given process
42  returned: list of none, one, or more process IDs
43  type: list
44  sample: [100,200]
45'''
46
47from ansible.module_utils.basic import AnsibleModule
48try:
49    import psutil
50    HAS_PSUTIL = True
51except ImportError:
52    HAS_PSUTIL = False
53
54
55def compare_lower(a, b):
56    if a is None or b is None:
57        # this could just be "return False" but would lead to surprising behavior if both a and b are None
58        return a == b
59
60    return a.lower() == b.lower()
61
62
63def get_pid(name):
64    pids = []
65
66    for proc in psutil.process_iter(attrs=['name', 'cmdline']):
67        if compare_lower(proc.info['name'], name) or \
68                proc.info['cmdline'] and compare_lower(proc.info['cmdline'][0], name):
69            pids.append(proc.pid)
70
71    return pids
72
73
74def main():
75    module = AnsibleModule(
76        argument_spec=dict(
77            name=dict(required=True, type="str"),
78        ),
79        supports_check_mode=True,
80    )
81    if not HAS_PSUTIL:
82        module.fail_json(msg="Missing required 'psutil' python module. Try installing it with: pip install psutil")
83    name = module.params["name"]
84    response = dict(pids=get_pid(name))
85    module.exit_json(**response)
86
87
88if __name__ == '__main__':
89    main()
90