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



On Tuesday, June 14, 2016 at 11:24:46 PM UTC+1, Rajas Pimpalwadkar wrote:
>
> 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.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/cdeadf8c-b479-4eb5-9c23-0e209bf375c1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to