This commit is contained in:
Andrew Sayers 2026-05-27 22:07:28 -05:00 committed by GitHub
commit ce15694b2e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 27 additions and 2 deletions

View file

@ -0,0 +1,6 @@
breaking_changes:
- >-
Ansible's systemd logs now contain a PRIORITY field (default value: 5).
Downstream programs that inspect Ansible's systemd logs should be updated
if they expect the PRIORITY field to be missing
(https://github.com/ansible/ansible/pull/84574).

View file

@ -2032,6 +2032,14 @@ TARGET_LOG_INFO:
vars:
- name: ansible_target_log_info
version_added: "2.17"
TARGET_LOG_SEVERITY:
name: logging severity
default: LOG_INFO
description: Default syslog-style log level to use when Ansible logs to the remote target.
env: [{name: ANSIBLE_TARGET_LOG_SEVERITY}]
ini:
- {key: target_log_severity, section: defaults}
TASK_TIMEOUT:
name: Task Timeout
default: 0

View file

@ -395,6 +395,7 @@ class AnsibleModule(object):
self._socket_path = None
self._shell = None
self._syslog_facility = 'LOG_USER'
self._target_log_severity = 'LOG_INFO'
self._verbosity = 0
# May be used to set modifications to the environment for any
# run_command invocation
@ -1255,8 +1256,9 @@ class AnsibleModule(object):
try:
module = 'ansible-%s' % self._name
facility = getattr(syslog, self._syslog_facility, syslog.LOG_USER)
level = getattr(syslog, self._target_log_severity, syslog.LOG_INFO)
syslog.openlog(str(module), 0, facility)
syslog.syslog(syslog.LOG_INFO, msg)
syslog.syslog(level, msg)
except (TypeError, ValueError) as e:
self.fail_json(
msg='Failed to log to syslog (%s). To proceed anyway, '
@ -1311,6 +1313,8 @@ class AnsibleModule(object):
name = "_%s" % name
journal_args.append((name, value))
priority = getattr(syslog, self._target_log_severity, syslog.LOG_INFO)
try:
if HAS_SYSLOG:
# If syslog_facility specified, it needs to convert
@ -1321,9 +1325,11 @@ class AnsibleModule(object):
syslog.LOG_USER) >> 3
journal.send(MESSAGE=u"%s %s" % (module, journal_msg),
SYSLOG_FACILITY=facility,
PRIORITY=priority,
**dict(journal_args))
else:
journal.send(MESSAGE=u"%s %s" % (module, journal_msg),
PRIORITY=priority,
**dict(journal_args))
except OSError:
# fall back to syslog since logging to journal failed

View file

@ -87,10 +87,11 @@ PASS_VARS: dict[str, t.Any] = {
'no_log': ('no_log', False),
'remote_tmp': ('_remote_tmp', None),
'target_log_info': ('_target_log_info', None),
'target_log_severity': ('_target_log_severity', 'LOG_INFO'),
'selinux_special_fs': ('_selinux_special_fs', ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']),
'shell_executable': ('_shell', '/bin/sh'),
'socket': ('_socket_path', None),
'syslog_facility': ('_syslog_facility', 'INFO'),
'syslog_facility': ('_syslog_facility', 'LOG_USER'),
'tmpdir': ('_tmpdir', None),
'tracebacks_for': ('_tracebacks_for', frozenset()),
'verbosity': ('_verbosity', 0),

View file

@ -77,6 +77,7 @@ namespace Ansible.Basic
{ "socket", null },
{ "syslog_facility", null },
{ "target_log_info", "TargetLogInfo" },
{ "target_log_severity", null },
{ "tracebacks_for", "_tracebacksFor" },
{ "tmpdir", "tmpdir" },
{ "verbosity", "Verbosity" },

View file

@ -1085,6 +1085,9 @@ class ActionBase(ABC, _AnsiblePluginInfoMixin):
# allow user to insert string to add context to remote logging
module_args['_ansible_target_log_info'] = C.config.get_config_value('TARGET_LOG_INFO', variables=task_vars)
# set the syslog-style severity to be used in the module
module_args['_ansible_target_log_severity'] = C.config.get_config_value('TARGET_LOG_SEVERITY', variables=task_vars)
module_args['_ansible_tracebacks_for'] = _traceback.traceback_for()
module_args['_ansible_inject_invocation'] = C.config.get_config_value('INJECT_INVOCATION', variables=task_vars)