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

failed task inside an include_tasks inside a rescue block does not remove host from ansible_play_hosts nor from ansible_play_batch #78612

Closed
1 task done
jmcgill298 opened this issue Aug 22, 2022 · 3 comments · Fixed by #78657
Labels
affects_2.13 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. P3 Priority 3 - Approved, No Time Limitation support:core This issue/PR relates to code supported by the Ansible Engineering Team. verified This issue has been verified/reproduced by maintainer

Comments

@jmcgill298
Copy link
Contributor

jmcgill298 commented Aug 22, 2022

Summary

I have created a role that has two tasks files: main and validation. The main task file uses block/rescue to validate the health of a system, with the rescue block trying to recover a failed system and then re-running the validation tasks. If the system still reports failure after an attempted recovery, I expect the failed host to be removed from ansible_play_hosts and ansible_play_batch; however they persist. Interestingly, the failed host(s) are excluded from future tasks in the play, and are excluded from subsequent plays in a playbook. I was only able to reproduce this if the rescue block had a failure from an include_tasks.

Issue Type

Bug Report

Component Name

rescue

Ansible Version

$ ansible --version

ansible [core 2.13.3]
  config file = None
  python version = 3.10.4 (main, Jun 29 2022, 12:14:53) [GCC 11.2.0]
  jinja version = 3.1.2
  libyaml = True

Configuration

# if using a version older than ansible-core 2.12 you should omit the '-t all'
$ ansible-config dump --only-changed -t all

OS / Environment

ubuntu 22.04

Steps to Reproduce

inventory:

[all]
a
b

roles/fail/tasks/validation.yml:

- fail:
  when: "inventory_hostname == 'a'"

roles/fail/tasks/main.yml:

- block:
    - include_tasks: "validation.yml"
  rescue:
    - debug:
        msg: "Failed for {{ inventory_hostname }}"
    - include_tasks: "validation.yml"

playbook:

- hosts: "all"
  gather_facts: false
  tasks:
    - include_role:
        name: "fail"

    - debug:
        msg: "{{ item }} is: {{ lookup('vars', item) }}"
      loop:
        - "ansible_play_hosts_all"
        - "ansible_play_hosts"
        - "ansible_play_batch"

- hosts: "all"
  gather_facts: false
  tasks:
    - debug:
        msg: "second play for {{ inventory_hostname }}"

Expected Results

I expected host a to be removed from ansible_play_hosts and ansible_play_batch

Actual Results

results:

PLAY [all] ***************************************************************************************************************************************************************************************************************************************************************************************************

TASK [include_role : fail] ***********************************************************************************************************************************************************************************************************************************************************************************

TASK [fail : include_tasks] **********************************************************************************************************************************************************************************************************************************************************************************
included: roles/fail/tasks/validation.yml for a, b

TASK [fail : fail] *******************************************************************************************************************************************************************************************************************************************************************************************
skipping: [b]
fatal: [a]: FAILED! => {"changed": false, "msg": "Failed as requested from task"}

TASK [fail : debug] ******************************************************************************************************************************************************************************************************************************************************************************************
ok: [a] => {
    "msg": "Failed for a"
}

TASK [fail : include_tasks] **********************************************************************************************************************************************************************************************************************************************************************************
included: roles/fail/tasks/validation.yml for a

TASK [fail : fail] *******************************************************************************************************************************************************************************************************************************************************************************************
fatal: [a]: FAILED! => {"changed": false, "msg": "Failed as requested from task"}

TASK [debug] *************************************************************************************************************************************************************************************************************************************************************************************************
ok: [b] => (item=ansible_play_hosts_all) => {
    "msg": "ansible_play_hosts_all is: ['a', 'b']"
}
ok: [b] => (item=ansible_play_hosts) => {
    "msg": "ansible_play_hosts is: ['a', 'b']"
}
ok: [b] => (item=ansible_play_batch) => {
    "msg": "ansible_play_batch is: ['a', 'b']"
}

PLAY [all] ***************************************************************************************************************************************************************************************************************************************************************************************************

TASK [debug] *************************************************************************************************************************************************************************************************************************************************************************************************
ok: [b] => {
    "msg": "second play for b"
}

PLAY RECAP ***************************************************************************************************************************************************************************************************************************************************************************************************
a                          : ok=3    changed=0    unreachable=0    failed=1    skipped=0    rescued=1    ignored=0   
b                          : ok=3    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   

Code of Conduct

  • I agree to follow the Ansible Code of Conduct
@ansibot
Copy link
Contributor

ansibot commented Aug 22, 2022

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.13 bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Aug 22, 2022
@s-hertel s-hertel added P3 Priority 3 - Approved, No Time Limitation needs_verified This issue needs to be verified/reproduced by maintainer verified This issue has been verified/reproduced by maintainer and removed needs_triage Needs a first human triage before being processed. needs_verified This issue needs to be verified/reproduced by maintainer labels Aug 23, 2022
@s-hertel
Copy link
Contributor

s-hertel commented Aug 23, 2022

A similar bug was recently fixed #72781 where tasks after a failure in a rescue would continue to execute, but this one is still an issue on devel.

mkrizek added a commit to mkrizek/ansible that referenced this issue Aug 29, 2022
@ansibot ansibot added the has_pr This issue has an associated PR. label Aug 29, 2022
@mkrizek
Copy link
Contributor

mkrizek commented Aug 29, 2022

A reproducer without using include_role/include_tasks:

- hosts: h1,h2
  gather_facts: false
  tasks:
    - block:
        - fail:
          when: inventory_hostname == 'h1'
      rescue:
        - block:
          - fail:
            when: inventory_hostname == 'h1'

    - assert:
        that:
          - "'h1' not in ansible_play_hosts"
          - "'h1' not in ansible_play_batch"

@ansible ansible locked and limited conversation to collaborators Sep 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.13 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. P3 Priority 3 - Approved, No Time Limitation support:core This issue/PR relates to code supported by the Ansible Engineering Team. verified This issue has been verified/reproduced by maintainer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants