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

No linear execution in handler after include_tasks with when condition #83019

Open
1 task done
alvinkwekel opened this issue Apr 11, 2024 · 4 comments · May be fixed by #83209
Open
1 task done

No linear execution in handler after include_tasks with when condition #83019

alvinkwekel opened this issue Apr 11, 2024 · 4 comments · May be fixed by #83209
Labels
affects_2.18 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. P3 Priority 3 - Approved, No Time Limitation verified This issue has been verified/reproduced by maintainer

Comments

@alvinkwekel
Copy link

alvinkwekel commented Apr 11, 2024

Summary

We have a use-case where we want to restart a cluster, node by node with a handler. We use a when condition on an include_tasks to accomplish this. First we include the restart tasks for host 1, with a when condition. This is skipped for host 2. Afterwards we have another include_tasks which is only applied to host 2.

This workes on Ansible 6, but when upgrading to Ansible 8 this stopped working as expected. I was not able to find a change in the change log that specifically explains this behaviour, although I can see there are several changes made in this area.

I appears to me as a bug that a conditional (when) include_tasks behind a handler does not follow linear execution. What I see is that the host for which the condition is not true continues with the next task, while the host for which is condition is true, is executing the included tasks. If the include_tasks with a when condition is not behind a handler but simply in the tasks of the role itself, then all included tasks are executed on the host that meets the when condition, while the other host waits and then they continue together afterwards. Which is as I'd expect.

If I replace the include_tasks behind the handler with an import_tasks which does a static (parse-time) import of the tasks. Then host 1 and 2 linearly run all imported tasks with the when condition on each task. This provides more or less the same outcome as with the include_task in Ansible 6. But I'like to understand why the include_tasks is no longer working as before.

I've prepared a simple role to demonstrate the behavior with an include_task in the tasks of the role, and an import and include in the handler. In the expected result I've put the output of Ansible 6 in the actual result that of Ansible 8.7.0.

Issue Type

Bug Report

Component Name

core

Ansible Version

$ ansible --version
{
    "ansible_version": {
        "full": "2.15.9",
        "major": 2,
        "minor": 15,
        "revision": 9,
        "string": "2.15.9"
    }
}

Configuration

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

Omitted until desired and not reproducible without.

OS / Environment

Ubuntu 20.04

Steps to Reproduce

tasks/main.yml

- name: Restart primary broker (tasks)
  include_tasks: handlers/restart_broker.yml
  when: artemis_role == 'primary'

- name: Broker restart end (tasks)
  debug:
    msg: Broker restart end (tasks)

- name: Restart broker if requested
  debug:
    msg: Broker restart requested
  changed_when: true
  notify: Restart artemis

handlers/main.yml

- name: Restart primary broker
  include_tasks: handlers/restart_brokers.yml
  listen: Restart artemis

handlers/restart_brokers.yml

- name: Restart primary broker (import)
  import_tasks: handlers/restart_broker.yml
  when: artemis_role == 'primary'

- name: Broker restart end (import)
  debug:
    msg: Broker restart end (import)

- name: Restart primary broker (include)
  include_tasks: handlers/restart_broker.yml
  when: artemis_role == 'primary'

- name: Broker restart end (include)
  debug:
    msg: Broker restart end (include)

handlers/restart_broker.yml

- name: Broker restart step 1
  debug:
    msg: Broker restart step 1

- name: Broker restart step 2
  debug:
    msg: Broker restart step 2

- name: Broker restart step 3
  debug:
    msg: Broker restart step 3

Expected Results

PLAY [Apply artemis_restart role] **********************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************
ok: [server-01]
ok: [server-02]

TASK [artemis_restart : Restart primary broker (tasks)] ************************************************************************************************
skipping: [server-02]
included: /home/kwekela/ansbile/roles/artemis_restart/handlers/restart_broker.yml for server-01

TASK [artemis_restart : Broker restart step 1] *********************************************************************************************************
ok: [server-01] => {
    "msg": "Broker restart step 1"
}

TASK [artemis_restart : Broker restart step 2] *********************************************************************************************************
ok: [server-01] => {
    "msg": "Broker restart step 2"
}

TASK [artemis_restart : Broker restart step 3] *********************************************************************************************************
ok: [server-01] => {
    "msg": "Broker restart step 3"
}

TASK [artemis_restart : Broker restart end (tasks)] ****************************************************************************************************
ok: [server-01] => {
    "msg": "Broker restart end (tasks)"
}
ok: [server-02] => {
    "msg": "Broker restart end (tasks)"
}

TASK [artemis_restart : Restart broker if requested] ***************************************************************************************************
changed: [server-01] => {
    "msg": "Broker restart requested"
}
changed: [server-02] => {
    "msg": "Broker restart requested"
}

RUNNING HANDLER [artemis_restart : Restart primary broker] *********************************************************************************************
included: /home/kwekela/ansbile/roles/artemis_restart/handlers/restart_brokers.yml for server-01, server-02

RUNNING HANDLER [artemis_restart : Broker restart step 1] **********************************************************************************************
ok: [server-01] => {
    "msg": "Broker restart step 1"
}
skipping: [server-02]

RUNNING HANDLER [artemis_restart : Broker restart step 2] **********************************************************************************************
ok: [server-01] => {
    "msg": "Broker restart step 2"
}
skipping: [server-02]

RUNNING HANDLER [artemis_restart : Broker restart step 3] **********************************************************************************************
ok: [server-01] => {
    "msg": "Broker restart step 3"
}
skipping: [server-02]

RUNNING HANDLER [artemis_restart : Broker restart end (import)] ****************************************************************************************
ok: [server-01] => {
    "msg": "Broker restart end (import)"
}
ok: [server-02] => {
    "msg": "Broker restart end (import)"
}

RUNNING HANDLER [artemis_restart : Restart primary broker (include)] ***********************************************************************************
skipping: [server-02]
included: /home/kwekela/ansbile/roles/artemis_restart/handlers/restart_broker.yml for server-01

RUNNING HANDLER [artemis_restart : Broker restart step 1] **********************************************************************************************
ok: [server-01] => {
    "msg": "Broker restart step 1"
}

RUNNING HANDLER [artemis_restart : Broker restart step 2] **********************************************************************************************
ok: [server-01] => {
    "msg": "Broker restart step 2"
}

RUNNING HANDLER [artemis_restart : Broker restart step 3] **********************************************************************************************
ok: [server-01] => {
    "msg": "Broker restart step 3"
}

RUNNING HANDLER [artemis_restart : Broker restart end (include)] ***************************************************************************************
ok: [server-01] => {
    "msg": "Broker restart end (include)"
}
ok: [server-02] => {
    "msg": "Broker restart end (include)"
}

PLAY RECAP *********************************************************************************************************************************************
server-01                : ok=17   changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
server-02                : ok=6    changed=1    unreachable=0    failed=0    skipped=5    rescued=0    ignored=0

Actual Results

PLAY [Apply artemis_restart role] **********************************************

TASK [Gathering Facts] *********************************************************
ok: [server-01]
ok: [server-02]

TASK [artemis_restart : Restart primary broker (tasks)] ************************
skipping: [server-02]
included: /home/kwekela/ansbile/roles/artemis_restart/handlers/restart_broker.yml for server-01

TASK [artemis_restart : Broker restart step 1] *********************************
ok: [server-01] => {
    "msg": "Broker restart step 1"
}

TASK [artemis_restart : Broker restart step 2] *********************************
ok: [server-01] => {
    "msg": "Broker restart step 2"
}

TASK [artemis_restart : Broker restart step 3] *********************************
ok: [server-01] => {
    "msg": "Broker restart step 3"
}

TASK [artemis_restart : Broker restart end (tasks)] ****************************
ok: [server-01] => {
    "msg": "Broker restart end (tasks)"
}
ok: [server-02] => {
    "msg": "Broker restart end (tasks)"
}

TASK [artemis_restart : Restart broker if requested] ***************************
changed: [server-01] => {
    "msg": "Broker restart requested"
}
changed: [server-02] => {
    "msg": "Broker restart requested"
}

RUNNING HANDLER [artemis_restart : Restart primary broker] *********************
included: /home/kwekela/ansbile/roles/artemis_restart/handlers/restart_brokers.yml for server-01, server-02

RUNNING HANDLER [artemis_restart : Broker restart step 1] **********************
ok: [server-01] => {
    "msg": "Broker restart step 1"
}
skipping: [server-02]

RUNNING HANDLER [artemis_restart : Broker restart step 2] **********************
ok: [server-01] => {
    "msg": "Broker restart step 2"
}
skipping: [server-02]

RUNNING HANDLER [artemis_restart : Broker restart step 3] **********************
ok: [server-01] => {
    "msg": "Broker restart step 3"
}
skipping: [server-02]

RUNNING HANDLER [artemis_restart : Broker restart end (import)] ****************
ok: [server-01] => {
    "msg": "Broker restart end (import)"
}
ok: [server-02] => {
    "msg": "Broker restart end (import)"
}

RUNNING HANDLER [artemis_restart : Restart primary broker (include)] ***********
skipping: [server-02]
included: /home/kwekela/ansbile/roles/artemis_restart/handlers/restart_broker.yml for server-01

RUNNING HANDLER [artemis_restart : Broker restart step 1] **********************
ok: [server-01] => {
    "msg": "Broker restart step 1"
}

TASK [artemis_restart : Broker restart step 1] *********************************
ok: [server-02] => {
    "msg": "Broker restart end (include)"
}

RUNNING HANDLER [artemis_restart : Broker restart step 2] **********************
ok: [server-01] => {
    "msg": "Broker restart step 2"
}

RUNNING HANDLER [artemis_restart : Broker restart step 3] **********************
ok: [server-01] => {
    "msg": "Broker restart step 3"
}

RUNNING HANDLER [artemis_restart : Broker restart end (include)] ***************
ok: [server-01] => {
    "msg": "Broker restart end (include)"
}

PLAY RECAP *********************************************************************
server-01                : ok=17   changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
server-02                : ok=6    changed=1    unreachable=0    failed=0    skipped=5    rescued=0    ignored=0

Code of Conduct

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

ansibot commented Apr 11, 2024

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.

@ansibot ansibot added bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. labels Apr 11, 2024
@mkrizek
Copy link
Contributor

mkrizek commented Apr 11, 2024

This appears to be another case of #82307 but unlike that issue #82170 isn't (yet) fixing it: #82307 (comment).

@mkrizek mkrizek added verified This issue has been verified/reproduced by maintainer affects_2.18 labels Apr 11, 2024
@mkrizek
Copy link
Contributor

mkrizek commented Apr 11, 2024

$ ansible --version
{
"ansible_version": {
"full": "2.15.9",
"major": 2,
"minor": 15,
"revision": 9,
"string": "2.15.9"
}
}

Also, for next time, please note that we ask for output of ansible --version and not arbitrarily formatted version output for a reason (our github bot relies on that for parsing, there is more valuable information in ansible --version than just the version). Thanks!

@bcoca bcoca added P3 Priority 3 - Approved, No Time Limitation and removed needs_triage Needs a first human triage before being processed. labels Apr 11, 2024
@mkrizek
Copy link
Contributor

mkrizek commented Apr 12, 2024

Minimal reproducer:

- hosts: h1,h2
  gather_facts: false
  tasks:
    - command: echo
      notify: handler
  handlers:
    - name: handler
      include_tasks: 83019-handler.yml

83019-handler.yml:

- include_tasks: 83019-handler-nested.yml
  when: inventory_hostname == "h1"

- name: handler2
  debug:
    msg: handler2

83019-handler-nested.yml:

- name: handler1
  debug:
    msg: handler1

Output:

PLAY [h1,h2] ***************************************************************************************************************************************

TASK [command] *************************************************************************************************************************************
changed: [h2]
changed: [h1]

RUNNING HANDLER [handler] **************************************************************************************************************************
included: /Users/mkrizek/src/ansible-issues/83019-handler.yml for h1, h2

RUNNING HANDLER [include_tasks] ********************************************************************************************************************
skipping: [h2]
included: /Users/mkrizek/src/ansible-issues/83019-handler-nested.yml for h1

RUNNING HANDLER [handler1] *************************************************************************************************************************
ok: [h1] => {
    "msg": "handler1"
}

TASK [handler1] ************************************************************************************************************************************
ok: [h2] => {
    "msg": "handler2"
}

RUNNING HANDLER [handler2] *************************************************************************************************************************
ok: [h1] => {
    "msg": "handler2"
}

PLAY RECAP *****************************************************************************************************************************************
h1                         : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
h2                         : ok=3    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

mkrizek added a commit to mkrizek/ansible that referenced this issue May 7, 2024
@mkrizek mkrizek linked a pull request May 7, 2024 that will close this issue
@ansibot ansibot added the has_pr This issue has an associated PR. label May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects_2.18 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. P3 Priority 3 - Approved, No Time Limitation verified This issue has been verified/reproduced by maintainer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants