Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"ansible_failed_task" & "ansible_failed_result" variables are undefined when rescue block is in a file

The ansible_failed_task and ansible_failed_result variables give undefined error, when error handling is done using block-rescue in a file which is called from main.yml inside an Ansible role.

Folder structure:

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

When test.yml is executed I get the below error:

fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible_failed_result' is undefined\n\nThe error appears to have been in '/tmp/test/role-test/tasks/test_main.yml': line 32, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n# msg: \"ansible_failed_task- {{ ansible_failed_task }}\"\n - debug:\n ^ here\n"}

And the same error I get for ansible_failed_task variable.

If I copy the code of file_with_error_handling.yml in main.yml and then execute it, it works fine. I get the values in ansible_failed_result and ansible_failed_task. The problem occurs only when I call it from a file.

Is there a way I can display the output of these variables?

like image 249
Parth Shah Avatar asked Oct 18 '22 20:10

Parth Shah


1 Answers

Seems it's broken again in 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!
like image 155
user66771 Avatar answered Oct 21 '22 06:10

user66771