Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ansible_failed_task references loop variables that are not available in rescue #74369

Closed
diego-treitos opened this issue Apr 21, 2021 · 3 comments
Labels
affects_2.10 This issue/PR affects Ansible v2.10 bug This issue/PR relates to a bug. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@diego-treitos
Copy link

Summary

It is my understanding that the bug #57399 references a problem when using a loop with include_role, however in that very same thread, a different bug is reported and it was not fixed in the PR #73881 and therefore still fails in ansible==3.3.0 and ansible-base==2.10.8.

Issue Type

Bug Report

Component Name

ansible

Ansible Version

$ ansible --version

ansible 2.10.8
  config file = None
  configured module search path = ['/home/ans/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.7/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.7.3 (default, Jan 22 2021, 20:04:44) [GCC 8.3.0]

Configuration

$ ansible-config dump --only-changed

OS / Environment

Debian 10 (buster)

Steps to Reproduce

Here is the example code from #57399

- hosts: localhost
  gather_facts: no
  vars:
    ansible_connection: local
    data:
      - name: a1
      - name: a2
      - name: a3

  tasks:
    - debug:
        msg: "{{ data }}"
    - name: Block START
      block:
      - name: LOOP TASK
        debug:
          msg: "The element is: {{ item.name }}"
        loop: "{{ data }}"
        failed_when: item.name == 'a2'

      rescue:
        - name: Set error info report
          set_fact:
            error_info:
              title: "{{ansible_failed_task}}"
              details: "{{ansible_failed_result}}"

        - name: print ansible_failed_task
          debug:
            var: ansible_failed_task

        - name: print ansible_failed_result
          debug:
            var: ansible_failed_result

In this example, the task Set error info report, inside rescue fails because ansible_failed_task has a variable called item which is not defined inside rescue but was in the failed task (LOOP TASK)

Expected Results

It should rescue properly and continue executions

Actual Results

The rescue fails



PLAY [localhost] **************************************************************************************************

TASK [debug] ******************************************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "name": "a1"
        },
        {
            "name": "a2"
        },
        {
            "name": "a3"
        }
    ]
}

TASK [LOOP TASK] **************************************************************************************************
ok: [localhost] => (item={'name': 'a1'}) => {
    "msg": "The element is: a1"
}
failed: [localhost] (item={'name': 'a2'}) => {
    "msg": "The element is: a2"
}
ok: [localhost] => (item={'name': 'a3'}) => {
    "msg": "The element is: a3"
}
fatal: [localhost]: FAILED! => {"msg": "All items completed"}

TASK [Set error info report] **************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: {'args': {'msg': 'The element is: {{ item.name }}'}, 'action': 'debug', 'async_val': 0, 'async': 0, 'changed_when': [], 'delay
': 5, 'delegate_to': None, 'delegate_facts': None, 'failed_when': \"item.name == 'a2'\", 'loop': '{{ data }}', 'loop_control': None, 'notify': None, 'poll': 15, 'register': None, 'retries': 3, 'until': [], 'loop_with': None, 'name': 'LOO
P TASK', 'connection': 'smart', 'port': None, 'remote_user': None, 'vars': {}, 'module_defaults': [], 'environment': [], 'no_log': None, 'run_once': None, 'ignore_errors': None, 'ignore_unreachable': None, 'check_mode': True, 'diff': Fal
se, 'any_errors_fatal': False, 'throttle': 0, 'timeout': 0, 'debugger': None, 'become': False, 'become_method': 'sudo', 'become_user': None, 'become_flags': None, 'become_exe': None, 'when': [], 'tags': [], 'collections': [], 'uuid': '56
000250-84dd-b474-1c28-000000000009', 'finalized': True, 'squashed': True}: 'item' is undefined\n\nThe error appears to be in '/home/ans/ansible/test.yml': line 22, column 11, but may\nbe elsewhere in the file depending on the exact synta
x problem.\n\nThe offending line appears to be:\n\n      rescue:\n        - name: Set error info report\n          ^ here\n"}

PLAY RECAP ********************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=1    ignored=0


### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct
@ansibot
Copy link
Contributor

ansibot commented Apr 21, 2021

Files identified in the description:
None

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibot ansibot added affects_2.10 This issue/PR affects Ansible v2.10 bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Apr 21, 2021
@diego-treitos
Copy link
Author

NOTE: As a very dirty workaround, you can make this work if you declare the item variable in the rescue section with all the keys used in all the tasks inside the block.

For the provided example, the workaround would be:

- hosts: localhost
  gather_facts: no
  vars:
    ansible_connection: local
    data:
      - name: a1
      - name: a2
      - name: a3

  tasks:
    - debug:
        msg: "{{ data }}"
    - name: Block START
      block:
      - name: LOOP TASK
        debug:
          msg: "The element is: {{ item.name }}"
        loop: "{{ data }}"
        failed_when: item.name == 'a2'

      rescue:
        - name: workaround to BUG https://github.com/ansible/ansible/issues/74369
          set_fact:
            item:
              name: ''

        - name: Set error info report
          set_fact:
            error_info:
              title: "{{ ansible_failed_task }}"
              details: "{{ ansible_failed_result }}"

        - name: print ansible_failed_task
          debug:
            var: ansible_failed_task

        - name: print ansible_failed_result
          debug:
            var: ansible_failed_result

If you have other tasks inside block that use a loop with the item variable, you have to include all of the item keys in case any of them fail. For example:

  rescue:
    - name: workaround to BUG https://github.com/ansible/ansible/issues/74369
      set_fact:
        item:
          path: ''
          mode: ''
          owner: ''
          group: ''
          src: ''
          dst: ''

Even if item.path and item.src are used in different tasks. What is important is that they are defined.

@sivel
Copy link
Member

sivel commented Apr 21, 2021

This has already been resolved in #74290

If you have further questions please stop by IRC or the mailing list:

@sivel sivel closed this as completed Apr 21, 2021
@mkrizek mkrizek removed the needs_triage Needs a first human triage before being processed. label Apr 22, 2021
@ansible ansible locked and limited conversation to collaborators May 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.10 This issue/PR affects Ansible v2.10 bug This issue/PR relates to a bug. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

No branches or pull requests

4 participants