获取正在运行的剧本的 pid 以在剧本中使用 [英] Get the pid of a running playbook for use within the playbook

查看:24
本文介绍了获取正在运行的剧本的 pid 以在剧本中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们运行剧本并启用详细输出时,在 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 |播放回顾

我猜 p=4105 是剧本运行时的 pid.

I guess that the p=4105 is the pid of the playbook when it ran.

有没有办法在 playbook 运行时(例如作为变量)在 playbook 中获取这个 pid?

Is there a way to get this pid inside the playbook during its runtime (as a variable for example)?

推荐答案

您可以使用带有 lookup 过滤器的 set_fact 模块为 localhost 定义 PID.

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

稍后您可以通过 hostvars 字典引用 PID.

And later on you can reference the PID via the hostvars dictionary.

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

这篇关于获取正在运行的剧本的 pid 以在剧本中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆