英文:
How to know in which task an ansible play failed when launched using AWX API?
问题
我打算使用AWX API在AWX上启动Ansible作业,并从Ansible Playbook获取回调以了解播放结果。要这样做,我使用/api/v2/job_templates/<job-template-id>/launch/
,并在请求体中使用一些额外的变量来传递参数给我的Playbook。
这里是一个示例额外变量的JSON部分:
{
"extra_vars": {
"target": "w.x.y.z", (替换为实际的IP)
"directory_name_1": "dir1",
"directory_name_2": "dir2",
"file_name": "file1" (或者 "subdir/file1" 以引发错误)
}
}
此外,我还在作业模板中配置了Webhook通知,使用默认的自定义模板:{{ job_metadata }}
。
以下是我使用的Playbook示例,非常简单,它创建了两个目录和一个文件在第一个目录中:
- hosts: "{{ target }}"
name: fbplay
tasks:
- name: Create dummy directory 1
file:
path: "{{directory_name_1}}"
state: directory
- name: Create dummy directory 2
file:
path: "{{directory_name_2}}"
state: directory
- name: Create dummy file in directory
file:
path: "{{directory_name_1}}/{{file_name}}"
state: touch
mode: u=rw,g=r,o=r
所有这些都运行得很好,如果出现错误,将在目标机器上执行4个任务,但是在处理错误时,我如何指示回调哪个任务失败了呢?
实际上,我可以通过获取以下信息来知道Play是否失败:
"hosts": {
"w.x.y.z": {
"failed": true,
"changed": 0,
"dark": 0,
"failures": 1,
"ok": 3,
"processed": 1,
"skipped": 0,
"rescued": 0,
"ignored": 0
}
}
但是在发生故障时,我无法获取到确切失败的任务(在此示例中,是最后一个任务,因为传递了一个包含不存在的子目录的文件名)。
我是AWX和Ansible的新手,我正在处理我认为会比较简单的问题...所以任何提示或想法都受欢迎。
提前感谢。
英文:
I intend to launch ansible jobs on AWX using AWX api and get a call back from the ansible playbook to be informed about the result of the play.
To do so I'm using the /api/v2/job_templates/<job-template-id>/launch/
with some extra_vars in the body to pass parameters to my play.
{
"extra_vars": {
"target": "w.x.y.z", (put here a real IP)
"directory_name_1": "dir1",
"directory_name_2": "dir2",
"file_name": "file1" (or "subdir/file1" to make it fails)
}
}
I've also configured a webhook notification in the job-template with the default customization: {{ job_metadata }}
I've put here the play I'm using which is super simple, it creates 2 directories and one file in the first directory.
- hosts: "{{ target }}"
name: fbplay
tasks:
- name: Create dummy directory 1
file:
path: "{{directory_name_1}}"
state: directory
- name: Create dummy directory 2
file:
path: "{{directory_name_2}}"
state: directory
- name: Create dummy file in directory
file:
path: "{{directory_name_1}}/{{file_name}}"
state: touch
mode: u=rw,g=r,o=r
All of this work great and in case of error 4 tasks will be executed on the target machine:
TASK [Gathering Facts] *********************************************************
TASK [Create dummy directory 1] ************************************************
TASK [Create dummy directory 2] ************************************************
TASK [Create dummy file in directory] ******************************************
...but here is my question with regard to error handling: How can I indicate in the call back which task failed in case of error ?
In fact I can know if the play failed or not getting, in case of success:
"hosts": {
"w.x.y.z": {
"failed": false,
"changed": 1,
"dark": 0,
"failures": 0,
"ok": 4,
"processed": 1,
"skipped": 0,
"rescued": 0,
"ignored": 0
}
}
and in case of failure:
"hosts": {
"w.x.y.z": {
"failed": true,
"changed": 0,
"dark": 0,
"failures": 1,
"ok": 3,
"processed": 1,
"skipped": 0,
"rescued": 0,
"ignored": 0
}
}
But I cannot get the exact task that failed (in this case the last one by passing a filename containing a sub-directory that does not exist for example).
I'm a newbie on AWX & ansible and I'm fighting with what I thought would be a relatively simple point... so any hints or ideas is welcome.
Thx beforehand.
答案1
得分: 0
ARA可以帮助某些人,我确认它确实可以实现我之前寻找的功能,即在每次运行的playbook中显示使用其Web客户端(或通过API提供)哪个任务失败。
英文:
in case it helps someone, I actually confirm that ARA does what I was looking for above which is to display if using its web client (or provide through an API) exactly which task failed in each playbook you ran.
答案2
得分: 0
ARA在分析awx-runs方面做得很出色,但必须在AWX服务器上设置(运行AWX实例的主机),如果我理解正确。
一个解决方法是使用awx
命令行界面(请参阅https://docs.ansible.com/ansible-tower/latest/html/towercli/index.html和*):
watch --color 'awx --conf.host https://awx.site --conf.username my.user --conf.password "my_super_secret" -k -f human job_events list --job 3232445 --event "runner_on_failed" --filter "stdout"'
也可以浏览API以查找不同的"?event=runner_on_failed",例如:
https://your.awx.site/api/v2/jobs/3232445/job_events/?event=runner_on_failed
或其他"filters",例如:
awx --conf.host https://awx.site --conf.username my.user --conf.password "my_supersecret" -k -f human job_events list --job 3232445 --event "runner_on_unreachable" --filter "*">/tmp/tmp
https://docs.ansible.com/ansible-tower/latest/html/towercli/output.html#human-readable-tabular-formatting
https://docs.ansible.com/ansible-tower/latest/html/towercli/reference.html#awx-job-events-list
*(不要与awx-cli/tower-cli
混淆 - 我认为它们是不同的)
英文:
ARA does an excellent job for analyzing awx-runs, but it has to be set up on the AWX-server (the host running the AWX instance), if I understand correctly.
A workaroud would be to use the awx
commandline interface ( see https://docs.ansible.com/ansible-tower/latest/html/towercli/index.html and * ):
watch --color 'awx --conf.host https://awx.site --conf.username my.user --conf.password "my_super_secret" -k -f human job_events list --job 3232445 --event "runner_on_failed" --filter "stdout"'
One could also sroll the API for different "?event=runner_on_failed", like:
https://your.awx.site/api/v2/jobs/3232445/job_events/?event=runner_on_failed
Or other "filters" like:
awx --conf.host https://awx.site --conf.username my.user --conf.password "my_supersecret" -k -f human job_events list --job 3232445 --event "runner_on_unreachable" --filter "*" >/tmp/tmp
https://docs.ansible.com/ansible-tower/latest/html/towercli/output.html#human-readable-tabular-formatting
https://docs.ansible.com/ansible-tower/latest/html/towercli/reference.html#awx-job-events-list
* (not to confuse with awx-cli/tower-cli
- I think they are different)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论