我在我的CLi中使用ansible。因此,我想自己做所有的错误处理,而且我不想提供可能使用户迷失方向的额外信息。
当我尝试在python代码中运行ansible playbook时,我收到了详细的消息:
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水平较少找到答案
答案 0 :(得分:0)
这是禁用输出的最简单方法。 我在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