diff options
-rw-r--r-- | changelogs/fragments/empty_log_path.yml | 3 | ||||
-rw-r--r-- | lib/ansible/config/base.yml | 4 | ||||
-rw-r--r-- | lib/ansible/utils/display.py | 21 | ||||
-rw-r--r-- | test/units/utils/display/test_logger.py | 16 |
4 files changed, 34 insertions, 10 deletions
diff --git a/changelogs/fragments/empty_log_path.yml b/changelogs/fragments/empty_log_path.yml new file mode 100644 index 0000000000..c8e5022bbb --- /dev/null +++ b/changelogs/fragments/empty_log_path.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - display - warn user about empty log filepath (https://github.com/ansible/ansible/issues/79959). diff --git a/lib/ansible/config/base.yml b/lib/ansible/config/base.yml index cc16c83514..edeae44f79 100644 --- a/lib/ansible/config/base.yml +++ b/lib/ansible/config/base.yml @@ -818,7 +818,9 @@ DEFAULT_LOCAL_TMP: DEFAULT_LOG_PATH: name: Ansible log file path default: ~ - description: File to which Ansible will log on the controller. When empty logging is disabled. + description: + - File to which Ansible will log on the controller. + - When not set the logging is disabled. env: [{name: ANSIBLE_LOG_PATH}] ini: - {key: log_path, section: defaults} diff --git a/lib/ansible/utils/display.py b/lib/ansible/utils/display.py index 08a5923719..7ade08070b 100644 --- a/lib/ansible/utils/display.py +++ b/lib/ansible/utils/display.py @@ -154,16 +154,19 @@ logger = None if getattr(C, 'DEFAULT_LOG_PATH'): path = C.DEFAULT_LOG_PATH if path and (os.path.exists(path) and os.access(path, os.W_OK)) or os.access(os.path.dirname(path), os.W_OK): - # NOTE: level is kept at INFO to avoid security disclosures caused by certain libraries when using DEBUG - logging.basicConfig(filename=path, level=logging.INFO, # DO NOT set to logging.DEBUG - format='%(asctime)s p=%(process)d u=%(user)s n=%(name)s | %(message)s') - - logger = logging.getLogger('ansible') - for handler in logging.root.handlers: - handler.addFilter(FilterBlackList(getattr(C, 'DEFAULT_LOG_FILTER', []))) - handler.addFilter(FilterUserInjector()) + if not os.path.isdir(path): + # NOTE: level is kept at INFO to avoid security disclosures caused by certain libraries when using DEBUG + logging.basicConfig(filename=path, level=logging.INFO, # DO NOT set to logging.DEBUG + format='%(asctime)s p=%(process)d u=%(user)s n=%(name)s | %(message)s') + + logger = logging.getLogger('ansible') + for handler in logging.root.handlers: + handler.addFilter(FilterBlackList(getattr(C, 'DEFAULT_LOG_FILTER', []))) + handler.addFilter(FilterUserInjector()) + else: + print(f"[WARNING]: DEFAULT_LOG_PATH can not be a directory '{path}', aborting", file=sys.stderr) else: - print("[WARNING]: log file at %s is not writeable and we cannot create it, aborting\n" % path, file=sys.stderr) + print(f"[WARNING]: log file at '{path}' is not writeable and we cannot create it, aborting\n", file=sys.stderr) # map color to log levels color_to_log_level = {C.COLOR_ERROR: logging.ERROR, diff --git a/test/units/utils/display/test_logger.py b/test/units/utils/display/test_logger.py index 8767affba3..b3203f8cff 100644 --- a/test/units/utils/display/test_logger.py +++ b/test/units/utils/display/test_logger.py @@ -28,3 +28,19 @@ def test_logger(): from ansible.utils.display import logger assert logger.root.level != logging.DEBUG + + +def test_empty_logger(): + # clear loaded modules to have unadultered test. + for loaded in list(sys.modules.keys()): + if 'ansible' in loaded: + del sys.modules[loaded] + + # force logger to exist via config + from ansible import constants as C + C.DEFAULT_LOG_PATH = '' + + # initialize logger + from ansible.utils.display import logger + + assert logger is None |