| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2017, Tim Rightnour <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: syslogger |
| 10 |
short_description: Log messages in the syslog |
| 11 |
description: |
| 12 |
- Uses syslog to add log entries to the host. |
| 13 |
extends_documentation_fragment: |
| 14 |
- community.general._attributes |
| 15 |
attributes: |
| 16 |
check_mode: |
| 17 |
support: none |
| 18 |
diff_mode: |
| 19 |
support: none |
| 20 |
options: |
| 21 |
msg: |
| 22 |
type: str |
| 23 |
description: |
| 24 |
- This is the message to place in syslog. |
| 25 |
required: true |
| 26 |
priority: |
| 27 |
type: str |
| 28 |
description: |
| 29 |
- Set the log priority. |
| 30 |
choices: ["emerg", "alert", "crit", "err", "warning", "notice", "info", "debug"] |
| 31 |
default: "info" |
| 32 |
facility: |
| 33 |
type: str |
| 34 |
description: |
| 35 |
- Set the log facility. |
| 36 |
choices: |
| 37 |
- kern |
| 38 |
- user |
| 39 |
- mail |
| 40 |
- daemon |
| 41 |
- auth |
| 42 |
- lpr |
| 43 |
- news |
| 44 |
- uucp |
| 45 |
- cron |
| 46 |
- syslog |
| 47 |
- local0 |
| 48 |
- local1 |
| 49 |
- local2 |
| 50 |
- local3 |
| 51 |
- local4 |
| 52 |
- local5 |
| 53 |
- local6 |
| 54 |
- local7 |
| 55 |
default: "daemon" |
| 56 |
log_pid: |
| 57 |
description: |
| 58 |
- Log the PID in brackets. |
| 59 |
type: bool |
| 60 |
default: false |
| 61 |
ident: |
| 62 |
description: |
| 63 |
- Specify the name of application name which is sending the log to syslog. |
| 64 |
type: str |
| 65 |
default: 'ansible_syslogger' |
| 66 |
version_added: '0.2.0' |
| 67 |
author: |
| 68 |
- Tim Rightnour (@garbled1) |
| 69 |
""" |
| 70 |
|
| 71 |
EXAMPLES = r""" |
| 72 |
- name: Simple Usage |
| 73 |
community.general.syslogger: |
| 74 |
msg: "I will end up as daemon.info" |
| 75 |
|
| 76 |
- name: Send a log message with err priority and user facility with log_pid |
| 77 |
community.general.syslogger: |
| 78 |
msg: "Hello from Ansible" |
| 79 |
priority: "err" |
| 80 |
facility: "user" |
| 81 |
log_pid: true |
| 82 |
|
| 83 |
- name: Specify the name of application which is sending log message |
| 84 |
community.general.syslogger: |
| 85 |
ident: "MyApp" |
| 86 |
msg: "I want to believe" |
| 87 |
priority: "alert" |
| 88 |
""" |
| 89 |
|
| 90 |
RETURN = r""" |
| 91 |
ident: |
| 92 |
description: Name of application sending the message to log. |
| 93 |
returned: always |
| 94 |
type: str |
| 95 |
sample: "ansible_syslogger" |
| 96 |
version_added: '0.2.0' |
| 97 |
priority: |
| 98 |
description: Priority level. |
| 99 |
returned: always |
| 100 |
type: str |
| 101 |
sample: "daemon" |
| 102 |
facility: |
| 103 |
description: Syslog facility. |
| 104 |
returned: always |
| 105 |
type: str |
| 106 |
sample: "info" |
| 107 |
log_pid: |
| 108 |
description: Log PID status. |
| 109 |
returned: always |
| 110 |
type: bool |
| 111 |
sample: true |
| 112 |
msg: |
| 113 |
description: Message sent to syslog. |
| 114 |
returned: always |
| 115 |
type: str |
| 116 |
sample: "Hello from Ansible" |
| 117 |
""" |
| 118 |
|
| 119 |
import syslog |
| 120 |
import traceback |
| 121 |
|
| 122 |
from ansible.module_utils.basic import AnsibleModule |
| 123 |
|
| 124 |
|
| 125 |
def get_facility(facility): |
| 126 |
return { |
| 127 |
"kern": syslog.LOG_KERN, |
| 128 |
"user": syslog.LOG_USER, |
| 129 |
"mail": syslog.LOG_MAIL, |
| 130 |
"daemon": syslog.LOG_DAEMON, |
| 131 |
"auth": syslog.LOG_AUTH, |
| 132 |
"lpr": syslog.LOG_LPR, |
| 133 |
"news": syslog.LOG_NEWS, |
| 134 |
"uucp": syslog.LOG_UUCP, |
| 135 |
"cron": syslog.LOG_CRON, |
| 136 |
"syslog": syslog.LOG_SYSLOG, |
| 137 |
"local0": syslog.LOG_LOCAL0, |
| 138 |
"local1": syslog.LOG_LOCAL1, |
| 139 |
"local2": syslog.LOG_LOCAL2, |
| 140 |
"local3": syslog.LOG_LOCAL3, |
| 141 |
"local4": syslog.LOG_LOCAL4, |
| 142 |
"local5": syslog.LOG_LOCAL5, |
| 143 |
"local6": syslog.LOG_LOCAL6, |
| 144 |
"local7": syslog.LOG_LOCAL7, |
| 145 |
}.get(facility, syslog.LOG_DAEMON) |
| 146 |
|
| 147 |
|
| 148 |
def get_priority(priority): |
| 149 |
return { |
| 150 |
"emerg": syslog.LOG_EMERG, |
| 151 |
"alert": syslog.LOG_ALERT, |
| 152 |
"crit": syslog.LOG_CRIT, |
| 153 |
"err": syslog.LOG_ERR, |
| 154 |
"warning": syslog.LOG_WARNING, |
| 155 |
"notice": syslog.LOG_NOTICE, |
| 156 |
"info": syslog.LOG_INFO, |
| 157 |
"debug": syslog.LOG_DEBUG, |
| 158 |
}.get(priority, syslog.LOG_INFO) |
| 159 |
|
| 160 |
|
| 161 |
def main(): |
| 162 |
# define the available arguments/parameters that a user can pass to |
| 163 |
# the module |
| 164 |
module_args = dict( |
| 165 |
ident=dict(type="str", default="ansible_syslogger"), |
| 166 |
msg=dict(type="str", required=True), |
| 167 |
priority=dict( |
| 168 |
type="str", choices=["emerg", "alert", "crit", "err", "warning", "notice", "info", "debug"], default="info" |
| 169 |
), |
| 170 |
facility=dict( |
| 171 |
type="str", |
| 172 |
choices=[ |
| 173 |
"kern", |
| 174 |
"user", |
| 175 |
"mail", |
| 176 |
"daemon", |
| 177 |
"auth", |
| 178 |
"lpr", |
| 179 |
"news", |
| 180 |
"uucp", |
| 181 |
"cron", |
| 182 |
"syslog", |
| 183 |
"local0", |
| 184 |
"local1", |
| 185 |
"local2", |
| 186 |
"local3", |
| 187 |
"local4", |
| 188 |
"local5", |
| 189 |
"local6", |
| 190 |
"local7", |
| 191 |
], |
| 192 |
default="daemon", |
| 193 |
), |
| 194 |
log_pid=dict(type="bool", default=False), |
| 195 |
) |
| 196 |
|
| 197 |
module = AnsibleModule( |
| 198 |
argument_spec=module_args, |
| 199 |
) |
| 200 |
|
| 201 |
result = dict( |
| 202 |
changed=False, |
| 203 |
ident=module.params["ident"], |
| 204 |
priority=module.params["priority"], |
| 205 |
facility=module.params["facility"], |
| 206 |
log_pid=module.params["log_pid"], |
| 207 |
msg=module.params["msg"], |
| 208 |
) |
| 209 |
|
| 210 |
# do the logging |
| 211 |
try: |
| 212 |
syslog.openlog( |
| 213 |
module.params["ident"], |
| 214 |
syslog.LOG_PID if module.params["log_pid"] else 0, |
| 215 |
get_facility(module.params["facility"]), |
| 216 |
) |
| 217 |
syslog.syslog(get_priority(module.params["priority"]), module.params["msg"]) |
| 218 |
syslog.closelog() |
| 219 |
result["changed"] = True |
| 220 |
|
| 221 |
except Exception as exc: |
| 222 |
module.fail_json(error=f"Failed to write to syslog {exc}", exception=traceback.format_exc(), **result) |
| 223 |
|
| 224 |
module.exit_json(**result) |
| 225 |
|
| 226 |
|
| 227 |
if __name__ == "__main__": |
| 228 |
main() |