1

I tried to setup Ansible using Python API from the following site.

https://serversforhackers.com/c/running-ansible-2-programmatically

The question at hand is as follows: How to alter verbosity programmatically in ansible. The codes below seems to not work to adjust the verbosity of the output.

# Set global verbosity
self.display = Display()
self.display.verbosity = self.options.verbosity
# Executor appears to have it's own 
# verbosity object/setting as well
playbook_executor.verbosity = self.options.verbosity

It has been pretty successful (ansible 2.3.0) thus far except verbosity settings. As the codes are in the website, I'll not post my codes here since it is exactly the same. I have tried to check the source code of ansible but it seems to be quite difficult to look through how the options are used. (i looked at display.py, playbook_executor.py, play_context.py, etc...)

note: for users who use the same code, please set the skip_tags and tags to '' for it to work in versions > 2.2.0.

johnnyow
  • 49
  • 4
  • Also, regarding *"As the codes are in the website, I'll not post my codes here since it is exactly the same."* - can you post your code anyway for the benefit of future readers, in case the site goes down? Or people who otherwise don't trust links like "serversforhackers.com"? ;) – alex Oct 12 '17 at 11:33

1 Answers1

1

Here is a really dirty hack how to apply verbosity when using Ansible API 2:

import __main__ as main
from ansible.utils.display import Display


display = Display(verbosity=4)
setattr(main, 'display', display)

Confirmed to work in Ansible v2.4.

This answer is based on https://www.programcreek.com/python/example/95763/ansible.utils.display.Display and https://stackoverflow.com/a/47639200/4533625.

Eugen
  • 1,465
  • 1
  • 13
  • 17