2

I am writing a custom ansible module to get process id with process name using the linux package psutil . this is my module file.

from ansible.module_utils.basic import AnsibleModule
import sys
try:
    import psutil
    HAS_PSUTIL = True
except ImportError:
    HAS_PSUTIL = False


def get_pid(name, module):
    return [int(p.info['pid']) for p in psutil.process_iter(attrs=['pid', 'name']) if name in p.info['name']]


def main():
    module = AnsibleModule(
        argument_spec={
            "name": {"required": True, "type": "str"}
        }
    )
    if not HAS_PSUTIL:
        module.fail_json(msg="Missing required 'psutil' python module. Try installing it with: pip install psutil")
    name = module.params["name"]
    response = dict(pids=get_pid(name, module))
    module.exit_json(**response)


if __name__ == '__main__':
    main()

ansible code to use the module

- name: "Checking the process IDs (PIDs) of sleep binary" 
  pids: 
    name: "some-long-process-name-999999"
  register: pids
- name: "Verify that the Process IDs (PIDs) returned is not empty"
  assert:
    that:
    - "pids.pids | length > 0"

I am getting empty process name when trying with long name, for small name it works fine. Is it some issue with psutil module ? I have tried pidof and pgrep also it also seems to be failing. Can anyone help me with this ? thanks for your time.

  • What does "pgrep also it also seems to be failing" mean? in what way does it fail? – mdaniel Jan 10 '19 at 06:53
  • "Is it some issue with psutil module?" I would presume you can find the answer to that by running `psutil.process_iter` without the `if name in` part, and see if the resulting object contains a truncated name or what – mdaniel Jan 10 '19 at 06:54
  • @Matthew it returns empty value instead of the process id I expect to see. – Saranya Sridharan Jan 10 '19 at 09:06
  • @SaranyaSridharan `long name` and `short name` are subjective. Give an actual example of names that works and does not work. I had no issues with a 256 char process name. – helloV Jan 10 '19 at 19:22
  • @helloV I am trying with this name exactly “some-random-long-name-<6 digit random number>” it didn’t work for me . But merely 6 digit number works. – Saranya Sridharan Jan 10 '19 at 19:25
  • @SaranyaSridharan is there a process with the name `some-random-long-name-<6 digit random number>` running? – helloV Jan 10 '19 at 19:27
  • Yes I rename the sleep binary in that name and start with a shell script in background . As sleep is renamed that name will be the process name – Saranya Sridharan Jan 10 '19 at 19:39
  • I did the same and had no issue. It could be due to how you start the process. Make sure the process appears in `ps` command output. If it does then your script should work. – helloV Jan 11 '19 at 05:20
  • 1
    There is now an official module "pids" which does exactly what you are trying to do here. See https://docs.ansible.com/ansible/latest/modules/pids_module.html – centic Jun 28 '20 at 08:13

0 Answers0