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
I guess that the p=4105
is the pid of the playbook when it ran.
Is there a way to get this pid inside the playbook during its runtime (as a variable for example)?
This sounds a little like an XY problem, but one option may be to spawn a shell with the
shell
command and then ask for the parent PID:This will give you the PID of the
python
process that is executing the playbook.You can define the PID for localhost using the
set_fact
module with alookup
filter.And later on you can reference the
PID
via thehostvars
dictionary.If you will be using the pid in different plays, just add it to the
setup
module.and it will always be available.