情境

情境

情境

我有一本 Ansible 劇本,其中包含一項很長的任務,長達一小時。

非常簡化,看起來像:

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

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

當使用者運行劇本時,輸出首先是:

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]

問題

劇本的最終用戶認為存在問題,Short task因為它掛在那裡,而這正是Long task導致延遲的原因。

問題

我如何配置 ansible 或 playbook 來列印中定義的標頭name: 執行任務?

我要實現的是這樣的輸出:

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]

答案1

根據OP的要求將我的評論遷移到答案


我正在使用 Ansible 2.9.2。

stdout_callback我嘗試使用沒有設定檔和沒有聲明值(預設)的設定檔。在這兩種情況下我都無法重現您的問題。

這是我的測試劇本:

---
- hosts: localhost
  gather_facts: false

  tasks:

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

    - name: LOOOOOOOOOng task
      shell: sleep 2000

以及結果(兩種情況。注意:任務標題顯示後用戶中斷)

$ 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

ansible-playbook --version仔細檢查您要在要啟動 playbook 的目錄中載入哪個設定檔。我還建議您嘗試不使用任何配置文件,看看它是否可以解決您的問題(然後查看哪個設定實際上導致了問題)。


從OP的評論中添加:事實證明,在這種特定情況下ansible.cfg中的有問題的設置是display_skipped_hosts=False

答案2

/etc/ansible/ansible.cfg 中的內容display_skipped_hosts=false為我修復了它。

這是一個錯誤嗎?我不喜歡看到跳過的主機,但確實喜歡看到我的劇本的進度。

使用 Ansible 2.10.3

答案3

對於那些仍然想隱藏跳過的任務的人,現在有兩個選項:

  1. 使用預設回調插件並且設定display_skipped_hosts = Falseshow_per_host_start = True,這將在每個任務開始時添加另一個日誌行,但是它並不能完全恢復及時輸出標題的行為(並且有時在任務已經完成後也會出現,因此使事情變得更加困難閱讀)
  2. 繼續使用(現已棄用)跳過插件在預設插件獲得跳過功能之前,這是轉到插件。它在 2.11 中仍然按預期工作,也是我對此的首選修復。stdout_callback = skippy在你的 ansible.cfg 中設定。只是為了確保我也將外掛程式列入白名單callback_enabled = ansible.posix.skippy

我的 ansible.cfg 現在看起來像這樣,一切都像以前一樣工作,我使用預設值遷移到最新的 ansible.cfg :

[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
# ...

延伸閱讀:

答案4

就我而言,我使用的是「免費」策略,在完成任務之前不會顯示標題。

相關內容