Error handling &引用;ansible“任务失败”&&引用;ansible“结果失败”;当rescue块位于文件中时,变量未定义
当在ansible角色内从Error handling &引用;ansible“任务失败”&&引用;ansible“结果失败”;当rescue块位于文件中时,变量未定义,error-handling,ansible,ansible-playbook,ansible-2.x,Error Handling,Ansible,Ansible Playbook,Ansible 2.x,当在ansible角色内从main.yml调用的文件中使用块救援完成错误处理时,ansible\u failed\u任务和ansible\u failed\u结果变量给出未定义的错误 文件夹结构: roles/ role-test/ main.yml file_with_error_handling.yml main.yml - include file_with_error_handling.yml - block: # some code with erro
main.yml
调用的文件中使用块救援完成错误处理时,ansible\u failed\u任务
和ansible\u failed\u结果
变量给出未定义的错误
文件夹结构:
roles/
role-test/
main.yml
file_with_error_handling.yml
main.yml
- include file_with_error_handling.yml
- block:
# some code with error.
rescue:
debug:
msg: "Task- {{ ansible_failed_task }} failed with error {{ ansible_failed_result }}"
- hosts: all
become: yes
roles:
- role-test
- hosts: local
tasks:
- include: included_tasks.yml
- name: test block
block:
- name: Fail
fail:
msg: "test"
rescue:
- name: debug
debug:
var: ansible_failed_task
带有错误处理的文件.yml
- include file_with_error_handling.yml
- block:
# some code with error.
rescue:
debug:
msg: "Task- {{ ansible_failed_task }} failed with error {{ ansible_failed_result }}"
- hosts: all
become: yes
roles:
- role-test
- hosts: local
tasks:
- include: included_tasks.yml
- name: test block
block:
- name: Fail
fail:
msg: "test"
rescue:
- name: debug
debug:
var: ansible_failed_task
test.yml
- include file_with_error_handling.yml
- block:
# some code with error.
rescue:
debug:
msg: "Task- {{ ansible_failed_task }} failed with error {{ ansible_failed_result }}"
- hosts: all
become: yes
roles:
- role-test
- hosts: local
tasks:
- include: included_tasks.yml
- name: test block
block:
- name: Fail
fail:
msg: "test"
rescue:
- name: debug
debug:
var: ansible_failed_task
当执行
test.yml
时,我得到以下错误:
致命:[localhost]:失败!=>{“failed”:true,“msg”:"字段“args”有一个无效值,该值似乎包含一个未定义的变量。错误是:“ansible\u failed\u result”未定义\n\n错误似乎出现在“/tmp/test/role test/tasks/test\u main.yml”:第32行第6列,但根据确切的语法问题,可能\n出现在文件的其他位置。\n\n有问题的行出现在“/tmp/test/role test/tasks/test\u main.yml”中o be:\n\n#msg:\“ansible\u failed\u task-{{ansible\u failed\u task}}\”\n-调试:\n^here\n}
对于ansible\u failed\u task
变量,我得到了相同的错误
如果我在main.yml
中复制file\u的代码,并使用错误处理.yml
,然后执行它,它可以正常工作。我在ansible\u failed\u result
和ansible\u failed\u task
中获得值。只有从文件调用它时才会出现问题
有没有办法显示这些变量的输出?我找到了一个解决方法,尝试创建两个文件,一个block.yml和一个rescue.yml main.yml:
-块:
-名称:您的块任务
包括:block.yml
救援:
-名称:区块发生错误时的救援任务
包括:rescue.yml
block.yml:
#某些代码有错误。
rescue.yml:
调试:
msg:“任务-{ansible\u failed\u Task}}失败,错误为{{ansible\u failed\u result}”测试后,ansible
include/include\u tasks/import\u tasks时:
与文件include\u tasks.yml
包含block/rescue
tasks的not导出ansible\u failed\u结果
和ansible\u failed\u task
变量
playbook.yml
- include file_with_error_handling.yml
- block:
# some code with error.
rescue:
debug:
msg: "Task- {{ ansible_failed_task }} failed with error {{ ansible_failed_result }}"
- hosts: all
become: yes
roles:
- role-test
- hosts: local
tasks:
- include: included_tasks.yml
- name: test block
block:
- name: Fail
fail:
msg: "test"
rescue:
- name: debug
debug:
var: ansible_failed_task
包含的任务。yml
- include file_with_error_handling.yml
- block:
# some code with error.
rescue:
debug:
msg: "Task- {{ ansible_failed_task }} failed with error {{ ansible_failed_result }}"
- hosts: all
become: yes
roles:
- role-test
- hosts: local
tasks:
- include: included_tasks.yml
- name: test block
block:
- name: Fail
fail:
msg: "test"
rescue:
- name: debug
debug:
var: ansible_failed_task
将返回(在2.5和2.6中):
在2.7中工作良好的地方
> ansible --version && ansible-playbook playbook.yml
ansible 2.7.6
python version = 3.7.2 (default, Jan 3 2019, 02:55:40) [GCC 8.2.0]
PLAY [localhost] ***************************************************
TASK [Gathering Facts] *********************************************
ok: [localhost]
TASK [Fail] ********************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "test"}
TASK [debug] *******************************************************
ok: [localhost] => {
"ansible_failed_task": {
"action": "fail",
"any_errors_fatal": false,
"args": {
"msg": "test"
},
...
似乎它在2.9中再次被破坏:
]$ ansible --version && ansible-playbook broken.yml
ansible 2.9.0
python version = 2.7.17 (default, Oct 21 2019, 17:20:57) [GCC 9.2.1 20190827 (Red Hat 9.2.1-1)]
PLAY [localhost] ********************************************************************************************************************************************************************
TASK [Fail] *************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => changed=false
msg: test
TASK [debug] ************************************************************************************************************************************************************************
ok: [localhost] =>
ansible_failed_task: VARIABLE IS NOT DEFINED!
您假定的角色文件夹结构对无效。您的
main.yml
包含语法错误,使其成为带有字符串的列表(无:
)。您在Ansible中使用了block
内部角色。您的错误指的是/tmp/test/role test/tasks/test\u main.yml
文件,该文件在您的剧本中未被引用-由于语法错误,它不可能被包括在内。这完全是捏造的,不是一个严重的问题。对我来说也是Ansible\u失败的结果t
和ansible\u失败\u任务
抛出未定义,但这是因为使用\u项
循环,有没有方法使这些变量可用于循环任务?我有同样的问题。没有循环时它工作正常,但存在循环时它失败。