大网数据广告活动
服务器运维问答

Print Ansible header before executing a long running task

· 52CE 我爱测

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 th…

问题原文

原帖链接 · Phenyl · CC BY-SA 4.0

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]

已采纳解答

原帖链接 · Zeitounator · CC BY-SA 4.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-01-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。