![以正确的格式显示 Ansible playbook 输出](https://linux22.com/image/756673/%E4%BB%A5%E6%AD%A3%E7%A1%AE%E7%9A%84%E6%A0%BC%E5%BC%8F%E6%98%BE%E7%A4%BA%20Ansible%20playbook%20%E8%BE%93%E5%87%BA.png)
需要什么配置才能正确格式化 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,不适合人类阅读。但还有其他可用的“回调”模块,其中一些可以格式化流输出。
因此,使用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. 创建配置文件
为了使 Ansible playbook 输出格式化为漂亮的 JSON,请创建一个配置文件:
- 或者
ansible.cfg
,与 Ansible 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.