获取正在运行的剧本的pid,以在该剧本中使用 [英] Get the pid of a running playbook for use within the playbook
问题描述
当我们在启用了详细输出的情况下运行剧本时,在ansible日志中,我们会看到类似以下内容:
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
我猜想p=4105
是剧本在运行时的pid.
I guess that the p=4105
is the pid of the playbook when it ran.
是否有办法在运行时(例如,作为变量)在剧本中获取此pid?
Is there a way to get this pid inside the playbook during its runtime (as a variable for example)?
推荐答案
这听起来有点像 XY问题,但是一种选择可能是使用shell
命令生成一个shell,然后请求父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
这将为您提供执行剧本的python
进程的PID.
This will give you the PID of the python
process that is executing the playbook.
这篇关于获取正在运行的剧本的pid,以在该剧本中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!