首页 > 解决方案 > Ansible 不返回异步任务的作业 ID

问题描述

根据文档,当我运行 async ad hoc 命令时,我应该得到一个作业 ID,以便稍后检查状态。这没有发生:

$ ansible all -a "sleep 60" -P 0 -B 86400
node1 | SUCCESS | rc=-1 >>


node2 | SUCCESS | rc=-1 >>


node0 | SUCCESS | rc=-1 >>


node3 | SUCCESS | rc=-1 >>


$

sleep命令在所有节点上运行,因此该任务确实运行正确:

$ ssh node2 ps aux | grep sleep
$ ansible node2 --user ${USER} --background 3600 --poll 0 --args "/bin/sleep 10s"
node2 | SUCCESS | rc=-1 >>


$ ssh node2 ps aux | grep sleep
user  7232  0.0  0.0   7468   744 ?        S    13:54   0:00 /bin/sleep 10s
$

但是async_status由于我不知道作业 ID,所以我无法检查作业的状态。有没有办法列出当前正在运行的异步作业?或者其他获取工作ID的方法?

Ubuntu 18.04.1、ansible 2.5.1、python 2.7.17。

标签: ansible

解决方案


根据您的输出,返回代码rc=-1和命令sleep仅没有完整路径。

在具有 Ansible 2.9.25 和 Python 2.7.5 的 RHEL7.9.9 系统上,用户的异步临时任务的输出看起来像

ansible test --user ${USER} --ask-pass --background 3600 --poll 0 --args "/usr/bin/sleep 1m"
SSH password:
test1.example.com | CHANGED => {
    "ansible_job_id": "211363790122.167971",
    "changed": true,
    "finished": 0,
    "results_file": "/home/test/.ansible_async/211363790122.167971",
    "started": 1
}
test2.example.com | CHANGED => {
    "ansible_job_id": "289811848480.63295",
    "changed": true,
    "finished": 0,
    "results_file": "/home/test/.ansible_async/289811848480.63295",
    "started": 1
}

对于超级用户来说

ansible test --user ${USER} --ask-pass --become --background 3600 --poll 0 --args "/usr/bin/sleep 1m"
SSH password:
test1.example.com | CHANGED => {
    "ansible_job_id": "494650580974.167671",
    "changed": true,
    "finished": 0,
    "results_file": "/root/.ansible_async/494650580974.167671",
    "started": 1
}
test2.example.com | CHANGED => {
    "ansible_job_id": "273855897479.62987",
    "changed": true,
    "finished": 0,
    "results_file": "/root/.ansible_async/273855897479.62987",
    "started": 1
}

您可以使用几个不同的临时命令执行一些测试,例如

ansible test --user ${USER} --ask-pass --args "python --version"
SSH password:
test1.example.com | CHANGED | rc=0 >>
Python 2.7.5
test2.example.com | CHANGED | rc=0 >>
Python 2.7.5

有和没有完整路径等等。也因为rc=-1

ansible test --user ${USER} --ask-pass --module-name shell --args "echo test; echo rc=$?"
test1.example.com | CHANGED | rc=0 >>
test
rc=0

ansible test --user ${USER} --ask-pass --module-name shell --args "echo 'test '; exit -1;"
test1.example.com | FAILED | rc=255 >>
test non-zero return code

推荐阅读