Reputation: 329
I am trying to run a playbook using Ansible 2.0 API inside python code. When I run the code, it prints the execution of playbook. Is there any way to suppress/hide those prints?
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)
Upvotes: 0
Views: 1452
Reputation: 430
This is an old question but I've got the same problem. In my case it helped to set a stdout_callback plugin to the one you wish
- your own or
- default
- json
- none
pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory,variable_manager=variable_manager, loader=loader, passwords=passwords)
...
pbex._tqm._stdout_callback = 'null'
results = pbex.run()
hint came from: https://kurisu.love/index.php/archives/137/
Upvotes: 0
Reputation: 73
It looks like you've written a custom callback handler (ResultsCollector), but without seeing that code and how it handles results this solution may not satisfy what you're requesting.
Try setting your callback object as the stdout_callback for the playbook executor before running the playbook, like so:
callback = ResultsCollector()
...
pbex._tqm._stdout_callback = callback
results = pbex.run()
stats = pbex._tqm._stats
return stats
Upvotes: 0