简体   繁体   English

从同一 playbook 中获取 ansible playbook 的 PID

[英]Get the PID of ansible playbook from within the same playbook

I am trying to get the ansible playbook's PID from within the playbook.我正在尝试从剧本中获取 ansible 剧本的 PID。 I found one crude approach, I am trying to make it more refined and robust.我找到了一种粗略的方法,我正在努力使其更加精致和强大。 If I run following find + awk command, it gives me all the PID of ansible-playbook by the user.如果我按照find + awk命令运行,它会给我用户提供的ansible-playbook的所有 PID。 Although it give me few bogus PIDs as well and I need to remove them.尽管它也给了我一些虚假的 PID,但我需要删除它们。

For example: 4229 is a valid PID and I need it whereas 19425 is a stale PID(not present in ps -eaf output) and I need to remove it from my list.例如:4229 是一个有效的 PID,我需要它,而 19425 是一个陈旧的 PID(不存在于 ps -eaf 输出中),我需要将它从我的列表中删除。

To visually see the files with PID names in them:要直观地查看其中包含 PID 名称的文件:

meta@monk:~/.ansible/tmp>ls -lrt
total 8
drwx------ 2 monk admin4096 Oct 16 13:09 ansible-local-19425A_62FT
drwx------ 3 monk admin4096 Oct 17 10:38 ansible-local-4229U_pXdg
meta@monk:~/.ansible/tmp>

To extract the PID names:要提取 PID 名称:

meta@monk:~/.ansible/tmp>find . -type d |awk 'NR>1{pid=gensub(/.\/ansible-local-([0-9]+)._.*$/,"\\1","g",$NF);print pid}'
4229
4229
19425

To validate if a PID is alive or not:验证 PID 是否存在:

meta@monk:~>ps -eaf |grep -iE '4229|4229|19425'
monk   4229  2179  5 10:33 pts/26   00:00:49 /usr/local/bin/python /usr/local/bin/ansible-playbook pid.yml -vv
monk   5303  4229  0 10:38 pts/26   00:00:00 /usr/local/bin/python /usr/local/bin/ansible-playbook pid.yml -vv
monk   5744  5569  0 10:49 pts/3    00:00:00 grep -iE 4229|4229|19425
meta@monk:~>

Concluding only 4229 is desired, as 19425 is gone from ps -eaf output.只需要得出 4229,因为 19425 从ps -eaf output 中消失了。

Question:问题:

How to combine find , awk , and ps -eaf command efficiently to produce the output 4229 ?如何有效地结合findawkps -eaf命令来生成 output 4229

By the way, I tried simpler solutions provided in Get the pid of a running playbook for use within the playbook ,even added bounty but no joy yet.顺便说一句,我尝试了Get the pid of a running playbook 中提供的更简单的解决方案,以便在 playbook 中使用,甚至增加了赏金,但还没有乐趣。 So please do not mark it as duplicate as this is an extension to that question.因此,请不要将其标记为重复,因为这是对该问题的扩展。

Try this:尝试这个:

#!/bin/bash

cd ~/.ansible/tmp

while read pid; do
  [ -d /proc/${pid} ] || ls -lad ansible-local-${pid}*;
done < <(find . -type d | sed -n 's/^ansible-local-\([0-9]*\).*$/\1/p' )

If correctly lists the stale directories, then change ls -lad to 'rm -r` in line 6.如果正确列出了陈旧的目录,则在第 6 行将ls -lad更改为 'rm -r'。

Since you've already got a question going about running a playbook within a playbook, I'll address your other question.由于您已经有一个关于在剧本中运行剧本的问题,我将解决您的另一个问题。

As Andrew suggested, I think if you want to eliminate your stale Ansible locks, it makes more sense to parse the lock directory than to start with your process table.正如 Andrew 建议的那样,我认为如果您想消除过时的 Ansible 锁,解析锁目录比从进程表开始更有意义。 This would be my take on it:这将是我的看法:

for f in ~/.ansible/tmp/ansible-local-*; do
  [[ $f =~ .*-([0-9]+) ]]
  pid="${BASH_REMATCH[1]}"
  ps "$pid" >/dev/null || rm -vf "$f"
done

This basically says:这基本上说:

  • For each Ansible file,对于每个 Ansible 文件,
    • extract the pid from the filename,从文件名中提取pid,
    • use ps to see if a process by that pid is running, and使用ps查看该 pid 的进程是否正在运行,并且
    • if it is not, delete the file.如果不是,请删除该文件。

Whatever remains is a valid process.剩下的就是一个有效的过程。 (Though this doesn't guarantee that it's an ansible process.) (虽然这并不能保证它是一个 ansible 进程。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM