6

Context

I have an Ansible playbook that includes a very long task, up to one hour.

Very simplified, it looks like:

- hosts: localhost
  tasks:
  - name: Short task
    debug:
      msg: "I'm quick!"

  - name: Long task
    shell: "sleep 15s"

When the user runs the playbook, the output is at first:

PLAY [localhost] ********************
TASK [Gathering Facts] **************
ok: [127.0.0.1]

TASK [Short task] *******************
ok: [127.0.0.1] => {
    "msg": "I'm quick!"
}

(hang there until Long task is done)

TASK [Long task] ********************
changed: [127.0.0.1]

Problem

The end users of the playbook believe that there is a problem with Short task since it hangs there, while it's Long task that is causing the delay.

Question

How could I configure ansible or the playbook to print the headers defined in name: before excuting the task?

What I what to achieve is an output like:

PLAY [localhost] ********************
TASK [Gathering Facts] **************
ok: [127.0.0.1]

TASK [Short task] *******************
ok: [127.0.0.1] => {
    "msg": "I'm quick!"
}

TASK [Long task] ********************

(and hang there during the execution)

changed: [127.0.0.1]
5
  • 1
    How are you launching your playbook ? Because what you expect is actually how it is supposed to behave when launching in a terminal at least. Jan 28, 2020 at 19:40
  • I use ansible-playbook from the terminal with ansible-2.9.2. I have the option stdout_callback=skippy in my ansible.cfg. I tried with stdout_callback=default without success. I observed that ansible prints the header first in ad-hoc mode but not in playbook mode.
    – Phenyl
    Jan 29, 2020 at 10:18
  • Ansible 2.9.2. I tried with no config file and with a config file with no value for stdout_callback (default). I cannot reproduce your problem in either case. Double check you are not loading a config file with ansible-playbook --version in the directory where you are going to launch the playbook. Jan 29, 2020 at 11:19
  • 1
    Thanks to your hint, I found that the behavior comes from another option in my custom ansible.cfg. Setting display_skipped_hosts=False is the root cause of my problem.
    – Phenyl
    Jan 29, 2020 at 12:57
  • @Zeitounator, do you want to submit an answer?
    – Phenyl
    Jan 29, 2020 at 13:42

5 Answers 5

3

Migrating my comment to an answer upon OP's request


I am using Ansible 2.9.2.

I tried with no config file and with a config file with no value declared for stdout_callback (default). I cannot reproduce your problem in either case.

This is my test playbook:

---
- hosts: localhost
  gather_facts: false

  tasks:

    - name: Short running
      debug:
        msg: I'm a short task

    - name: LOOOOOOOOOng task
      shell: sleep 2000

And the result (in both cases. Note: interruption by user after task header display)

$ ansible-playbook /tmp/play.yml  

PLAY [localhost] **************************************************************************************************************************************************************************************************

TASK [Short running] **********************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "I'm a short task"
}

TASK [LOOOOOOOOOng task] ******************************************************************************************************************************************************************************************
^C [ERROR]: User interrupted execution

Double check which config file you are loading with ansible-playbook --version in the directory where you are going to launch the playbook. I also suggest you try without any config file to see if it fixes your issue (and then see which setting is actually causing the issue).


Added from OP's comment: it turns out the problematic setting in ansible.cfg in this specific case was display_skipped_hosts=False

3

The display_skipped_hosts=false in /etc/ansible/ansible.cfg fixed it for me.

Is it a bug? I don't like to see the skipped hosts but do like to see the progress of my playbook.

Using Ansible 2.10.3

2
  • Thank you for the answer. Indeed, the cause was the display_skipped_hosts setting as shown in the accepted answer. To your question, this is not a bug but something inherent to how ansible is dealing with the output.
    – Phenyl
    Mar 9, 2021 at 20:43
  • In short: users think it's a bug, Ansible devs don't ;) See github.com/ansible/ansible/issues/51042 and github.com/ansible/ansible/issues/52089 and a bunch of other related issues. Solution: use (deprecated) skippy plugin, see my answer below Sep 27, 2021 at 11:50
1

a simple approach to this is to print a message informing the user that a long task is going to be executed and then start the task execution.

2
  • I agree that a valid approach is to put a debug: msg= task before long running tasks but it becomes tedious when there are multiple of them. Let's say this is a workaround :).
    – Phenyl
    Jan 29, 2020 at 10:21
  • 1
    The usual ansible way to do this is.... to give an informational name to your task. Else you end up with a cluttered playbook full of debugs saying"I'm going to do A - Now I'm doing A". This definitely looks like a bug, at least a sensible feature to add. Obfuscating skipped hosts should not buffer the task header Mar 26, 2021 at 7:35
1

For those who still want to hide skipped tasks there is now two options:

  1. Using the default callback plugin and setting display_skipped_hosts = False and show_per_host_start = True, this will add another log line when each task is started, however it doesn't get quite the behaviour back of just outputting the title in time (and somehow sometimes also appears after a task is already doen, hence making things harder to read)
  2. Keep using the (now deprecated) skippy plugin which was the go to plugin before the default plugin got the feature for skipping. It still works as expected with 2.11 and is also my preferred fix to this. Set stdout_callback = skippy in your ansible.cfg. Just to make sure I also whitelisted the plugin with callback_enabled = ansible.posix.skippy

My ansible.cfg looks like this now and all works as before I migrated to latest ansible.cfg with defaults:

[default]
# ...
stdout_callback = skippy
# if you had other plugins here just add to list, comma seperated
callback_enabled = ansible.posix.skippy
# deprecated flag, sometimes still needed, hence I always set both
callback_whitelist = ansible.posix.skippy
# ...

Further reading:

0

In my case, I was using the "free" strategy which would not show the header before finishing with the task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.