在 ansible.playbook.PlayBook 中停止冗长

Stop verbose in ansible.playbook.PlayBook

我在我的 CLi 中使用 ansible。所以所有的错误处理我都想自己做,我不想提供可能让用户迷惑的额外信息。

当我尝试在 python 代码中 运行 ansible 剧本时,我收到了详细的消息:

PLAY [Role] ***************************************************************

GATHERING FACTS ***************************************************************
ok: [127.0.0.1]

TASK: [base-devel-go | Install go lang support] *******************************
ok: [127.0.0.1]

我使用 ansible-playbook 作为我的流程来源。现在我看到了很多冗长的东西。我想隐藏它,但找不到如何隐藏它?:

playbook = params["playbook"]
inventory = ansible.inventory.Inventory(hosts)
inventory.set_playbook_basedir(os.path.dirname(playbook))

stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
runner_cb = callbacks.PlaybookRunnerCallbacks(
    stats, verbose=utils.VERBOSITY)


params["inventory"] = inventory
params["stats"] = stats
params["callbacks"] = playbook_cb
params["runner_callbacks"] = runner_cb

pb = ansible.playbook.PlayBook(**playbook_params)
pr = pb.run()

我试图在 callbacks.py 中找到答案,但我的 python 水平较低,无法找到答案

这是禁用输出的最简单方法。 我在 CLi 中使用 ansible。所以当我 运行 它认为 python 绑定 -> 有输出。但我找到了禁用它的方法:

from ansible import callbacks
# pylint: disable=too-many-arguments
def display(msg, color=None, stderr=False,
            screen_only=False, log_only=True, runner=None):
    # prevent a very rare case of interlaced multiprocess I/O
    callbacks.log_flock(runner)
    msg2 = msg
    if color:
        msg2 = stringc(msg, color)
    if not log_only:
        if not stderr:
            try:
                print msg2
            except UnicodeEncodeError:
                print msg2.encode('utf-8')
        else:
            try:
                print >>sys.stderr, msg2
            except UnicodeEncodeError:
                print >>sys.stderr, msg2.encode('utf-8')
    if callbacks.constants.DEFAULT_LOG_PATH != '':
        while msg.startswith("\n"):
            msg = msg.replace("\n", "")
        if not screen_only:
            if color == 'red':
                logging.error(msg)
            else:
                logging.info(msg)
    callbacks.log_unflock(runner)

# disable verbose
callbacks.display = display