When we run a playbook, with verbose output enabled, in the ansible logs we can see something like this:
2016-02-03 12:51:58,235 p=4105 u=root | PLAY RECAP
You can refer to the pids
module new with v2.8. which delivers all pids for a specific process name to you.
Simple example to get Pids of Ansible Playbooks on host machines:
- hosts: localhost
tasks:
- name: "get pids! and no, 'ansible-playboo' is no typo"
pids:
name: ansible-playboo
register: pids_of_python
- name: "Print pids"
debug:
msg: "PIDs: {{ pids_of_python.pids|join(',') }}"
Downside: you'll have to install psutil
Please refer to https://docs.ansible.com/ansible/latest/modules/pids_module.html