From 58ed77e851179df6caf6b2e463a4db802e2c051b Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Fri, 15 May 2020 14:35:32 +0530 Subject: [PATCH] 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. --- changelogs/fragments/319-syslogger.yml | 2 + plugins/modules/notification/syslogger.py | 92 ++++++++++++++--------- 2 files changed, 59 insertions(+), 35 deletions(-) create mode 100644 changelogs/fragments/319-syslogger.yml diff --git a/changelogs/fragments/319-syslogger.yml b/changelogs/fragments/319-syslogger.yml new file mode 100644 index 0000000000..c41c43ba8b --- /dev/null +++ b/changelogs/fragments/319-syslogger.yml @@ -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). diff --git a/plugins/modules/notification/syslogger.py b/plugins/modules/notification/syslogger.py index 1768456442..cfd4733ee2 100644 --- a/plugins/modules/notification/syslogger.py +++ b/plugins/modules/notification/syslogger.py @@ -1,5 +1,5 @@ #!/usr/bin/python -# Copyright (c) 2017 Tim Rightnour +# Copyright: (c) 2017, Tim Rightnour # 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()