Get Process ID of ansible-playbook within the playbook

2,701 views
Skip to first unread message

Rajas Pimpalwadkar

unread,
Jun 14, 2016, 6:24:46 PM6/14/16
to Ansible Project
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.

Abhishek R.

unread,
Jun 15, 2016, 7:53:41 AM6/15/16
to Ansible Project
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.

J Hawkesworth

unread,
Jun 15, 2016, 8:45:54 AM6/15/16
to Ansible Project
I have done this in a callback plugin by 

import os
pid = os.getpid()


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.

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

Guillaume Nobiron

unread,
Jun 15, 2016, 11:04:26 AM6/15/16
to Ansible Project
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% Agrowww.vif.fr 
VifYouTubeVifTwitter
Suivez l'actualité VIF sur:

Brian Coca

unread,
Jun 15, 2016, 11:28:31 AM6/15/16
to ansible...@googlegroups.com
lookup('pipe', 'echo $PPID')

----------
Brian Coca

Dhiwakar Ravikumar

unread,
Aug 6, 2021, 12:38:43 AM8/6/21
to Ansible Project
Is this process ID unique for the lifetime of a task of the playbook, or for the lifetime of the playbook itself ? 

Brian Coca

unread,
Aug 6, 2021, 12:19:52 PM8/6/21
to Ansible Project
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.


--
----------
Brian Coca

Dhiwakar Ravikumar

unread,
Aug 7, 2021, 8:43:49 PM8/7/21
to Ansible Project
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 

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

Brian Coca

unread,
Aug 9, 2021, 11:28:12 AM8/9/21
to Ansible Project
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).

--
----------
Brian Coca

Reply all
Reply to author
Forward
0 new messages