Get a pid from a running playbook for use on a playbook

When we run the playbook, with the detailed output turned on, in the irreplaceable magazines we can see something like this:

2016-02-03 12:51:58,235 p=4105 u=root | PLAY RECAP

I think the p=4105playbook pid is when it was running.

Is there a way to get this pid inside the playbook at runtime (e.g. as a variable)?

+5
source share
4 answers

This is a bit like the XY problem , but one option would be to start the shell using the command shelland then request the parent PID:

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

This will give you the PID of the process pythonthat runs the game.

+3

PID localhost set_fact lookup.

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

PID hostvars.

- hosts: remote
  tasks:
    - debug: var=hostvars.localhost.pid
+1

pid , setup.

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

.

    "ansible_os_family": "Debian",
    "ansible_pid": 27930,
    "ansible_pkg_mgr": "apt",
0

pids, v2.8. pids .

Pids of Ansible Playbooks -:

- 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(',') }}"

: psutil

, https://docs.ansible.com/ansible/latest/modules/pids_module.html

0

Source: https://habr.com/ru/post/1627138/