display: warn user about empty log path value (#83381)

Fixes: #79959

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/83562/head
Abhijeet Kasurde 3 months ago committed by GitHub
parent 6371a883d4
commit cd105d479a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,3 @@
---
bugfixes:
- display - warn user about empty log filepath (https://github.com/ansible/ansible/issues/79959).

@ -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}

@ -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,

@ -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

Loading…
Cancel
Save