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

docs example of using notify in a block: to call a handler doesn't work #33381

Closed
alikins opened this issue Nov 29, 2017 · 15 comments
Closed

docs example of using notify in a block: to call a handler doesn't work #33381

alikins opened this issue Nov 29, 2017 · 15 comments
Labels
affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. c:playbook/block docs This issue/PR relates to or includes documentation. docsite This issue/PR relates to the documentation website. playbook/block support:core This issue/PR relates to code supported by the Ansible Engineering Team. windows Windows community

Comments

@alikins
Copy link
Contributor

alikins commented Nov 29, 2017

ISSUE TYPE
  • Bug Report

  • Documentation Report

COMPONENT NAME

lib/ansible/playbook/block.py
lib/ansible/plugins/strategy/init.py
lib/ansible/executor/init.py
docs/docsite/rst/playbooks_blocks.rst

ANSIBLE VERSION
ansible 2.5.0 (devel fec39ba1f2) last updated 2017/11/29 09:53:12 (GMT -400)
  config file = None
  configured module search path = [u'/home/adrian/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/adrian/src/ansible/lib/ansible
  executable location = /home/adrian/src/ansible/bin/ansible
  python version = 2.7.14 (default, Nov  3 2017, 10:55:25) [GCC 7.2.1 20170915 (Red Hat 7.2.1-2)]
CONFIGURATION

default config

OS / ENVIRONMENT

fedora27 x86_64

SUMMARY

The example code at http://docs.ansible.com/ansible/latest/playbooks_blocks.html#id3
describing 'Another example is how to run handlers after an error occurred' doesn't work.

Not sure if the docs are wrong or if it is a bug

STEPS TO REPRODUCE
- name: example  # from http://docs.ansible.com/ansible/latest/playbooks_blocks.html#id3
  hosts: localhost
  gather_facts: false
  tasks:
     - name: Attempt and gracefull roll back demo
       block:
         - debug: msg='I execute normally'
           notify: run me even after an error
         - command: /bin/false
       rescue:
         - name: make sure all handlers run
           meta: flush_handlers
  handlers:
    - name: run me even after an error
      debug: msg='this handler runs even on error'
EXPECTED RESULTS

The 'run me even after an error' handler would run and debug display
the message ''this handler runs even on error''

ACTUAL RESULTS

The 'run me even after an error' doesn't appear to be run.

first, with no verbosity, then with -vvv:

[newswoop:F27:ansible (devel % u=)]$ ansible-playbook -i hosts block_example.yml 

PLAY [example] ***********************************************************************************

TASK [debug] *************************************************************************************
ok: [localhost] => {
    "msg": "I execute normally"
}

TASK [command] ***********************************************************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["/bin/false"], "delta": "0:00:00.002713", "end": "2017-11-29 10:04:51.748003", "msg": "non-zero return code", "rc": 1, "start": "2017-11-29 10:04:51.745290", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

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

with -vvv:

[newswoop:F27:ansible (devel % u=)]$ ansible-playbook -i hosts -vvv block_example.yml 
ansible-playbook 2.5.0 (devel fec39ba1f2) last updated 2017/11/29 09:53:12 (GMT -400)
  config file = None
  configured module search path = [u'/home/adrian/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/adrian/src/ansible/lib/ansible
  executable location = /home/adrian/src/ansible/bin/ansible-playbook
  python version = 2.7.14 (default, Nov  3 2017, 10:55:25) [GCC 7.2.1 20170915 (Red Hat 7.2.1-2)]
No config file found; using defaults
Parsed /home/adrian/src/ansible/hosts inventory source with ini plugin

PLAYBOOK: block_example.yml **********************************************************************
1 plays in block_example.yml

PLAY [example] ***********************************************************************************
META: ran handlers

TASK [debug] *************************************************************************************
task path: /home/adrian/src/ansible/block_example.yml:7
ok: [localhost] => {
    "msg": "I execute normally"
}

TASK [command] ***********************************************************************************
task path: /home/adrian/src/ansible/block_example.yml:9
Using module file /home/adrian/src/ansible/lib/ansible/modules/commands/command.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: adrian
<localhost> EXEC /bin/sh -c 'echo ~ && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/adrian/.ansible/tmp/ansible-tmp-1511967836.08-210701305656610 `" && echo ansible-tmp-1511967836.08-210701305656610="` echo /home/adrian/.ansible/tmp/ansible-tmp-1511967836.08-210701305656610 `" ) && sleep 0'
<localhost> PUT /tmp/tmpgVIyAa TO /home/adrian/.ansible/tmp/ansible-tmp-1511967836.08-210701305656610/command.py
<localhost> EXEC /bin/sh -c 'chmod u+x /home/adrian/.ansible/tmp/ansible-tmp-1511967836.08-210701305656610/ /home/adrian/.ansible/tmp/ansible-tmp-1511967836.08-210701305656610/command.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python /home/adrian/.ansible/tmp/ansible-tmp-1511967836.08-210701305656610/command.py; rm -rf "/home/adrian/.ansible/tmp/ansible-tmp-1511967836.08-210701305656610/" > /dev/null 2>&1 && sleep 0'
fatal: [localhost]: FAILED! => {
    "changed": true, 
    "cmd": [
        "/bin/false"
    ], 
    "delta": "0:00:00.001654", 
    "end": "2017-11-29 10:03:56.214393", 
    "invocation": {
        "module_args": {
            "_raw_params": "/bin/false", 
            "_uses_shell": false, 
            "chdir": null, 
            "creates": null, 
            "executable": null, 
            "removes": null, 
            "stdin": null, 
            "warn": true
        }
    }, 
    "msg": "non-zero return code", 
    "rc": 1, 
    "start": "2017-11-29 10:03:56.212739", 
    "stderr": "", 
    "stderr_lines": [], 
    "stdout": "", 
    "stdout_lines": []
}
META: ran handlers
META: ran handlers
META: ran handlers

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


@ansibot
Copy link
Contributor

ansibot commented Nov 29, 2017

Files identified in the description:

If these files are inaccurate, 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.5 This issue/PR affects Ansible v2.5 bug_report 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 Nov 29, 2017
@alikins
Copy link
Contributor Author

alikins commented Nov 29, 2017

Trying a few variants of that example doesn't seem to work either:

Also tried with an ignore_errors: on the 'command: /bin/false' but didn't seem to matter.

[newswoop:F27:ansible (devel % u=)]$ ansible-playbook -i hosts block_example.yml 

PLAY [example] ***********************************************************************************

TASK [debug] *************************************************************************************
ok: [localhost] => {
    "msg": "I execute normally"
}

TASK [command] ***********************************************************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["/bin/false"], "delta": "0:00:00.002713", "end": "2017-11-29 10:04:51.748003", "msg": "non-zero return code", "rc": 1, "start": "2017-11-29 10:04:51.745290", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

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

results:

[newswoop:F27:ansible (devel % u=)]$ ansible-playbook -i hosts -vv block_example_fix.yml 
PLAYBOOK: block_example_fix.yml ******************************************************************
1 plays in block_example_fix.yml

PLAY [example] ***********************************************************************************
META: ran handlers

TASK [debug] *************************************************************************************
task path: /home/adrian/src/ansible/block_example_fix.yml:7
ok: [localhost] => {
    "msg": "I execute normally"
}

TASK [command] ***********************************************************************************
task path: /home/adrian/src/ansible/block_example_fix.yml:10
fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["/bin/false"], "delta": "0:00:00.001665", "end": "2017-11-29 10:14:45.707840", "msg": "non-zero return code", "rc": 1, "start": "2017-11-29 10:14:45.706175", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

TASK [debug] *************************************************************************************
task path: /home/adrian/src/ansible/block_example_fix.yml:14
ok: [localhost] => {
    "msg": "I failed to execute normally."
}
META: ran handlers

TASK [debug] *************************************************************************************
task path: /home/adrian/src/ansible/block_example_fix.yml:20
ok: [localhost] => {
    "msg": "I always run"
}
META: ran handlers
META: ran handlers
META: ran handlers

PLAY RECAP ***************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=1   

@alikins
Copy link
Contributor Author

alikins commented Nov 29, 2017

pinging @jimi-c @dharmabumstead

@gundalow gundalow added c:playbook/block playbook/block and removed needs_triage Needs a first human triage before being processed. labels Nov 29, 2017
@flowerysong
Copy link
Contributor

This is a documentation bug; since the debug task doesn't return a 'changed' result the handler isn't notified and will never run. If you use a task that actually notifies the handler it should work as expected.

- name: example  # from http://docs.ansible.com/ansible/latest/playbooks_blocks.html#id3
  hosts: localhost
  gather_facts: false
  tasks:
     - name: Attempt and gracefull roll back demo
       block:
         - debug: msg='I execute normally'
           changed_when: true
           notify: run me even after an error
         - command: /bin/false
       rescue:
         - name: make sure all handlers run
           meta: flush_handlers
  handlers:
    - name: run me even after an error
      debug: msg='this handler runs even on error'
PLAY [example] *****************************************************************

TASK [debug] *******************************************************************
changed: [localhost] => {
    "msg": "I execute normally"
}

TASK [command] *****************************************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["/bin/false"], "delta": "0:00:00.002008", "end": "2017-11-29 19:41:21.177788", "msg": "non-zero return code", "rc": 1, "start": "2017-11-29 19:41:21.175780", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

RUNNING HANDLER [run me even after an error] ***********************************
ok: [localhost] => {
    "msg": "this handler runs even on error"
}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=1

@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 1, 2018
@ansibot ansibot added the docs This issue/PR relates to or includes documentation. label May 20, 2018
@ansibot
Copy link
Contributor

ansibot commented Aug 1, 2018

@ansibot ansibot added the support:community This issue/PR relates to code supported by the Ansible community. label Sep 18, 2018
@ansibot ansibot removed the support:community This issue/PR relates to code supported by the Ansible community. label Nov 26, 2018
@ansibot ansibot added the docsite This issue/PR relates to the documentation website. label Dec 4, 2018
@ansibot
Copy link
Contributor

ansibot commented Jan 16, 2019

@ansibot ansibot added the windows Windows community label Jan 16, 2019
@ansibot
Copy link
Contributor

ansibot commented Mar 5, 2019

Files identified in the description:

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

click here for bot help

@ShachafGoldstein
Copy link
Contributor

This issue is fixed in the docs site, can someone close?

@ansibot
Copy link
Contributor

ansibot commented Sep 1, 2019

Files identified in the description:

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

click here for bot help

@nitzmahone
Copy link
Member

closing, since docs seem to have been updated

@ansible ansible locked and limited conversation to collaborators Apr 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. c:playbook/block docs This issue/PR relates to or includes documentation. docsite This issue/PR relates to the documentation website. playbook/block support:core This issue/PR relates to code supported by the Ansible Engineering Team. windows Windows community
Projects
None yet
Development

No branches or pull requests

6 participants