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

Ansible JSON Output

· 52CE 我爱测

Could you please help me fetch Ansible-Playbook output in a JSON Format. I do get a JSON output if I set stdout_callback variable as "json" in ansible.cfg But that output is not in realtime. The result is shown when the whole pla…

问题原文

原帖链接 · Akshay · CC BY-SA 3.0

Could you please help me fetch Ansible-Playbook output in a JSON Format. I do get a JSON output if I set stdout_callback variable as "json" in ansible.cfg

But that output is not in realtime. The result is shown when the whole playbook is executed. How can I get the output as soon as a task is executed ?

已采纳解答

原帖链接 · Konstantin Suvorov · CC BY-SA 3.0

There are definitely some problems to overcome, but nothing is impossible.

Here is something for you to play with:

Save this as ./callback_plugins/json_cb.py:

from __future__ import absolute_import
from ansible.plugins.callback import CallbackBase
import json

class CallbackModule(CallbackBase):

    CALLBACK_VERSION = 2.0
    CALLBACK_TYPE = 'stdout'
    CALLBACK_NAME = 'json_cb'

    def __init__(self):
        self.tasks = {}

    def dump_result(self, result):
        print(json.dumps(dict(name=self.tasks[result._task._uuid],result=result._result)))

    def v2_playbook_on_task_start(self, task, is_conditional):
        self.tasks[task._uuid] = task.name

    v2_runner_on_ok = dump_result
    v2_runner_on_failed = dump_result

And execute your playbook as:

ANSIBLE_STDOUT_CALLBACK=json_cb ansible-playbook myplaybook.yml

This will print JSON-object for every completed task (ok or failed).

But you are going to feed this into some other tool to parse it, aren't you? So this other tool should understand continuous stream of JSON objects.

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

资料来源与适用范围

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

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

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