2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
2021-08-08 11:48:54 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2020-05-15 11:05:32 +02:00
|
|
|
# Copyright: (c) 2017, Tim Rightnour <thegarbledone@gmail.com>
|
2020-03-09 10:11:07 +01:00
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2021-01-02 20:40:27 +01:00
|
|
|
DOCUMENTATION = r'''
|
2020-03-09 10:11:07 +01:00
|
|
|
---
|
|
|
|
module: syslogger
|
|
|
|
short_description: Log messages in the syslog
|
|
|
|
description:
|
2020-05-15 11:05:32 +02:00
|
|
|
- Uses syslog to add log entries to the host.
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
msg:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2020-05-15 11:05:32 +02:00
|
|
|
- This is the message to place in syslog.
|
|
|
|
required: True
|
2020-03-09 10:11:07 +01:00
|
|
|
priority:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2020-05-15 11:05:32 +02:00
|
|
|
- Set the log priority.
|
2020-03-09 10:11:07 +01:00
|
|
|
choices: [ "emerg", "alert", "crit", "err", "warning", "notice", "info", "debug" ]
|
|
|
|
default: "info"
|
|
|
|
facility:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2020-05-15 11:05:32 +02:00
|
|
|
- Set the log facility.
|
2020-03-09 10:11:07 +01:00
|
|
|
choices: [ "kern", "user", "mail", "daemon", "auth", "lpr", "news",
|
|
|
|
"uucp", "cron", "syslog", "local0", "local1", "local2",
|
|
|
|
"local3", "local4", "local5", "local6", "local7" ]
|
|
|
|
default: "daemon"
|
|
|
|
log_pid:
|
|
|
|
description:
|
2021-01-02 20:40:27 +01:00
|
|
|
- Log the PID in brackets.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
2020-05-15 11:05:32 +02:00
|
|
|
default: False
|
|
|
|
ident:
|
|
|
|
description:
|
|
|
|
- Specify the name of application name which is sending the log to syslog.
|
|
|
|
type: str
|
|
|
|
default: 'ansible_syslogger'
|
2020-06-13 15:01:19 +02:00
|
|
|
version_added: '0.2.0'
|
2020-03-09 10:11:07 +01:00
|
|
|
author:
|
|
|
|
- Tim Rightnour (@garbled1)
|
|
|
|
'''
|
|
|
|
|
2020-05-15 11:05:32 +02:00
|
|
|
EXAMPLES = r'''
|
|
|
|
- name: Simple Usage
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.syslogger:
|
2020-05-15 11:05:32 +02:00
|
|
|
msg: "I will end up as daemon.info"
|
|
|
|
|
|
|
|
- name: Send a log message with err priority and user facility with log_pid
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.syslogger:
|
2020-05-15 11:05:32 +02:00
|
|
|
msg: "Hello from Ansible"
|
2020-03-09 10:11:07 +01:00
|
|
|
priority: "err"
|
2020-05-15 11:05:32 +02:00
|
|
|
facility: "user"
|
2020-03-09 10:11:07 +01:00
|
|
|
log_pid: true
|
|
|
|
|
2020-05-15 11:05:32 +02:00
|
|
|
- name: Specify the name of application which is sending log message
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.syslogger:
|
2020-05-15 11:05:32 +02:00
|
|
|
ident: "MyApp"
|
|
|
|
msg: "I want to believe"
|
|
|
|
priority: "alert"
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
2020-05-15 11:05:32 +02:00
|
|
|
RETURN = r'''
|
|
|
|
ident:
|
|
|
|
description: Name of application sending the message to log
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: "ansible_syslogger"
|
2020-06-13 15:01:19 +02:00
|
|
|
version_added: '0.2.0'
|
2020-05-15 11:05:32 +02:00
|
|
|
priority:
|
|
|
|
description: Priority level
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: "daemon"
|
|
|
|
facility:
|
|
|
|
description: Syslog facility
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: "info"
|
|
|
|
log_pid:
|
2021-01-02 20:40:27 +01:00
|
|
|
description: Log PID status
|
2020-05-15 11:05:32 +02:00
|
|
|
returned: always
|
|
|
|
type: bool
|
|
|
|
sample: True
|
|
|
|
msg:
|
|
|
|
description: Message sent to syslog
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: "Hello from Ansible"
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
import syslog
|
2021-01-02 20:40:27 +01:00
|
|
|
import traceback
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2021-06-27 09:39:18 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
2021-01-02 20:40:27 +01:00
|
|
|
def get_facility(facility):
|
2020-03-09 10:11:07 +01:00
|
|
|
return {
|
|
|
|
'kern': syslog.LOG_KERN,
|
|
|
|
'user': syslog.LOG_USER,
|
|
|
|
'mail': syslog.LOG_MAIL,
|
|
|
|
'daemon': syslog.LOG_DAEMON,
|
|
|
|
'auth': syslog.LOG_AUTH,
|
|
|
|
'lpr': syslog.LOG_LPR,
|
|
|
|
'news': syslog.LOG_NEWS,
|
|
|
|
'uucp': syslog.LOG_UUCP,
|
|
|
|
'cron': syslog.LOG_CRON,
|
|
|
|
'syslog': syslog.LOG_SYSLOG,
|
|
|
|
'local0': syslog.LOG_LOCAL0,
|
|
|
|
'local1': syslog.LOG_LOCAL1,
|
|
|
|
'local2': syslog.LOG_LOCAL2,
|
|
|
|
'local3': syslog.LOG_LOCAL3,
|
|
|
|
'local4': syslog.LOG_LOCAL4,
|
|
|
|
'local5': syslog.LOG_LOCAL5,
|
|
|
|
'local6': syslog.LOG_LOCAL6,
|
|
|
|
'local7': syslog.LOG_LOCAL7
|
2021-01-02 20:40:27 +01:00
|
|
|
}.get(facility, syslog.LOG_DAEMON)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
2021-01-02 20:40:27 +01:00
|
|
|
def get_priority(priority):
|
2020-03-09 10:11:07 +01:00
|
|
|
return {
|
|
|
|
'emerg': syslog.LOG_EMERG,
|
|
|
|
'alert': syslog.LOG_ALERT,
|
|
|
|
'crit': syslog.LOG_CRIT,
|
|
|
|
'err': syslog.LOG_ERR,
|
|
|
|
'warning': syslog.LOG_WARNING,
|
|
|
|
'notice': syslog.LOG_NOTICE,
|
|
|
|
'info': syslog.LOG_INFO,
|
|
|
|
'debug': syslog.LOG_DEBUG
|
2021-01-02 20:40:27 +01:00
|
|
|
}.get(priority, syslog.LOG_INFO)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
2020-05-15 11:05:32 +02:00
|
|
|
def main():
|
2020-03-09 10:11:07 +01:00
|
|
|
# define the available arguments/parameters that a user can pass to
|
|
|
|
# the module
|
|
|
|
module_args = dict(
|
2020-05-15 11:05:32 +02:00
|
|
|
ident=dict(type='str', default='ansible_syslogger'),
|
2020-03-09 10:11:07 +01:00
|
|
|
msg=dict(type='str', required=True),
|
|
|
|
priority=dict(type='str', required=False,
|
|
|
|
choices=["emerg", "alert", "crit", "err", "warning",
|
|
|
|
"notice", "info", "debug"],
|
|
|
|
default='info'),
|
|
|
|
facility=dict(type='str', required=False,
|
|
|
|
choices=["kern", "user", "mail", "daemon", "auth",
|
|
|
|
"lpr", "news", "uucp", "cron", "syslog",
|
|
|
|
"local0", "local1", "local2", "local3",
|
|
|
|
"local4", "local5", "local6", "local7"],
|
|
|
|
default='daemon'),
|
|
|
|
log_pid=dict(type='bool', required=False, default=False)
|
|
|
|
)
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=module_args,
|
|
|
|
)
|
|
|
|
|
|
|
|
result = dict(
|
|
|
|
changed=False,
|
2020-05-15 11:05:32 +02:00
|
|
|
ident=module.params['ident'],
|
2020-03-09 10:11:07 +01:00
|
|
|
priority=module.params['priority'],
|
|
|
|
facility=module.params['facility'],
|
|
|
|
log_pid=module.params['log_pid'],
|
|
|
|
msg=module.params['msg']
|
|
|
|
)
|
|
|
|
|
|
|
|
# do the logging
|
|
|
|
try:
|
2021-01-02 20:40:27 +01:00
|
|
|
syslog.openlog(module.params['ident'],
|
|
|
|
syslog.LOG_PID if module.params['log_pid'] else 0,
|
|
|
|
get_facility(module.params['facility']))
|
2020-03-09 10:11:07 +01:00
|
|
|
syslog.syslog(get_priority(module.params['priority']),
|
|
|
|
module.params['msg'])
|
|
|
|
syslog.closelog()
|
|
|
|
result['changed'] = True
|
|
|
|
|
2021-01-02 20:40:27 +01:00
|
|
|
except Exception as exc:
|
|
|
|
module.fail_json(error='Failed to write to syslog %s' % to_native(exc), exception=traceback.format_exc(), **result)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|