Python API with log_path default set redirects unrelated logs

126 views
Skip to first unread message

Jason Pettett

unread,
Jan 16, 2015, 12:26:41 AM1/16/15
to ansible...@googlegroups.com
Hello all,

I'm running some Ansible commands from within a flask application via the Python API

Ansible version: 1.8.2
Python version: 2.7

In my home directory, I have .ansible.cfg with the following contents:

[defaults]
host_key_checking = False
log_path=/tmp/ansible.log


The issue I'm having is that as soon as the runner is imported (see attached stack trace later in this post), the log_path defined above overrides the current logging location (in this case, stdout).

I've tracked it down to the following line:

logging.basicConfig(filename=path, level=logging.DEBUG, format='%(asctime)s %(name)s %(message)s') 

Line 40 in site-packages/ansible/callbacks.py

Note that it also changes the format and minimum logging level which is problematic. 


Is there currently an undocumented/unofficial way to retain my default log_path, while avoiding this issue (when using the Python API)?

Traceback of where it is imported and modified:

 File "/home/dev3/projects/tcg-cas/app/util.py", line 5, in <module>
    from ansible.runner import Runner
  File "/home/dev3/projects/tcg-cas/local/lib/python2.7/site-packages/ansible/runner/__init__.py", line 37, in <module>
    import ansible.inventory
  File "/home/dev3/projects/tcg-cas/local/lib/python2.7/site-packages/ansible/inventory/__init__.py", line 26, in <module>
    from ansible.inventory.ini import InventoryParser
  File "/home/dev3/projects/tcg-cas/local/lib/python2.7/site-packages/ansible/inventory/ini.py", line 21, in <module>
    from ansible.inventory.host import Host
  File "/home/dev3/projects/tcg-cas/local/lib/python2.7/site-packages/ansible/inventory/host.py", line 19, in <module>
    from ansible import utils
  File "/home/dev3/projects/tcg-cas/local/lib/python2.7/site-packages/ansible/utils/__init__.py", line 29, in <module>
    from ansible.utils.display_functions import *
  File "/home/dev3/projects/tcg-cas/local/lib/python2.7/site-packages/ansible/utils/display_functions.py", line 22, in <module>
    from ansible.callbacks import display
  File "/home/dev3/projects/tcg-cas/local/lib/python2.7/site-packages/ansible/callbacks.py", line 33, in <module>
    raise Exception()

The original line where I placed the exception was as follows:
logging.basicConfig(filename=path, level=logging.DEBUG, format='%(asctime)s %(name)s %(message)s')


If there's currently no way around this, does anyone with more knowledge of the Ansible ecosystem have any ideas of how this could be fixed from within Ansible?

For the moment, my solution is to overwrite the constant DEFAULT_LOG_PATH just before the first import of the Ansible runner like so:

from ansible import constants
constants.DEFAULT_LOG_PATH = ''
from ansible.runner import Runner

The offending code will be skipped due to the surrounding conditional, but this is obviously horribly fragile and way less than ideal.

Anyone have any thoughts on this?

Thanks!
Reply all
Reply to author
Forward
0 new messages