Skip to content

Commit f72d6b5

Browse files
committed
feat: add role fingerprint to system journal
Feature: Add role fingerprint to system journal as the first task in the role. Reason: The journal is often exported to reporting systems making it easy to tell if the role is being used, and when. Result: Users can get information in their reporting and log aggregation systems of role usage.
1 parent 72ee385 commit f72d6b5

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

library/sr_fingerprint.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/python
2+
3+
from __future__ import absolute_import, division, print_function
4+
5+
__metaclass__ = type
6+
7+
DOCUMENTATION = '''
8+
---
9+
module: sr_fingerprint
10+
short_description: Write a message string to syslog
11+
description:
12+
- Writes the given string to the system log using Python's C{syslog} library.
13+
- Intended for role-internal or diagnostic use.
14+
author: Linux System Roles
15+
options:
16+
sr_message:
17+
description: Text to record in syslog.
18+
type: str
19+
required: true
20+
'''
21+
22+
EXAMPLES = '''
23+
- name: Record a fingerprint message in syslog
24+
sr_fingerprint:
25+
sr_message: "system_role:ROLENAME"
26+
'''
27+
28+
RETURN = '''
29+
changed:
30+
description: C(true) when the message was written to syslog (not in check mode).
31+
type: bool
32+
message:
33+
description: Short status text.
34+
type: str
35+
'''
36+
37+
import syslog
38+
39+
from ansible.module_utils.basic import AnsibleModule
40+
41+
42+
def run_module():
43+
module_args = dict(
44+
sr_message=dict(type='str', required=True),
45+
)
46+
47+
module = AnsibleModule(
48+
argument_spec=module_args,
49+
supports_check_mode=True,
50+
)
51+
52+
text = module.params['sr_message']
53+
54+
if module.check_mode:
55+
module.exit_json(
56+
changed=False,
57+
message='Check mode: message not written to syslog',
58+
)
59+
60+
try:
61+
syslog.openlog('ansible-sr_fingerprint', syslog.LOG_PID, syslog.LOG_USER)
62+
try:
63+
syslog.syslog(syslog.LOG_INFO, text)
64+
finally:
65+
syslog.closelog()
66+
except Exception as exc:
67+
module.fail_json(msg='Failed to write to syslog: {0}'.format(exc))
68+
69+
module.exit_json(
70+
changed=True,
71+
message='Message written to syslog',
72+
)
73+
74+
75+
def main():
76+
run_module()
77+
78+
79+
if __name__ == '__main__':
80+
main()

tasks/main.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
---
2+
- name: Record storage role fingerprint in syslog
3+
sr_fingerprint:
4+
sr_message: "system_role:storage"
5+
changed_when: false
6+
27
- name: Set platform/version specific variables
38
include_tasks: tasks/set_vars.yml
49

tests/tests_default.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,13 @@
44
tasks:
55
- name: Run the role
66
include_tasks: tasks/run_role_with_clear_facts.yml
7+
8+
# look for the exact module invocation, not some other message that might contain the string
9+
- name: Check system journal contains role fingerprint
10+
shell: >-
11+
set -eo pipefail;
12+
journalctl --since "-1 hour" --no-pager SYSLOG_IDENTIFIER=ansible-sr_fingerprint |
13+
grep "ansible-sr_fingerprint.*: system_role:storage"
14+
args:
15+
executable: /bin/bash
16+
changed_when: false

0 commit comments

Comments
 (0)