feat: add role fingerprints to syslog
Feature: Add a fingerprint string to the system log to indicate when the role began successfully, and when the role finished successfully. The fingerprint string indicates the role name, a timestamp, and the platform. Reason: Users can see when the role was used and if it was used successfully. This information from the system log can be collected by log scanners and aggregators for further analysis. Result: The role logs fingerprints to the system log. This also adds a test to check if the fingerprints were written upon a successful role invocation. Signed-off-by: Rich Megginson <rmeggins@redhat.com>
This commit is contained in:
committed by
Richard Megginson
parent
19b462a7f8
commit
659d89dd69
@@ -0,0 +1 @@
|
||||
plugins/modules/sr_fingerprint.py validate-modules:missing-gplv3-license
|
||||
@@ -0,0 +1 @@
|
||||
plugins/modules/sr_fingerprint.py validate-modules:missing-gplv3-license
|
||||
@@ -0,0 +1 @@
|
||||
plugins/modules/sr_fingerprint.py validate-modules:missing-gplv3-license
|
||||
@@ -0,0 +1 @@
|
||||
plugins/modules/sr_fingerprint.py validate-modules:missing-gplv3-license
|
||||
@@ -0,0 +1 @@
|
||||
plugins/modules/sr_fingerprint.py validate-modules:missing-gplv3-license
|
||||
@@ -0,0 +1 @@
|
||||
plugins/modules/sr_fingerprint.py validate-modules:missing-gplv3-license
|
||||
@@ -0,0 +1 @@
|
||||
plugins/modules/sr_fingerprint.py validate-modules:missing-gplv3-license
|
||||
@@ -0,0 +1 @@
|
||||
plugins/modules/sr_fingerprint.py validate-modules:missing-gplv3-license
|
||||
@@ -0,0 +1,88 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
DOCUMENTATION = """
|
||||
---
|
||||
module: sr_fingerprint
|
||||
short_description: Write a message string to syslog using Ansible C(module.log) function.
|
||||
description:
|
||||
- Writes the given string to the system log using Ansible C(module.log) function.
|
||||
- Intended for role-internal or diagnostic use.
|
||||
author: Rich Megginson (@richm)
|
||||
options:
|
||||
sr_message:
|
||||
description: Text to record in syslog.
|
||||
type: str
|
||||
required: true
|
||||
"""
|
||||
|
||||
EXAMPLES = """
|
||||
- name: Record a fingerprint message in syslog
|
||||
sr_fingerprint:
|
||||
sr_message: "system_role:ROLENAME"
|
||||
"""
|
||||
|
||||
RETURN = r""" # """
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
|
||||
import datetime
|
||||
|
||||
|
||||
def _local_iso8601_no_microseconds():
|
||||
"""System local wall clock with local tz offset, ISO 8601, seconds only."""
|
||||
try:
|
||||
utc = datetime.timezone.utc
|
||||
except AttributeError:
|
||||
import time
|
||||
|
||||
return time.strftime("%Y-%m-%dT%H:%M:%S%z", time.localtime())
|
||||
# Prefer the local clock interpreted in the system timezone (not UTC displayed).
|
||||
now = datetime.datetime.now()
|
||||
astimezone = getattr(now, "astimezone", None)
|
||||
if astimezone is not None:
|
||||
try:
|
||||
return astimezone().replace(microsecond=0).isoformat()
|
||||
except (OSError, TypeError, ValueError):
|
||||
pass
|
||||
return datetime.datetime.now(utc).astimezone().replace(microsecond=0).isoformat()
|
||||
|
||||
|
||||
def run_module():
|
||||
module_args = dict(
|
||||
sr_message=dict(type="str", required=True),
|
||||
)
|
||||
|
||||
module = AnsibleModule(
|
||||
argument_spec=module_args,
|
||||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
log_message = "%s %s" % (
|
||||
module.params["sr_message"],
|
||||
_local_iso8601_no_microseconds(),
|
||||
)
|
||||
|
||||
if module.check_mode:
|
||||
module.exit_json(
|
||||
changed=False,
|
||||
message="Check mode: message not logged - [%s]" % log_message,
|
||||
)
|
||||
|
||||
module.log(log_message)
|
||||
|
||||
# we don't actually change anything, so we're not changed - writing a log message
|
||||
# is not considered a change
|
||||
# also, we don't want to report changed every time the role runs
|
||||
module.exit_json(changed=False)
|
||||
|
||||
|
||||
def main():
|
||||
run_module()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -138,3 +138,9 @@
|
||||
when:
|
||||
- aide_cron_check is not none
|
||||
- not aide_cron_check | bool
|
||||
|
||||
- name: Record role success fingerprint
|
||||
sr_fingerprint:
|
||||
sr_message: >-
|
||||
success system_role:aide ansible_version={{ ansible_version.full }}
|
||||
{{ ansible_facts['distribution'] }}-{{ ansible_facts['distribution_version'] }}
|
||||
|
||||
@@ -5,6 +5,12 @@
|
||||
when: __aide_required_facts |
|
||||
difference(ansible_facts.keys() | list) | length > 0
|
||||
|
||||
- name: Record role begin fingerprint
|
||||
sr_fingerprint:
|
||||
sr_message: >-
|
||||
begin system_role:aide ansible_version={{ ansible_version.full }}
|
||||
{{ ansible_facts['distribution'] }}-{{ ansible_facts['distribution_version'] }}
|
||||
|
||||
- name: Determine if system is ostree and set flag
|
||||
when: not __aide_is_ostree is defined
|
||||
block:
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
../../../library
|
||||
@@ -3,8 +3,25 @@
|
||||
- name: Ensure that the role runs with default parameters
|
||||
hosts: all
|
||||
tasks:
|
||||
- name: Set the start time for the journal search
|
||||
set_fact:
|
||||
__journal_start_time: "{{ ansible_facts['date_time']['date'] ~ ' ' ~ ansible_facts['date_time']['time'] }}"
|
||||
|
||||
- name: Run the role
|
||||
include_tasks: tasks/run_role_with_clear_facts.yml
|
||||
|
||||
# look for the exact module invocation, not some other message that might contain the string
|
||||
- name: Check system journal contains role fingerprints
|
||||
shell: >-
|
||||
set -eo pipefail;
|
||||
journalctl --since "{{ __journal_start_time }}" --no-pager |
|
||||
grep -v " Invoked with" | grep "sr_fingerprint.*begin system_role:aide" ||
|
||||
{ echo ERROR: BEGIN fingerprint not found; exit 1; };
|
||||
journalctl --since "{{ __journal_start_time }}" --no-pager |
|
||||
grep -v " Invoked with" | grep "sr_fingerprint.*success system_role:aide" ||
|
||||
{ echo ERROR: SUCCESS fingerprint not found; exit 1; }
|
||||
changed_when: false
|
||||
|
||||
- name: Check if the file exists
|
||||
ansible.builtin.stat:
|
||||
path: /etc/aide.conf
|
||||
|
||||
Reference in New Issue
Block a user