Get the pid of a running playbook for use within the playbook

匿名 (未验证) 提交于 2019-12-03 01:22:02

问题:

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

回答1:

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.



回答2:

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


回答3:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!