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

Bug - wrong task name with handlers #82307

Closed
1 task done
Sda79 opened this issue Nov 27, 2023 · 3 comments · Fixed by #83030
Closed
1 task done

Bug - wrong task name with handlers #82307

Sda79 opened this issue Nov 27, 2023 · 3 comments · Fixed by #83030
Labels
affects_2.16 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. verified This issue has been verified/reproduced by maintainer

Comments

@Sda79
Copy link

Sda79 commented Nov 27, 2023

Summary

I could produce a bug with the following playbook and inventory :

---
- name: test reproduction bug
  hosts: all
  tasks:
    - name: block
      block:
        - name: 1_debug
          debug:
            msg: "1 debug msg"

        - name: 2_some changes
          debug:
            msg: "2 debug msg"
          changed_when: true
          notify: handlers

        - name: 3_fail
          failed_when: "inventory_hostname == 'fails'"
          debug:
            msg: "3 fail msg"

        - name: Flush handlers
          meta: flush_handlers

      always:
        - name: 5_debug
          debug:
            msg: "5 debug msg"

  handlers:
    - name: 4_handler_1
      debug:
        msg: "4 handler msg 1"
      listen: "handlers"

    - name: 4_handler_2
      debug:
        msg: "4 handler msg 2"
      listen: "handlers"
---
all:
  hosts:
    success:
      ansible_host: 127.0.0.1
      ansible_user: foo
    fails:
      ansible_host: 127.0.0.1
      ansible_user: vagrant
      ansible_port: 2222

When the playbook is run (ansible-playbook -i inventory.yaml playbook.yml) :
I get the result :

PLAY [test reproduction bug] *********************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
[WARNING]: Platform darwin on host success is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python
interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.16/reference_appendices/interpreter_discovery.html for more
information.
ok: [success]
ok: [fails]

TASK [1_debug] ***********************************************************************************************************************************************
ok: [success] => {
    "msg": "1 debug msg"
}
ok: [fails] => {
    "msg": "1 debug msg"
}

TASK [2_some changes] ****************************************************************************************************************************************
changed: [success] => {
    "msg": "2 debug msg"
}
changed: [fails] => {
    "msg": "2 debug msg"
}

TASK [3_fail] ************************************************************************************************************************************************
ok: [success] => {
    "msg": "3 fail msg"
}
fatal: [fails]: FAILED! => {
    "msg": "3 fail msg"
}

TASK [Flush handlers] ****************************************************************************************************************************************

RUNNING HANDLER [4_handler_1] ********************************************************************************************************************************
ok: [success] => {
    "msg": "4 handler msg 1"
}

RUNNING HANDLER [4_handler_2] ********************************************************************************************************************************
ok: [success] => {
    "msg": "4 handler msg 2"
}

TASK [4_handler_2] *******************************************************************************************************************************************
ok: [fails] => {
    "msg": "5 debug msg"
}

TASK [5_debug] ***********************************************************************************************************************************************
ok: [success] => {
    "msg": "5 debug msg"
}

PLAY RECAP ***************************************************************************************************************************************************
fails                      : ok=4    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
success                    : ok=7    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

We can observe that the the task 5_debug on host fails is wrongly named: 4_handler_2.

This playbook was run with python 3.11.6 and ansible version :

ansible [core 2.16.0]
  config file = None
  configured module search path = ['/Users/sylvaindaste/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/sylvaindaste/0-Perso/works/bug-ansible/bug-ansible-3.11/lib/python3.11/site-packages/ansible
  ansible collection location = /Users/sylvaindaste/.ansible/collections:/usr/share/ansible/collections
  executable location = /Users/sylvaindaste/0-Perso/works/bug-ansible/bug-ansible-3.11/bin/ansible
  python version = 3.11.6 (main, Nov 27 2023, 20:19:53) [Clang 15.0.0 (clang-1500.0.40.1)] (/Users/sylvaindaste/0-Perso/works/bug-ansible/bug-ansible-3.11/bin/python3)
  jinja version = 3.1.2
  libyaml = True

Issue Type

Bug Report

Component Name

handlers

Ansible Version

ansible [core 2.16.0]
  config file = None
  configured module search path = ['/Users/sylvaindaste/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/sylvaindaste/0-Perso/works/bug-ansible/bug-ansible-3.11/lib/python3.11/site-packages/ansible
  ansible collection location = /Users/sylvaindaste/.ansible/collections:/usr/share/ansible/collections
  executable location = /Users/sylvaindaste/0-Perso/works/bug-ansible/bug-ansible-3.11/bin/ansible
  python version = 3.11.6 (main, Nov 27 2023, 20:19:53) [Clang 15.0.0 (clang-1500.0.40.1)] (/Users/sylvaindaste/0-Perso/works/bug-ansible/bug-ansible-3.11/bin/python3)
  jinja version = 3.1.2
  libyaml = True

Configuration

CONFIG_FILE() = None

OS / Environment

Macos 14.1.1 (23B81)

Steps to Reproduce

Playbook

---
- name: test reproduction bug
  hosts: all
  tasks:
    - name: block
      block:
        - name: 1_debug
          debug:
            msg: "1 debug msg"

        - name: 2_some changes
          debug:
            msg: "2 debug msg"
          changed_when: true
          notify: handlers

        - name: 3_fail
          failed_when: "inventory_hostname == 'fails'"
          debug:
            msg: "3 fail msg"

        - name: Flush handlers
          meta: flush_handlers

      always:
        - name: 5_debug
          debug:
            msg: "5 debug msg"

  handlers:
    - name: 4_handler_1
      debug:
        msg: "4 handler msg 1"
      listen: "handlers"

    - name: 4_handler_2
      debug:
        msg: "4 handler msg 2"
      listen: "handlers"

Inventory file :

---
all:
  hosts:
    success:
      ansible_host: 127.0.0.1
      ansible_user: foo
    fails:
      ansible_host: 127.0.0.1
      ansible_user: vagrant
      ansible_port: 2222

Expected Results

PLAY [test reproduction bug] *********************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
[WARNING]: Platform darwin on host success is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python
interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.16/reference_appendices/interpreter_discovery.html for more
information.
ok: [success]
ok: [fails]

TASK [1_debug] ***********************************************************************************************************************************************
ok: [success] => {
    "msg": "1 debug msg"
}
ok: [fails] => {
    "msg": "1 debug msg"
}

TASK [2_some changes] ****************************************************************************************************************************************
changed: [success] => {
    "msg": "2 debug msg"
}
changed: [fails] => {
    "msg": "2 debug msg"
}

TASK [3_fail] ************************************************************************************************************************************************
ok: [success] => {
    "msg": "3 fail msg"
}
fatal: [fails]: FAILED! => {
    "msg": "3 fail msg"
}

TASK [Flush handlers] ****************************************************************************************************************************************

RUNNING HANDLER [4_handler_1] ********************************************************************************************************************************
ok: [success] => {
    "msg": "4 handler msg 1"
}

RUNNING HANDLER [4_handler_2] ********************************************************************************************************************************
ok: [success] => {
    "msg": "4 handler msg 2"
}

TASK [5_debug] *******************************************************************************************************************************************
ok: [fails] => {
    "msg": "5 debug msg"
}

TASK [5_debug] ***********************************************************************************************************************************************
ok: [success] => {
    "msg": "5 debug msg"
}

PLAY RECAP ***************************************************************************************************************************************************
fails                      : ok=4    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
success                    : ok=7    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Actual Results

PLAY [test reproduction bug] *********************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
[WARNING]: Platform darwin on host success is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python
interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.16/reference_appendices/interpreter_discovery.html for more
information.
ok: [success]
ok: [fails]

TASK [1_debug] ***********************************************************************************************************************************************
ok: [success] => {
    "msg": "1 debug msg"
}
ok: [fails] => {
    "msg": "1 debug msg"
}

TASK [2_some changes] ****************************************************************************************************************************************
changed: [success] => {
    "msg": "2 debug msg"
}
changed: [fails] => {
    "msg": "2 debug msg"
}

TASK [3_fail] ************************************************************************************************************************************************
ok: [success] => {
    "msg": "3 fail msg"
}
fatal: [fails]: FAILED! => {
    "msg": "3 fail msg"
}

TASK [Flush handlers] ****************************************************************************************************************************************

RUNNING HANDLER [4_handler_1] ********************************************************************************************************************************
ok: [success] => {
    "msg": "4 handler msg 1"
}

RUNNING HANDLER [4_handler_2] ********************************************************************************************************************************
ok: [success] => {
    "msg": "4 handler msg 2"
}

TASK [4_handler_2] *******************************************************************************************************************************************
ok: [fails] => {
    "msg": "5 debug msg"
}

TASK [5_debug] ***********************************************************************************************************************************************
ok: [success] => {
    "msg": "5 debug msg"
}

PLAY RECAP ***************************************************************************************************************************************************
fails                      : ok=4    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
success                    : ok=7    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Code of Conduct

  • I agree to follow the Ansible Code of Conduct
@ansibot ansibot added bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. labels Nov 27, 2023
@ansibot
Copy link
Contributor

ansibot commented Nov 27, 2023

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.

@mkrizek mkrizek added the verified This issue has been verified/reproduced by maintainer label Nov 28, 2023
@mkrizek
Copy link
Contributor

mkrizek commented Nov 28, 2023

I was able to reproduce the issue. I will note that I also tested against my POC branch reworking some of the internals (PlayIterator) #82170 and it works correctly there.

@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label Nov 28, 2023
@Sda79
Copy link
Author

Sda79 commented Jan 11, 2024

Hi,

Could it be related to this method :

https://github.com/ansible/ansible/blob/a69fab86bc151e5a029e20647291ab189fdf6419/lib/ansible/plugins/callback/default.py#L164C1-L164C1

in the default callback module? With debugger I can see that the cached value is different that the task name :

(Pdb) l
187  
188             if task.check_mode and self.get_option('check_mode_markers'):
189                 checkmsg = " [CHECK MODE]"
190             else:
191                 checkmsg = ""
192  ->         self._display.banner(u"%s [%s%s]%s" % (prefix, task_name, args, checkmsg))
193  
194             if self._display.verbosity >= 2:
195                 self._print_task_path(task)
196  
197             self._last_task_banner = task._uuid
(Pdb) p task_name
'4_handler_2'
(Pdb) p task._name
'5_debug'
(Pdb) p self._last_task_
self._last_task_banner  self._last_task_name    
(Pdb) p self._last_task_name
'4_handler_2'

From what I see and when I run python debugger the Play_Iterator _get_next_task_from_state method seems to return a well formed task with the right name and host.

mkrizek added a commit to mkrizek/ansible that referenced this issue Apr 12, 2024
@ansibot ansibot added the has_pr This issue has an associated PR. label Apr 12, 2024
mkrizek added a commit to mkrizek/ansible that referenced this issue Apr 17, 2024
mkrizek added a commit to mkrizek/ansible that referenced this issue Apr 17, 2024
sivel pushed a commit that referenced this issue Apr 17, 2024
@ansible ansible locked and limited conversation to collaborators Apr 30, 2024
mattclay pushed a commit that referenced this issue May 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.16 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. verified This issue has been verified/reproduced by maintainer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants