我需要每个主机(服务器)的 ansible 日志,因此我在 ansible.cfg 中使用 logs_play 模块。
callback_whitelist = log_plays
但输出并不人性化。请参阅下面的示例日志:
Apr 10 2020 12:23:35 - OK - omitted
Apr 10 2020 12:23:46 - OK - {"results": [{"msg": "", "invocation": {"module_args": {"lock_timeout": 30, "update_cache": false, "disable_excludes": null, "exclude": [], "allow_downgrade": false, "disable_gpg_check": false, "conf_file": null, "use_backend": "auto", "state": "installed", "disablerepo": [], "releasever": null, "skip_broken": false, "autoremove": false, "download_dir": null, "enable_plugin": [], "installroot": "/", "install_weak_deps": true, "name": ["vim", "git"], "download_only": false, "bugfix": false, "list": null, "install_repoquery": true, "update_only": false, "disable_plugin": [], "enablerepo": [], "security": false, "validate_certs": true}}, "changed": false, "results": ["2:vim-enhanced-7.4.629-6.el7.x86_64 providing vim is already installed", "git-1.8.3.1-21.el7_7.x86_64 providing git is already installed"], "rc": 0, "_ansible_no_log": false, "failed": false, "item": ["vim", "git"], "ansible_loop_var": "item", "_ansible_item_label": ["vim", "git"]}], "msg": "All items completed", "changed": false}
Apr 10 2020 12:23:47 - SKIPPED - ...
您会看到第二行大文本显示为一行,并且不便于阅读。如何使逐行输出更美观?
我知道有一个设置可以更改屏幕输出格式,例如 stdout_callback。但是如何对 /var/log/ansible/hosts 位置中的日志执行相同操作?
答案1
我们在以下应用中使用它ansible.cfg
:
stdout_callback = yaml
这提供了更易读的 YAML 输出,而不是 JSON。如果这不适合您,还有许多其他回调插件。请参阅https://docs.ansible.com/ansible/latest/plugins/callback.html