顯示格式正確的 Ansible playbook 輸出

顯示格式正確的 Ansible playbook 輸出

需要什麼設定才能正確格式化 Ansibleansible-playbook運作中任務的標準流輸出?

我運行ansible-playbook foo.yaml任務的輸出包括標準流 ( stdout, stderr) 內容。但這些顯示在一個大的 JSON 單行 blob 中,而不是列印為傳送到流的格式化行。

TASK [Django: Collect media fixture files] ******************************************************************************
ok: [lorem]

TASK [Django: Create superuser] ******************************************************************************
fatal: [lorem]: FAILED! => {"changed": false, "cmd": "python3 -m django createsuperuser\n  --noinput\n  --username \"admin\"\n  --email \"[email protected]\"", "msg": "\n:stderr: CommandError: You must use --full_name with --noinput.\n", "path": "/var/local/dolor/virtualenv/rectory/venv.py3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/games", "syspath": ["/tmp/ansible_django_manage_payload_uj9f3le8/ansible_django_manage_payload.zip", "/usr/lib/python37.zip", "/usr/lib/python3.7", "/usr/lib/python3.7/lib-dynload", "/usr/local/lib/python3.7/dist-packages", "/usr/lib/python3/dist-packages"]}

是什麼導致了這種不想要的輸出格式?我如何告訴 Ansible 始終正確格式化流輸出以供顯示ansible-playbook輸出中?

答案1

Ansible 預設為機器可讀的 JSON 輸出,不適合人類閱讀。但還有其他可用的“回調”模組,其中一些可以格式化流輸出。

  • 具有誤導性的名稱debug模組更適合人類觀看。
  • 最近,yaml模組將流輸出格式化為易於閱讀的 YAML 文件。

因此,使用ANSIBLE_STDOUT_CALLBACK環境變數:

$ ANSIBLE_STDOUT_CALLBACK=yaml ansible-playbook ansible/deploy.yaml

將更改流輸出的格式:

[…]
TASK [Django: Collect media fixture files] ******************************************************************************
ok: [lorem]

TASK [Django: Create superuser] ******************************************************************************
fatal: [lorem]: FAILED! => changed=false
  cmd: |-
    python3 -m django createsuperuser
      --noinput
      --username "admin"
      --email "[email protected]
  msg: |-
    stderr: |-
      CommandError: You must use --full_name with --noinput.
    path: "/var/local/dolor/virtualenv/rectory/venv.py3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/games"
    syspath: 
    - /tmp/ansible_django_manage_payload_uj9f3le8/ansible_django_manage_payload.zip
    - /usr/lib/python37.zip
    - /usr/lib/python3.7
    - /usr/lib/python3.7/lib-dynload
    - /usr/local/lib/python3.7/dist-packages
    - /usr/lib/python3/dist-packages

答案2

1.建立設定檔

若要獲得正確格式化為漂亮 JSON 的 Ansible playbook 輸出,請建立一個設定檔:

  • ansible.cfgAnsible playbook 檔案位於同一目錄中
  • 或在主目錄中,以點開頭,~/.ansible.cfg
  • 或在/etc/ansible/ansible.cfg

來源:https://docs.ansible.com/ansible/latest/reference_appendices/config.html

獎金: debug也給漂亮標準輸出,標準錯誤味精

文件內容:

[defaults]
stdout_callback=debug

2. 運行劇本

以與以前完全相同的方式運行劇本。

ansible-playbook foo.yaml

比較

前:

TASK [Django: Collect media fixture files] ******************************************************************************
ok: [lorem]

TASK [Django: Create superuser] ******************************************************************************
fatal: [lorem]: FAILED! => {"changed": false, "cmd": "python3 -m django createsuperuser\n  --noinput\n  --username \"admin\"\n  --email \"[email protected]\"", "msg": "\n:stderr: CommandError: You must use --full_name with --noinput.\n", "path": "/var/local/dolor/virtualenv/rectory/venv.py3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/games", "syspath": ["/tmp/ansible_django_manage_payload_uj9f3le8/ansible_django_manage_payload.zip", "/usr/lib/python37.zip", "/usr/lib/python3.7", "/usr/lib/python3.7/lib-dynload", "/usr/local/lib/python3.7/dist-packages", "/usr/lib/python3/dist-packages"]}

後:

TASK [Django: Collect media fixture files] ******************************************************************************
ok: [lorem]

TASK [Django: Create superuser] ******************************************************************************
fatal: [lorem]: FAILED! => {
  "changed": false,
  "cmd": "python3 -m django createsuperuser\n  --noinput\n  --username \"admin\"\n  --email \"[email protected]\"",
  "msg": "\n:stderr: CommandError: You must use --full_name with --noinput.\n",
  "path": "/var/local/dolor/virtualenv/rectory/venv.py3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/games",
  "syspath": [
    "/tmp/ansible_django_manage_payload_uj9f3le8/ansible_django_manage_payload.zip",
    "/usr/lib/python37.zip",
    "/usr/lib/python3.7",
    "/usr/lib/python3.7/lib-dynload",
    "/usr/local/lib/python3.7/dist-packages",
    "/usr/lib/python3/dist-packages"
  ]
}

STDERR:

CommandError: You must use --full_name with --noinput.



MSG:

CommandError: You must use --full_name with --noinput.

相關內容