# Get Process ID of ansible-playbook within the playbook

**URL:** <https://forum.ansible.com/t/get-process-id-of-ansible-playbook-within-the-playbook/21778>\
**Category:** Ansible Project\
**Created:** [June 14, 2016, 7:48pm UTC](https://forum.ansible.com/t/get-process-id-of-ansible-playbook-within-the-playbook/21778 "2016-06-14T19:48:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajas\_Pimpalwadkar](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Rajas\_Pimpalwadkar](https://forum.ansible.com/u/Rajas_Pimpalwadkar)\
**Post date:** [June 14, 2016, 7:48pm UTC](https://forum.ansible.com/t/get-process-id-of-ansible-playbook-within-the-playbook/21778/1 "2016-06-14T19:48:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Abhishek\_R](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@Abhishek\_R](https://forum.ansible.com/u/Abhishek_R)\
**Post date:** [June 15, 2016, 11:53am UTC](https://forum.ansible.com/t/get-process-id-of-ansible-playbook-within-the-playbook/21778/2 "2016-06-15T11:53:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [June 15, 2016, 12:45pm UTC](https://forum.ansible.com/t/get-process-id-of-ansible-playbook-within-the-playbook/21778/3 "2016-06-15T12:45:54Z")

</div>

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](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](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](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](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](http://docs.ansible.com/ansible/playbooks_blocks.html#error-handling)

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![Guillaume\_Nobiron](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Guillaume\_Nobiron](https://forum.ansible.com/u/Guillaume_Nobiron)\
**Post date:** [June 15, 2016, 1:19pm UTC](https://forum.ansible.com/t/get-process-id-of-ansible-playbook-within-the-playbook/21778/4 "2016-06-15T13:19:47Z")

</div>

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](https://ci3.googleusercontent.com/proxy/jNw3kk6adk01Es5Sohxe1dLczyeO-oDK-x1ilyy9UZKzBvvYS6tLJdWQccesL76IUbn7nqjdykptLsCu6Q=s0-d-e1-ft#http://exper-ia.com/images/logo_vif.png)](http://www.vif.fr/)

 |
| 

L’informatique 100% Agro

 | 

[www.vif.fr](http://www.vif.fr/)

 | 

[![VifYouTube](https://ci3.googleusercontent.com/proxy/McHI_KgbDIV1VTZNHms4RfYjSyf9dZilloQOXJFt2R_8k5gZP5ezMt8pL7m7dkrNDrVIYdSrY9JJO8CWk8AArr_x2xs=s0-d-e1-ft#http://exper-ia.com/images/ico_YouTubeMail.png)](http://www.youtube.com/user/Agrovif)[![VifTwitter](https://ci5.googleusercontent.com/proxy/TQSsKHGL--n7vnMbP-E37n9cQ0gF0xKtEyEKLVaDA1XyHPqseT8z2Ocw6PnUxjTfHHg7o8H3fzrNxcs7ZmDoKGzURx4=s0-d-e1-ft#http://exper-ia.com/images/ico_twitterMail.png)](https://twitter.com/VIF_agro)

 |
| 

_Suivez l’actualité VIF sur:_

 |

[![](http://www.vif.fr/wp-content/uploads/2014/02/signature-mail-agrovif-2016.jpeg)](http://www.agrovif.com)

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 15, 2016, 3:28pm UTC](https://forum.ansible.com/t/get-process-id-of-ansible-playbook-within-the-playbook/21778/5 "2016-06-15T15:28:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dhiwakar\_Ravikumar](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@Dhiwakar\_Ravikumar](https://forum.ansible.com/u/Dhiwakar_Ravikumar)\
**Post date:** [August 6, 2021, 4:38am UTC](https://forum.ansible.com/t/get-process-id-of-ansible-playbook-within-the-playbook/21778/6 "2021-08-06T04:38:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 6, 2021, 4:18pm UTC](https://forum.ansible.com/t/get-process-id-of-ansible-playbook-within-the-playbook/21778/7 "2021-08-06T16:18:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Dhiwakar\_Ravikumar](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@Dhiwakar\_Ravikumar](https://forum.ansible.com/u/Dhiwakar_Ravikumar)\
**Post date:** [August 8, 2021, 12:43am UTC](https://forum.ansible.com/t/get-process-id-of-ansible-playbook-within-the-playbook/21778/8 "2021-08-08T00:43:49Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 9, 2021, 3:27pm UTC](https://forum.ansible.com/t/get-process-id-of-ansible-playbook-within-the-playbook/21778/9 "2021-08-09T15:27:15Z")

</div>

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).
