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

Display Ansible playbook output properly formatted

· 52CE 我爱测

What configuration is needed to correctly format the standard stream output from tasks in an Ansible ansible-playbook run? What I run ansible-playbook foo.yaml the output from tasks includes the standard stream ( stdout , stderr …

问题原文

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

What configuration is needed to correctly format the standard stream output from tasks in an Ansible ansible-playbook run?

What I run ansible-playbook foo.yaml the output from tasks includes the standard stream (stdout, stderr) content. But the display of these is in a big JSON single-line blob, and not printed as the formatted lines that were sent to the stream.

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 omitted]\"", "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"]}

What is causing this unwanted formatting of the output? How can I tell Ansible to always format the stream output correctly for display in the ansible-playbook output?

已采纳解答

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

Ansible defaults to a machine-readable JSON output, not suitable for human reading. But there are other “callback” modules available, some of which can format the stream output.

  • The misleadingly-named debug module is more suitable for human viewing.
  • Recently, the yaml module formats the stream output as a easy-to-read YAML document.

So, using the ANSIBLE_STDOUT_CALLBACK environment variable:

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

will change the formatting of stream output:

[…]
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 omitted]
  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

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

资料来源与适用范围

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

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

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