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
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 }}"