通用 Ansible 运行的回调? [英] Callbacks for generic Ansible runs?

查看:33
本文介绍了通用 Ansible 运行的回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了一个回调:

I have defined a callback like so:

#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-

import sys
import pprint

from ansible.plugins.callback import CallbackBase


class JSONPrettyPrintCallback(CallbackBase):

    printer = pprint.PrettyPrinter(indent=4, stream=sys.stdout)

    def log(self, host, category, data):
        # one json blob to rule them all
        self.printer.pprint({'host': host, 'category': category, 'data': data})

在我的 Ansible 配置中,我定义了路径:

In my Ansible config, I have the path defined:

[defaults]
callback_plugins = callback_plugins/

然而,当我运行我的模块时,我仍然看到默认的 Ansible 输出:

However, when i run my module, I still see the default Ansible output:

10.0.0.1 | SUCCESS => {
    ...
}

我是这样运行的:

ansible all -u myuser -m script -a 'path/to/script.py'

我需要做些什么来正确格式化我的输出吗?

Is there something I need to do to format my output properly?

推荐答案

如果你想替换 Ansible 标准输出,你应该设置 stdout_callback 参数给您的回调.

If you want to replace Ansible standard output, you should set stdout_callback parameter to your callback.

更新: stdout_callback 参数将仅影响 ansible-playbook CLI 输出,如注释中所述.

Update: stdout_callback parameter will affect only ansible-playbook CLI output, as correctly noted in the comments.

我调查了 代码 一点,ansible CLI 仅支持 minimaloneline(如果 -o选项已设置)标准输出回调.
更改输出行为的最简单方法是将 ./callback_plugins/minimal.py 放在您的 playbook 文件夹中,并根据您的需要对其进行修改.
或者您可以修补 adhoc.py 以接受其他标准输出插件.

I've investigated the code a bit, ansible CLI supports only minimal and oneline (if -o option is set) stdout callbacks.
The easiest way to change the output behavior is to place ./callback_plugins/minimal.py in your playbook folder and modify it to your needs.
Or you can patch adhoc.py to be able to accept other stdout plugins.

这篇关于通用 Ansible 运行的回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆