如何使用debug模組

debug 模組的用途,顧名思義就是除錯用的,一般用來在 playbook 裡列印變數內容,或是指定的訊息。在預設的情況下,模組只會有狀態的輸出, 像是 changed, ignored 等等的,使用 debug 模組,就可以印出變數內容或者是運算式的結果。基本上就把他想做是 bash 裡的 echo,或是 python 裡的 print。

debug 模組支援兩種基本的用法:msg, var

msg

- name: How to use debug module with msg
  hosts: all

  tasks:
  - name: Display message
    msg: "Hello world!"

用 ansible-playbook 執行以後,可以看到輸出結果

TASK [Display message **********************************************************************************************************************************
ok: [localhost] => {
  "msg": "Hello world!"
}

var

- name: How to use debug module with var
  hosts: all

  var:
    guest_name: "John"

  tasks:
  - name: Display var
    var: guest_name

用 ansible-playbook 執行以後,可以看到輸出結果

TASK [Display var **********************************************************************************************************************************
ok: [localhost] => {
  "guest_name": "John"
}

參考資料