“ansible_failed_task”&当救援块在文件中时,“ansible_failed_result”变量未定义

时间:2016-12-12 15:32:47

标签: error-handling ansible ansible-playbook ansible-2.x

当在Ansible角色内从ansible_failed_task调用的文件中使用block-rescue进行错误处理时,ansible_failed_resultmain.yml变量会给出未定义的错误。

文件夹结构:

roles/
  role-test/
    main.yml
    file_with_error_handling.yml

main.yml

- include file_with_error_handling.yml

file_with_error_handling.yml

- block:
    # some code with error.
  rescue:
    debug:
      msg: "Task- {{ ansible_failed_task }} failed with error {{ ansible_failed_result }}"

test.yml

- hosts: all
  become: yes 
  roles:
    - role-test

执行test.yml时,我收到以下错误:

  

致命:[localhost]:失败! => {“failed”:true,“msg”:“字段'args'有一个无效值,它似乎包含一个未定义的变量。错误是:'ansible_failed_result'未定义\ n \ n错误似乎是在'/tmp/test/role-test/tasks/test_main.yml'中:第32行第6列,但可能在文件的其他位置,具体取决于确切的语法问题。\ n \ n违规行似乎是:\ n \ n#msg:\“ansible_failed_task- {{ansible_failed_task}} \”\ n - debug:\ n ^ here \ n“}

我为ansible_failed_task变量得到了同样的错误。

如果我在file_with_error_handling.yml中复制main.yml的代码然后执行它,它就可以正常工作。我得到了ansible_failed_resultansible_failed_task中的值。只有当我从文件中调用它时才会出现问题。

有没有办法可以显示这些变量的输出?

3 个答案:

答案 0 :(得分:2)

似乎在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!

答案 1 :(得分:0)

我找到了一种解决方法,尝试创建两个文件,一个是block.yml,一个是rescue.yml。

main.yml:

- block: - name: Your block tasks include: block.yml rescue: - name: Rescue task in case of error in block include: rescue.yml

block.yml:

# some code with error.

rescue.yml:

debug: msg: "Task- {{ ansible_failed_task }} failed with error {{ ansible_failed_result }}"

答案 2 :(得分:0)

经过测试后,Ansible

实际上,在将include/include_tasks/import_tasks:与包含included_tasks.yml任务的文件block/rescue一起使用时,导出ansible_failed_result和{{1} }变量。

ansible_failed_task

playbook.yml

- hosts: local tasks: - include: included_tasks.yml

included_tasks.yml

将返回(在2.5和2.6中):

- name: test block
  block:
    - name: Fail
      fail:
        msg: "test"
  rescue:
    - name: debug
      debug:
        var: ansible_failed_task

在2.7中效果很好的地方

> ansible --version && ansible-playbook playbook.yml
ansible 2.6.12                                                                                                               
  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": "VARIABLE IS NOT DEFINED!"
}