以JSON,CSV或其他格式获取Ansible临时命令输出的方法是什么?
如果您不想修改.cfg
文件,也可以通过环境变量执行此操作,例如:
ANSIBLE_LOAD_CALLBACK_PLUGINS=true
ANSIBLE_STDOUT_CALLBACK=json
ansible all
-a "df -h /tmp"
有关 Ansible 环境变量的更多信息,请参阅: https://docs.ansible.com/ansible/latest/reference_appendices/config.html#environment-variables。
在ansible.cfg
中添加:
[defaults]
stdout_callback = json
请参阅文档
取而代之的是:
ok: [localhost] => {
"msg": "test"
}
您将拥有:
{
"plays": [
{
"play": {
"id": "720000f8-9450-586c-9a68-000000000005",
"name": "Json Test"
},
"tasks": [
{
"hosts": {
"localhost": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"changed": false,
"msg": "test"
}
},
"task": {
"id": "720000f8-9450-586c-9a68-000000000007",
"name": "Debug"
}
}
]
}
],
"stats": {
"localhost": {
"changed": 0,
"failures": 0,
"ok": 1,
"skipped": 0,
"unreachable": 0
}
}
}
对于以下行动手册:
---
- name: Json Test
hosts: localhost
gather_facts: False
vars:
test: test
tasks:
- name: Debug
debug:
msg: "{{ test }}"
至少需要使用 Ansible 2.5
然后在您的 Ansible 配置中设置此项:
stdout_callback = json
bin_ansible_callbacks = True
关于 ansible 配置的快速说明(投诉?配置文件不是累加的。 如果你有多个配置文件(例如/etc/ansible/ansible.cfg 和 ~/.ansible.cfg),它只会从 ~/.ansible 中获取值。
这是配置文件顺序:
https://docs.ansible.com/ansible/latest/reference_appendices/config.html#the-configuration-file
这是错误:
https://github.com/ansible/ansible/issues/17914
另外,这是完整的回调插件列表:
https://docs.ansible.com/ansible/2.6/plugins/callback.html#plugin-list