10000 show example of async handlers or make it clear that they cannot be run async · Issue #84924 · ansible/ansible · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

show example of async handlers or make it clear that they cannot be run async #84924

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

Open
1 task done
dberardo-com opened this issue Apr 3, 2025 · 5 comments
Open
1 task done
Labels
needs_verified This issue needs to be verified/reproduced by maintainer

Comments

@dberardo-com
Copy link

Summary

i am trying to restart a service in an handler that takes quite some time to restart. so i want to do it with async/poll: 0 . but the handler task is currently waiting on the service to restart, ignoring the poll = 0 option.

Issue Type

Documentation Report

Component Name

handler

Ansible Version

$ ansible --version

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

linux

Additional Information

.

Code of Conduct

  • I agree to follow the Ansible Code of Conduct
@ansibot
Copy link
Contributor
ansibot commented Apr 3, 2025

Files identified in the description:

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

@ansibot ansibot added the needs_triage Needs a first human triage before being processed. label Apr 3, 2025
@s-hertel s-hertel added needs_verified This issue needs to be verified/reproduced by maintainer and removed needs_triage Needs a first human triage before being processed. labels Apr 3, 2025
@s-hertel
Copy link
Contributor
s-hertel commented Apr 3, 2025

i am trying to restart a service in an handler that takes quite some time to restart. so i want to do it with async/poll: 0 . but the handler task is currently waiting on the service to restart, ignoring the poll = 0 option.

It should work, but poll: 0 would be ignored if async is 0 (the default) too. If that's not the issue, can you share a reproducer of the issue and ansible --version?

Here's an example:

- hosts: localhost
  gather_facts: no
  handlers:

  - name: Run handler
    command: sleep 9
    async: 10
    poll: 0
    register: handler_async_result

  tasks:
  - name: Cause handler to run
    command: /bin/true
    notify: Run handler

  - name: Flush handler before ending playbook
    meta: flush_handlers

  - name: Wait for slow handler to finish
    async_status:
      jid: "{{ handler_async_result.ansible_job_id }}"
    register: job_result
    until: job_result.finished
    retries: 5
    delay: 2

  - name: Cleanup results file
    async_status:
      jid: "{{ handler_async_result.ansible_job_id }}"
      mode: cleanup

A handler is a type of task, so task keywords are available (with the exception of tags; handler limitations are documented here).

Here's the documentation for poll: 0 https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_async.html#run-tasks-concurrently-poll-0.

I can open a pull request against https://github.com/ansible/ansible-documentation to improve the documentation if you have suggestions.

@s-hertel s-hertel added the needs_info This issue requires further information. Please answer any outstanding questions. label Apr 3, 2025
@ansibot
Copy link
Contributor
ansibot commented Apr 24, 2025

@dberardo-com This Issue is waiting for your response. Please respond or the Issue will be closed.

click here for bot help

@dberardo-com
Copy link
Author

I will try to create a reproducible example,. But for the time being I have switched to use the raw: module which is faster in this case ... However I am having issues starting async jobs there since async is not supported in Ansible and the use of "&" to strat the command seems not to work either as the process is killed after the ssh connection is closed ...

What would be the best way to start a raw command asynchronously?

@ansibot ansibot removed the needs_info This issue requires further information. Please answer any outstanding questions. label Apr 27, 2025
@bcoca
Copy link
Member
bcoca commented Apr 28, 2025

this is due to the connection ending and closing the terminal, any attached process will end. You need to detach before this happens. One simple way to do this is to prefix your command with nohup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs_verified This issue needs to be verified/reproduced by maintainer
Projects
None yet
Development

No branches or pull requests

4 participants
0