10

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

Dave Snigier
  • 2,574
  • 3
  • 21
  • 29
Cobra Kai Dojo
  • 1,018
  • 2
  • 12
  • 25
  • I don't think that's available directly, but what are you trying to do with the PID? I think @larsks has a good answer if you really need it – Dave Snigier Feb 03 '16 at 14:17
  • @DaveSnigier What I am trying to achieve is relevant to [my question here](http://stackoverflow.com/questions/35135954/how-to-log-in-a-separate-file-per-playbook-in-ansible) . I am trying to extract the entries related to a specific playbook from the ansible log, in order to create a separate log (I guess that's one way of doing it). – Cobra Kai Dojo Feb 04 '16 at 12:26
  • IF someone find the answer let this question also answered: https://stackoverflow.com/questions/58436621/combine-ps-eaf-with-awk-command-output – monk Oct 17 '19 at 18:20

5 Answers5

4

You can define the PID for localhost using the set_fact module with a lookup filter.

- hosts: localhost
  tasks:
    - set_fact:
        pid: "{{ lookup('pipe', 'echo $PPID') }}"

And later on you can reference the PID via the hostvars dictionary.

- hosts: remote
  tasks:
    - debug: var=hostvars.localhost.pid
ceving
  • 21,900
  • 13
  • 104
  • 178
3

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:

- name: get pid of playbook
  shell: |
    echo "$PPID"
  register: playbook_pid

This will give you the PID of the python process that is executing the playbook.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • Thanks for the tip. Sadly, I get back a pid that is not relevant with the pid in the logs. Btw, that may be indeed a XY problem. Please check my comment above of why I want to do something like this. Maybe you can help in my other question here in SO. – Cobra Kai Dojo Feb 04 '16 at 12:34
  • You'd want to ensure this runs on the local system, not the target host. Either add `delegate_to: localhost`, or rewrite it using `local_action:`. The PPID you're getting with this snippet is from the target host, not the localhost. – Dan Hunsaker Mar 06 '17 at 17:01
2

This might be what you are looking for, but is only applicable to Linux:

- name: Get the pid of this playbook
  shell: pstree -spal $PPID | grep ansible-playbook | awk '{print $1;exit}' | awk -F, '{print $2}'
  register: ansible_pid

- name: Set the ansible playbook pid variable
  set_fact:
    ansible_playbook_pid: "{{ ansible_pid.stdout|int }}"
pekowski
  • 131
  • 3
0

If you will be using the pid in different plays, just add it to the setup module.

setup_result['ansible_facts']['ansible_pid'] = os.getpid()

and it will always be available.

    "ansible_os_family": "Debian",
    "ansible_pid": 27930,
    "ansible_pkg_mgr": "apt",
helloV
  • 50,176
  • 7
  • 137
  • 145
  • The setup module is executed for every host. If you run the code on 200 hosts, it calls 200 times `getpid` although the `PID` of the Ansible host is always the same. This means, it is the wrong place to do this in the setup module. – ceving Jun 07 '17 at 12:42
0

You can refer to the pids module new with v2.8. which delivers all pids for a specific process name to you.

Simple example to get Pids of Ansible Playbooks on host machines:

- hosts: localhost
  tasks:

    - name: "get pids! and no, 'ansible-playboo' is no typo"
      pids:
        name: ansible-playboo
      register: pids_of_python

    - name: "Print pids"
      debug:
        msg: "PIDs: {{ pids_of_python.pids|join(',') }}"

Downside: you'll have to install psutil

Please refer to https://docs.ansible.com/ansible/latest/modules/pids_module.html

Patrick Pötz
  • 369
  • 5
  • 15
  • looking for ansible 2.7.9 or below, thanks for responding – monk Oct 16 '19 at 14:36
  • Also this will print all the pid of ansible-playbooks which is undesired , here I need to grab the pid of the current playbook from the same playbook. – monk Oct 16 '19 at 15:32