Python API with log_path default set redirects unrelated logs

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

from ansible.runner import Runner

File “/home/dev3/projects/tcg-cas/local/lib/python2.7/site-packages/ansible/runner/init.py”, line 37, in

import ansible.inventory

File “/home/dev3/projects/tcg-cas/local/lib/python2.7/site-packages/ansible/inventory/init.py”, line 26, in

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

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

from ansible import utils

File “/home/dev3/projects/tcg-cas/local/lib/python2.7/site-packages/ansible/utils/init.py”, line 29, in

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

from ansible.callbacks import display

File “/home/dev3/projects/tcg-cas/local/lib/python2.7/site-packages/ansible/callbacks.py”, line 33, in

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!