How to show progress for a long-running Ansible task?

How to show progress for a long-running Ansible task?

In Ansible, you can show progress for a long-running task by using the async and poll features. The async keyword allows a task to run asynchronously, and the poll parameter allows you to check the status of the running task. Here's a basic example:

- name: Long-running task with progress command: long_running_command async: 600 # Set the maximum runtime in seconds (adjust based on your task) poll: 5 # Check the status every 5 seconds (adjust based on your preferences) - name: Wait for the task to finish async_status: jid: "{{ ansible_job_id }}" # Use the job ID from the async task register: job_result until: job_result.finished retries: 120 # Retry for a total of 120 checks (adjust based on your preferences) - debug: var: job_result 

In this example:

  • The async parameter is set to 600 seconds, meaning the task will run asynchronously for a maximum of 600 seconds.
  • The poll parameter is set to 5 seconds, indicating that Ansible should check the status of the task every 5 seconds.

Adjust the values based on the expected duration of your long-running task.

Note that not all Ansible modules support asynchronous execution. In the example above, the command module is used, but you may need to adapt it based on the module you're using for your specific task.

To run the playbook, use the following command:

ansible-playbook your_playbook.yml 

During the execution, Ansible will display progress updates for the task. You can customize the playbook according to your specific task and requirements.

Examples

  1. "Ansible long-running task progress bar"

    • Code:
      --- - name: Long Running Task with Progress Bar hosts: your_target_hosts tasks: - name: Display Progress Bar command: "/path/to/long_running_script.sh" async: 600 poll: 5 register: task_result notify: Update Progress Bar - name: Wait for task completion async_status: jid: "{{ task_result.ansible_job_id }}" register: job_result until: job_result.finished retries: 120 delay: 5 handlers: - name: Update Progress Bar debug: msg: "Task is {{ item.ansible_job_result.progress_percent }}% complete." loop: "{{ task_result.ansible_job_result | default([]) }}" 
    • Description: This Ansible playbook uses the async and poll parameters to run a long-running task asynchronously and display progress using a debug message in a loop.
  2. "Ansible show progress during playbook execution"

    • Code:
      --- - name: Playbook with Progress Display hosts: your_target_hosts gather_facts: false tasks: - name: Long Running Task command: "/path/to/long_running_script.sh" register: task_result - name: Display Progress debug: msg: "Task is in progress. Current status: {{ task_result.stdout_lines | last }}" 
    • Description: This Ansible playbook executes a long-running task and displays progress by printing the latest output line of the task using the debug module.
  3. "Ansible verbose output for long-running task"

    • Code:
      --- - name: Verbose Output for Long Running Task hosts: your_target_hosts tasks: - name: Long Running Task command: "/path/to/long_running_script.sh" register: task_result args: warn: false - name: Display Task Output debug: var: task_result.stdout_lines 
    • Description: To show detailed output during a long-running task, use the debug module to display the task's standard output.
  4. "Ansible custom progress indicator"

    • Code:
      --- - name: Custom Progress Indicator hosts: your_target_hosts tasks: - name: Long Running Task command: "/path/to/long_running_script.sh" async: 600 poll: 10 register: task_result notify: Update Progress Indicator - name: Wait for task completion async_status: jid: "{{ task_result.ansible_job_id }}" register: job_result until: job_result.finished retries: 120 delay: 5 handlers: - name: Update Progress Indicator debug: msg: "Processing... {{ '*' * ((item.ansible_job_result.elapsed | default(0)) // 10) }}" loop: "{{ task_result.ansible_job_result | default([]) }}" 
    • Description: This playbook uses a custom progress indicator, where the number of '*' characters in the debug message represents the elapsed time of the long-running task.
  5. "Ansible playbook real-time status updates"

    • Code:
      --- - name: Real-time Status Updates hosts: your_target_hosts tasks: - name: Long Running Task command: "/path/to/long_running_script.sh" register: task_result async: 600 poll: 10 notify: Display Status - name: Wait for task completion async_status: jid: "{{ task_result.ansible_job_id }}" register: job_result until: job_result.finished retries: 120 delay: 5 handlers: - name: Display Status debug: msg: "Task status: {{ task_result.ansible_job_result.status }}" 
    • Description: This playbook shows real-time status updates by using the async and poll parameters, combined with a notify handler to display the current status.
  6. "Ansible progress bar for playbook execution"

    • Code:
      --- - name: Progress Bar for Playbook hosts: your_target_hosts tasks: - name: Long Running Task command: "/path/to/long_running_script.sh" async: 600 poll: 10 register: task_result notify: Display Progress Bar - name: Wait for task completion async_status: jid: "{{ task_result.ansible_job_id }}" register: job_result until: job_result.finished retries: 120 delay: 5 handlers: - name: Display Progress Bar ansible.builtin.include_tasks: progress_bar.yml 
      progress_bar.yml:
      --- - name: Include Progress Bar Tasks debug: msg: "Task is {{ item.ansible_job_result.progress_percent }}% complete." loop: "{{ task_result.ansible_job_result | default([]) }}" 
    • Description: This playbook incorporates a progress bar by using a separate YAML file (progress_bar.yml) with tasks to display the progress percentage.
  7. "Ansible task status notification on completion"

    • Code:
      --- - name: Task Status Notification hosts: your_target_hosts tasks: - name: Long Running Task command: "/path/to/long_running_script.sh" register: task_result - name: Display Status Notification debug: msg: "Task completed with status: {{ task_result.rc }}" 
    • Description: This playbook displays a notification on task completion, showing the task's exit status using the debug module.
  8. "Ansible dynamic progress display for tasks"

    • Code:
      --- - name: Dynamic Progress Display hosts: your_target_hosts tasks: - name: Long Running Task command: "/path/to/long_running_script.sh" async: 600 poll: 10 register: task_result notify: Dynamic Progress Display - name: Wait for task completion async_status: jid: "{{ task_result.ansible_job_id }}" register: job_result until: job_result.finished retries: 120 delay: 5 handlers: - name: Dynamic Progress Display ansible.builtin.debug: msg: "Task is {{ item.ansible_job_result.progress_percent }}% complete." loop: "{{ task_result.ansible_job_result | default([]) }}" 
    • Description: This playbook dynamically displays progress by using a loop in the handler to iterate through the progress percentages of the long-running task.
  9. "Ansible playbook task progress log file"

    • Code:
      --- - name: Task Progress Log hosts: your_target_hosts tasks: - name: Long Running Task command: "/path/to/long_running_script.sh" args: chdir: "/path/to/logs/" register: task_result - name: Display Progress Log ansible.builtin.command: cat "/path/to/logs/progress_log.txt" 
    • Description: This playbook logs the progress of a long-running task to a log file, and then displays the contents of the log file after the task completion.
  10. "Ansible notify on task failure or success"

    • Code:
      --- - name: Notify on Task Outcome hosts: your_target_hosts tasks: - name: Long Running Task command: "/path/to/long_running_script.sh" register: task_result ignore_errors: true - name: Notify on Failure debug: msg: "Task failed! Check logs for details." when: task_result.failed - name: Notify on Success debug: msg: "Task completed successfully." when: task_result.success 
    • Description: This playbook uses the ignore_errors parameter to run a task regardless of its success or failure, and then notifies based on the outcome using conditional debug messages.

More Tags

lytebox ef-code-first logstash-file ansi roles android-fileprovider mpvolumeview angular-ng-class ansible-template closest-points

More Programming Questions

More Auto Calculators

More Housing Building Calculators

More Fitness Calculators

More Date and Time Calculators