summaryrefslogtreecommitdiffstats
path: root/ansible_collections/community/general/plugins/modules/syslogger.py
blob: 3a7abf4fbe30b6801441f908c6b68f1368fb7d3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2017, Tim Rightnour <thegarbledone@gmail.com>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import absolute_import, division, print_function
__metaclass__ = type

DOCUMENTATION = r'''
---
module: syslogger
short_description: Log messages in the syslog
description:
    - Uses syslog to add log entries to the host.
extends_documentation_fragment:
    - community.general.attributes
attributes:
    check_mode:
        support: none
    diff_mode:
        support: none
options:
    msg:
        type: str
        description:
            - This is the message to place in syslog.
        required: true
    priority:
        type: str
        description:
            - Set the log priority.
        choices: [ "emerg", "alert", "crit", "err", "warning", "notice", "info", "debug" ]
        default: "info"
    facility:
        type: str
        description:
            - Set the log facility.
        choices: [ "kern", "user", "mail", "daemon", "auth", "lpr", "news",
                   "uucp", "cron", "syslog", "local0", "local1", "local2",
                   "local3", "local4", "local5", "local6", "local7" ]
        default: "daemon"
    log_pid:
        description:
            - Log the PID in brackets.
        type: bool
        default: false
    ident:
        description:
            - Specify the name of application name which is sending the log to syslog.
        type: str
        default: 'ansible_syslogger'
        version_added: '0.2.0'
author:
    - Tim Rightnour (@garbled1)
'''

EXAMPLES = r'''
- name: Simple Usage
  community.general.syslogger:
    msg: "I will end up as daemon.info"

- name: Send a log message with err priority and user facility with log_pid
  community.general.syslogger:
    msg: "Hello from Ansible"
    priority: "err"
    facility: "user"
    log_pid: true

- name: Specify the name of application which is sending log message
  community.general.syslogger:
    ident: "MyApp"
    msg: "I want to believe"
    priority: "alert"
'''

RETURN = r'''
ident:
  description: Name of application sending the message to log
  returned: always
  type: str
  sample: "ansible_syslogger"
  version_added: '0.2.0'
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"
'''

import syslog
import traceback

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native


def get_facility(facility):
    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
    }.get(facility, syslog.LOG_DAEMON)


def get_priority(priority):
    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
    }.get(priority, syslog.LOG_INFO)


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",
                               "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,
        ident=module.params['ident'],
        priority=module.params['priority'],
        facility=module.params['facility'],
        log_pid=module.params['log_pid'],
        msg=module.params['msg']
    )

    # do the logging
    try:
        syslog.openlog(module.params['ident'],
                       syslog.LOG_PID if module.params['log_pid'] else 0,
                       get_facility(module.params['facility']))
        syslog.syslog(get_priority(module.params['priority']),
                      module.params['msg'])
        syslog.closelog()
        result['changed'] = True

    except Exception as exc:
        module.fail_json(error='Failed to write to syslog %s' % to_native(exc), exception=traceback.format_exc(), **result)

    module.exit_json(**result)


if __name__ == '__main__':
    main()