gpt4 book ai didi

bash - 带有 pgrep 的 Ansible shell 任务不断失败,在普通 shell 中工作

转载 作者:行者123 更新时间:2023-11-29 09:25:36 25 4
gpt4 key购买 nike

我正在使用 ansible-2.0.1.0-2.el7.noarch(但也尝试使用 1.9.4)并且我正在尝试运行它剧本:

- hosts: all
remote_user: root
tasks:
- shell:
pgrep --full 'sleep' && pkill --full 'sleep' || true

但我得到:

# ansible-playbook -i aaa.ini aaa.yaml 

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [192.168.122.100]

TASK [command] *****************************************************************
fatal: [192.168.122.100]: FAILED! => {"changed": true, "cmd": "pgrep --full 'sleep' && pkill --full 'sleep' || true", "delta": "0:00:00.158772", "end": "2016-05-05 00:33:49.072139", "failed": true, "rc": -15, "start": "2016-05-05 00:33:48.913367", "stderr": "", "stdout": "385", "stdout_lines": ["385"], "warnings": []}

NO MORE HOSTS LEFT *************************************************************
to retry, use: --limit @aaa.retry

PLAY RECAP *********************************************************************
192.168.122.100 : ok=1 changed=0 unreachable=0 failed=1

当我直接运行命令时,它起作用了:

host # ssh root@192.168.122.100
vm # pgrep --full 'sleep' && pkill --full 'sleep' || true
vm # echo $?
0

请问您知道我做错了什么吗?

UPDATE 根据@Dag 的回答为我破译了这个:

$ ansible localhost -m shell -a "pgrep --list-full -f process_that_does_not_exists"
localhost | SUCCESS | rc=0 >>
828 /usr/bin/python /usr/bin/ansible localhost -m shell -a pgrep --list-full -f process_that_does_not_exists
835 /usr/bin/python /usr/bin/ansible localhost -m shell -a pgrep --list-full -f process_that_does_not_exists
836 /usr/bin/python /usr/bin/ansible localhost -m shell -a pgrep --list-full -f process_that_does_not_exists

所以带有 && pkill ... 的命令实际上杀死了一些 Ansible 的进程。看来我必须添加更多过滤才能使这项工作安全进行。

最佳答案

从您的输出中可以明显看出该命令返回 -15。这意味着该进程已终止。

如果我在本地主机上执行你的命令,我实际上是在杀死我自己的 ansible 运行。

[dag@moria ~]$ ansible localhost -m shell -a 'pgrep -f 'sleep' && pkill -f 'sleep' || true'
Terminated

因此,您正在从剧本中执行相同的操作,并且正在终止 Ansible 的部分进程,但不会终止太多,以便它可以正确地报告一些输出和返回代码。 (我实际上也在我自己的系统上杀死了主调用进程!)

关于bash - 带有 pgrep 的 Ansible shell 任务不断失败,在普通 shell 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37042643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com