Get Process ID of ansible-playbook within the playbook

I want to add locking around some part of a playbook. For this I need to get the process id of ansible-playbook that has started the task that I am currently running.
How do I get process id of ansible-playbook that the current task is part of? I cannot use grep reliable because there could be multiple ansible-playbook processes running.

The actual process record includes the name/path of the playbook being run. You should be able to use piped greps over ps/pgrep to get the current playbook.

I have done this in a callback plugin by

import os
pid = os.getpid()

The callback plugin code is here if that is helpful: https://github.com/jhawkesworth/ansible/commit/38b639ac1c2fae90177a87eff5611d62ff8ad00f

This would be more difficult for a task though, because a/ ansible forks so each host might have a different process id I think - see http://docs.ansible.com/ansible/intro_configuration.html#forks
Also not sure what happens when you delegate_to: localhost , although I’d imagine you’d still get the fork pid even then.

There might be another way around what you are trying to achieve though. Sometimes I use delegate_to to run commands just on the first node in a cluster for example, so I know that that command will only get run once.
See http://docs.ansible.com/ansible/faq.html#how-do-i-access-a-variable-of-the-first-host-in-a-group and http://docs.ansible.com/ansible/playbooks_delegation.html#delegation

You might be able to use the block / rescue syntax too as well - see http://docs.ansible.com/ansible/playbooks_blocks.html#error-handling

Hope this helps,

Jon

You can get the PID from the PPID shell environment variable:

  • name: get pid of playbook
    shell: echo “$PPID”
    register: ansible_pid

Then you can use {{ansible_pid.stdout}} to get the PID value.

logoVif

L’informatique 100% Agro

www.vif.fr

VifYouTubeVifTwitter

Suivez l’actualité VIF sur:

[](http://www.agrovif.com)

lookup(‘pipe’, ‘echo $PPID’)

Is this process ID unique for the lifetime of a task of the playbook, or for the lifetime of the playbook itself ?

You have several, but my previous response should give you the pid of
the `ansible-playbook` invocation you are currently running.

As for the 'several' pids, the ansible-playbook CLI forks for each
task/host (another pid) which then will execute on the remote (yet
another pid) .. which normally involves a fork of the daemon granting
access (ssh?) .. yet another pid. The task itself might fork again
(async, run a cli , etc) .. with more pids involved. So you have to be
very specific about which pid you want and in most cases requires
changing action plugin/module code to deliver them.

Thank you Brian, I was exploring different strategies and this whole PID thing i.e. reliance on PID as a constant value failed miserably, I am trying to implement a strategy where every single module that’s running is capable of identifying which instance of a playbook called it. One option was to always pass an argument to a collection of modules that I’m developing, something like Module Group defaults
https://docs.ansible.com/ansible/latest/user_guide/playbooks_module_defaults.html

Is there a way I can implement module group defaults for the collection of modules that I’m developing ?
For instance, the following snippet doesn’t really work since my collection of modules isn’t recognized as a “group”
module_defaults:
group/dhiwakar_namespace.dhiwakar_collection:
session: 123

use uuid, every playbook object gets one, there is one caveat and that
is that tasks from a role get the same uuid even if reused (within the
same play).