mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
syslogger: new parameter 'ident' (#340)
Added new parameter ident to specify the name of the application which is sending the message to Syslog using syslogger module.
This commit is contained in:
parent
5d226f86b6
commit
58ed77e851
2 changed files with 59 additions and 35 deletions
2
changelogs/fragments/319-syslogger.yml
Normal file
2
changelogs/fragments/319-syslogger.yml
Normal file
|
@ -0,0 +1,2 @@
|
|||
minor_changes:
|
||||
- syslogger - added new parameter ident to specify the name of application which is sending the message to syslog (https://github.com/ansible-collections/community.general/issues/319).
|
|
@ -1,5 +1,5 @@
|
|||
#!/usr/bin/python
|
||||
# Copyright (c) 2017 Tim Rightnour <thegarbledone@gmail.com>
|
||||
# Copyright: (c) 2017, Tim Rightnour <thegarbledone@gmail.com>
|
||||
# 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
|
||||
|
@ -8,61 +8,85 @@ __metaclass__ = type
|
|||
DOCUMENTATION = '''
|
||||
---
|
||||
module: syslogger
|
||||
|
||||
short_description: Log messages in the syslog
|
||||
|
||||
|
||||
description:
|
||||
- "Uses syslog to add log entries to the host."
|
||||
- "Can specify facility and priority."
|
||||
|
||||
- Uses syslog to add log entries to the host.
|
||||
options:
|
||||
msg:
|
||||
description:
|
||||
- This is the message to place in syslog
|
||||
required: true
|
||||
- This is the message to place in syslog.
|
||||
required: True
|
||||
priority:
|
||||
description:
|
||||
- Set the log priority
|
||||
- Set the log priority.
|
||||
choices: [ "emerg", "alert", "crit", "err", "warning", "notice", "info", "debug" ]
|
||||
required: false
|
||||
default: "info"
|
||||
facility:
|
||||
description:
|
||||
- Set the log facility
|
||||
- Set the log facility.
|
||||
choices: [ "kern", "user", "mail", "daemon", "auth", "lpr", "news",
|
||||
"uucp", "cron", "syslog", "local0", "local1", "local2",
|
||||
"local3", "local4", "local5", "local6", "local7" ]
|
||||
required: false
|
||||
default: "daemon"
|
||||
log_pid:
|
||||
description:
|
||||
- Log the pid in brackets
|
||||
- Log the pid in brackets.
|
||||
type: bool
|
||||
required: false
|
||||
default: "no"
|
||||
|
||||
default: False
|
||||
ident:
|
||||
description:
|
||||
- Specify the name of application name which is sending the log to syslog.
|
||||
type: str
|
||||
default: 'ansible_syslogger'
|
||||
author:
|
||||
- Tim Rightnour (@garbled1)
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# Full example
|
||||
- name: Test syslog
|
||||
syslogger:
|
||||
msg: "Hello from ansible"
|
||||
priority: "err"
|
||||
facility: "daemon"
|
||||
log_pid: true
|
||||
|
||||
# Basic usage
|
||||
EXAMPLES = r'''
|
||||
- name: Simple Usage
|
||||
syslogger:
|
||||
msg: "I will end up as daemon.info"
|
||||
|
||||
- name: Send a log message with err priority and user facility with log_pid
|
||||
syslogger:
|
||||
msg: "Hello from Ansible"
|
||||
priority: "err"
|
||||
facility: "user"
|
||||
log_pid: true
|
||||
|
||||
- name: Specify the name of application which is sending log message
|
||||
syslogger:
|
||||
ident: "MyApp"
|
||||
msg: "I want to believe"
|
||||
priority: "alert"
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
RETURN = r'''
|
||||
ident:
|
||||
description: Name of application sending the message to log
|
||||
returned: always
|
||||
type: str
|
||||
sample: "ansible_syslogger"
|
||||
priority:
|
||||
description: Priority level
|
||||
returned: always
|
||||
type: str
|
||||
sample: "daemon"
|
||||
facility:
|
||||
description: Syslog facility
|
||||
returned: always
|
||||
type: str
|
||||
sample: "info"
|
||||
log_pid:
|
||||
description: Log pid status
|
||||
returned: always
|
||||
type: bool
|
||||
sample: True
|
||||
msg:
|
||||
description: Message sent to syslog
|
||||
returned: always
|
||||
type: str
|
||||
sample: "Hello from Ansible"
|
||||
'''
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
|
@ -105,10 +129,11 @@ def get_priority(x):
|
|||
}.get(x, syslog.LOG_INFO)
|
||||
|
||||
|
||||
def run_module():
|
||||
def main():
|
||||
# define the available arguments/parameters that a user can pass to
|
||||
# the module
|
||||
module_args = dict(
|
||||
ident=dict(type='str', default='ansible_syslogger'),
|
||||
msg=dict(type='str', required=True),
|
||||
priority=dict(type='str', required=False,
|
||||
choices=["emerg", "alert", "crit", "err", "warning",
|
||||
|
@ -129,6 +154,7 @@ def run_module():
|
|||
|
||||
result = dict(
|
||||
changed=False,
|
||||
ident=module.params['ident'],
|
||||
priority=module.params['priority'],
|
||||
facility=module.params['facility'],
|
||||
log_pid=module.params['log_pid'],
|
||||
|
@ -138,11 +164,11 @@ def run_module():
|
|||
# do the logging
|
||||
try:
|
||||
if module.params['log_pid']:
|
||||
syslog.openlog('ansible_syslogger',
|
||||
syslog.openlog(module.params['ident'],
|
||||
logoption=syslog.LOG_PID,
|
||||
facility=get_facility(module.params['facility']))
|
||||
else:
|
||||
syslog.openlog('ansible_syslogger',
|
||||
syslog.openlog(module.params['ident'],
|
||||
facility=get_facility(module.params['facility']))
|
||||
syslog.syslog(get_priority(module.params['priority']),
|
||||
module.params['msg'])
|
||||
|
@ -155,9 +181,5 @@ def run_module():
|
|||
module.exit_json(**result)
|
||||
|
||||
|
||||
def main():
|
||||
run_module()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
Loading…
Reference in a new issue