我正在尝试在python代码中使用Ansible 2.0 API运行一个剧本。当我运行代码时,它会打印playbook的执行。有没有办法抑制/隐藏那些印刷品?
def run_playbook(playbook, inventory, package, var_file_path, forks):
callback = ResultsCollector()
variable_manager = VariableManager()
loader = DataLoader()
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=inventory)
playbook_path = playbook
if not os.path.exists(playbook_path):
print '[INFO] The playbook does not exist'
sys.exit()
Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax','connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_ex
tra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check'])
options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=forks, remote_user=None, private_key_file=None, ssh_common_args=None, ssh
_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=False, become_method=None, become_user=None, verbosity=5, check=False)
variable_manager.extra_vars = {'hosts': package, 'var_file': var_file_path} # This can accomodate various other command line arguments.`
passwords = {}
pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords)
results = pbex.run()
stats = pbex._tqm._stats
return stats
run_playbook(fetch_playbook, inventory_file, source_machine, fetch_var_yml,10)
答案 0 :(得分:0)
您似乎已经编写了自定义回调处理程序(ResultsCollector),但是没有看到该代码以及它如何处理结果,此解决方案可能无法满足您的请求。
在运行playbook之前,尝试将回调对象设置为playbook执行程序的stdout_callback,如下所示:
callback = ResultsCollector()
...
pbex._tqm._stdout_callback = callback
results = pbex.run()
stats = pbex._tqm._stats
return stats
答案 1 :(得分:0)
这是一个古老的问题,但我也遇到了同样的问题。 就我而言,这有助于将stdout_callback插件设置为您想要的插件
- 您自己或
- 默认
- json
- 没有
pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory,variable_manager=variable_manager, loader=loader, passwords=passwords)
...
pbex._tqm._stdout_callback = 'null'
results = pbex.run()