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

查看:201
本文介绍了通用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标准输出,则应设置

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.

我已经调查了

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天全站免登陆