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)?
The easiest way to run only one task in Ansible Playbook is using the tags statement parameter of the “ansible-playbook” command. The default behavior is to execute all the tags in your Playbook with --tags all .
To pass a value to nodes, use the --extra-vars or -e option while running the Ansible playbook, as seen below. This ensures you avoid accidental running of the playbook against hardcoded hosts.
Vars tag lets you define the variables which you can use in your playbook. Usage is similar to variables in any programming language.
By Default, Ansible would run the playbook on the host group which is mentioned in the playbook with hosts: directive. But if you want to ignore all those hosts specified in the playbook and run it locally. You can use this method.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With