1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/plugins/modules/sensu_subscription.py

161 lines
4.9 KiB
Python
Raw Permalink Normal View History

2020-03-09 10:11:07 +01:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2014, Anders Ingemann <aim@secoya.dk>
# 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
2020-03-09 10:11:07 +01:00
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
---
module: sensu_subscription
short_description: Manage Sensu subscriptions
description:
- Manage which I(sensu channels) a machine should subscribe to
extends_documentation_fragment:
- community.general.attributes
attributes:
check_mode:
support: full
diff_mode:
support: none
2020-03-09 10:11:07 +01:00
options:
name:
Tidy up validate-modules ignores for monitoring modules (#1350) * fixed validation-modules for plugins/modules/monitoring/bigpanda.py * fixed validation-modules for plugins/modules/monitoring/circonus_annotation.py * fixed validation-modules for plugins/modules/monitoring/honeybadger_deployment.py * fixed validation-modules for plugins/modules/monitoring/icinga2_feature.py * fixed validation-modules for plugins/modules/monitoring/icinga2_host.py * fixed validation-modules for plugins/modules/monitoring/librato_annotation.py * fixed validation-modules for plugins/modules/monitoring/logentries.py * fixed validation-modules for plugins/modules/monitoring/logstash_plugin.py * fixed validation-modules for plugins/modules/monitoring/newrelic_deployment.py * fixed validation-modules for plugins/modules/monitoring/pagerduty_alert.py * fixed validation-modules for plugins/modules/monitoring/pagerduty.py * fixed validation-modules for plugins/modules/monitoring/pingdom.py * fixed validation-modules for plugins/modules/monitoring/rollbar_deployment.py * fixed validation-modules for plugins/modules/monitoring/spectrum_device.py * fixed validation-modules for plugins/modules/monitoring/stackdriver.py * fixed validation-modules for plugins/modules/monitoring/statusio_maintenance.py * fixed validation-modules for plugins/modules/monitoring/uptimerobot.py * fixed validation-modules for plugins/modules/monitoring/datadog/datadog_event.py * fixed validation-modules for plugins/modules/monitoring/datadog/datadog_monitor.py * fixed validation-modules for plugins/modules/monitoring/sensu/sensu_check.py * fixed validation-modules for plugins/modules/monitoring/sensu/sensu_client.py * fixed validation-modules for plugins/modules/monitoring/sensu/sensu_handler.py * fixed validation-modules for plugins/modules/monitoring/sensu/sensu_silence.py * fixed validation-modules for plugins/modules/monitoring/sensu/sensu_subscription.py * fixed trailing space * Enabling validation-modules for monitoring modules * Added line for 2.9 * Update plugins/modules/monitoring/icinga2_host.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/monitoring/datadog/datadog_event.py Co-authored-by: Felix Fontein <felix@fontein.de> * Suggestion from PR * oops, missed the 2.11 ignore file * Update plugins/modules/monitoring/icinga2_host.py Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2020-11-23 22:07:03 +01:00
type: str
2020-03-09 10:11:07 +01:00
description:
- The name of the channel
required: true
state:
Tidy up validate-modules ignores for monitoring modules (#1350) * fixed validation-modules for plugins/modules/monitoring/bigpanda.py * fixed validation-modules for plugins/modules/monitoring/circonus_annotation.py * fixed validation-modules for plugins/modules/monitoring/honeybadger_deployment.py * fixed validation-modules for plugins/modules/monitoring/icinga2_feature.py * fixed validation-modules for plugins/modules/monitoring/icinga2_host.py * fixed validation-modules for plugins/modules/monitoring/librato_annotation.py * fixed validation-modules for plugins/modules/monitoring/logentries.py * fixed validation-modules for plugins/modules/monitoring/logstash_plugin.py * fixed validation-modules for plugins/modules/monitoring/newrelic_deployment.py * fixed validation-modules for plugins/modules/monitoring/pagerduty_alert.py * fixed validation-modules for plugins/modules/monitoring/pagerduty.py * fixed validation-modules for plugins/modules/monitoring/pingdom.py * fixed validation-modules for plugins/modules/monitoring/rollbar_deployment.py * fixed validation-modules for plugins/modules/monitoring/spectrum_device.py * fixed validation-modules for plugins/modules/monitoring/stackdriver.py * fixed validation-modules for plugins/modules/monitoring/statusio_maintenance.py * fixed validation-modules for plugins/modules/monitoring/uptimerobot.py * fixed validation-modules for plugins/modules/monitoring/datadog/datadog_event.py * fixed validation-modules for plugins/modules/monitoring/datadog/datadog_monitor.py * fixed validation-modules for plugins/modules/monitoring/sensu/sensu_check.py * fixed validation-modules for plugins/modules/monitoring/sensu/sensu_client.py * fixed validation-modules for plugins/modules/monitoring/sensu/sensu_handler.py * fixed validation-modules for plugins/modules/monitoring/sensu/sensu_silence.py * fixed validation-modules for plugins/modules/monitoring/sensu/sensu_subscription.py * fixed trailing space * Enabling validation-modules for monitoring modules * Added line for 2.9 * Update plugins/modules/monitoring/icinga2_host.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/monitoring/datadog/datadog_event.py Co-authored-by: Felix Fontein <felix@fontein.de> * Suggestion from PR * oops, missed the 2.11 ignore file * Update plugins/modules/monitoring/icinga2_host.py Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2020-11-23 22:07:03 +01:00
type: str
2020-03-09 10:11:07 +01:00
description:
- Whether the machine should subscribe or unsubscribe from the channel
choices: [ 'present', 'absent' ]
required: false
default: present
path:
Tidy up validate-modules ignores for monitoring modules (#1350) * fixed validation-modules for plugins/modules/monitoring/bigpanda.py * fixed validation-modules for plugins/modules/monitoring/circonus_annotation.py * fixed validation-modules for plugins/modules/monitoring/honeybadger_deployment.py * fixed validation-modules for plugins/modules/monitoring/icinga2_feature.py * fixed validation-modules for plugins/modules/monitoring/icinga2_host.py * fixed validation-modules for plugins/modules/monitoring/librato_annotation.py * fixed validation-modules for plugins/modules/monitoring/logentries.py * fixed validation-modules for plugins/modules/monitoring/logstash_plugin.py * fixed validation-modules for plugins/modules/monitoring/newrelic_deployment.py * fixed validation-modules for plugins/modules/monitoring/pagerduty_alert.py * fixed validation-modules for plugins/modules/monitoring/pagerduty.py * fixed validation-modules for plugins/modules/monitoring/pingdom.py * fixed validation-modules for plugins/modules/monitoring/rollbar_deployment.py * fixed validation-modules for plugins/modules/monitoring/spectrum_device.py * fixed validation-modules for plugins/modules/monitoring/stackdriver.py * fixed validation-modules for plugins/modules/monitoring/statusio_maintenance.py * fixed validation-modules for plugins/modules/monitoring/uptimerobot.py * fixed validation-modules for plugins/modules/monitoring/datadog/datadog_event.py * fixed validation-modules for plugins/modules/monitoring/datadog/datadog_monitor.py * fixed validation-modules for plugins/modules/monitoring/sensu/sensu_check.py * fixed validation-modules for plugins/modules/monitoring/sensu/sensu_client.py * fixed validation-modules for plugins/modules/monitoring/sensu/sensu_handler.py * fixed validation-modules for plugins/modules/monitoring/sensu/sensu_silence.py * fixed validation-modules for plugins/modules/monitoring/sensu/sensu_subscription.py * fixed trailing space * Enabling validation-modules for monitoring modules * Added line for 2.9 * Update plugins/modules/monitoring/icinga2_host.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/monitoring/datadog/datadog_event.py Co-authored-by: Felix Fontein <felix@fontein.de> * Suggestion from PR * oops, missed the 2.11 ignore file * Update plugins/modules/monitoring/icinga2_host.py Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2020-11-23 22:07:03 +01:00
type: str
2020-03-09 10:11:07 +01:00
description:
- Path to the subscriptions json file
required: false
default: /etc/sensu/conf.d/subscriptions.json
backup:
description:
- Create a backup file (if yes), including the timestamp information so you
- can get the original file back if you somehow clobbered it incorrectly.
type: bool
required: false
default: false
2020-03-09 10:11:07 +01:00
requirements: [ ]
author: Anders Ingemann (@andsens)
'''
RETURN = '''
reasons:
description: the reasons why the module changed or did not change something
returned: success
type: list
sample: ["channel subscription was absent and state is `present'"]
'''
EXAMPLES = '''
# Subscribe to the nginx channel
2020-05-16 15:07:51 +02:00
- name: Subscribe to nginx checks
community.general.sensu_subscription: name=nginx
2020-03-09 10:11:07 +01:00
# Unsubscribe from the common checks channel
2020-05-16 15:07:51 +02:00
- name: Unsubscribe from common checks
community.general.sensu_subscription: name=common state=absent
2020-03-09 10:11:07 +01:00
'''
import json
import traceback
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native
2020-03-09 10:11:07 +01:00
def sensu_subscription(module, path, name, state='present', backup=False):
changed = False
reasons = []
try:
config = json.load(open(path))
except IOError as e:
if e.errno == 2: # File not found, non-fatal
if state == 'absent':
reasons.append('file did not exist and state is `absent\'')
return changed, reasons
config = {}
else:
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
except ValueError:
msg = '{path} contains invalid JSON'.format(path=path)
module.fail_json(msg=msg)
if 'client' not in config:
if state == 'absent':
reasons.append('`client\' did not exist and state is `absent\'')
return changed, reasons
config['client'] = {}
changed = True
reasons.append('`client\' did not exist')
if 'subscriptions' not in config['client']:
if state == 'absent':
reasons.append('`client.subscriptions\' did not exist and state is `absent\'')
return changed, reasons
config['client']['subscriptions'] = []
changed = True
reasons.append('`client.subscriptions\' did not exist')
if name not in config['client']['subscriptions']:
if state == 'absent':
reasons.append('channel subscription was absent')
return changed, reasons
config['client']['subscriptions'].append(name)
changed = True
reasons.append('channel subscription was absent and state is `present\'')
else:
if state == 'absent':
config['client']['subscriptions'].remove(name)
changed = True
reasons.append('channel subscription was present and state is `absent\'')
if changed and not module.check_mode:
if backup:
module.backup_local(path)
try:
open(path, 'w').write(json.dumps(config, indent=2) + '\n')
except IOError as e:
module.fail_json(msg='Failed to write to file %s: %s' % (path, to_native(e)),
exception=traceback.format_exc())
return changed, reasons
def main():
arg_spec = {'name': {'type': 'str', 'required': True},
'path': {'type': 'str', 'default': '/etc/sensu/conf.d/subscriptions.json'},
'state': {'type': 'str', 'default': 'present', 'choices': ['present', 'absent']},
'backup': {'type': 'bool', 'default': False},
2020-03-09 10:11:07 +01:00
}
module = AnsibleModule(argument_spec=arg_spec,
supports_check_mode=True)
path = module.params['path']
name = module.params['name']
state = module.params['state']
backup = module.params['backup']
changed, reasons = sensu_subscription(module, path, name, state, backup)
module.exit_json(path=path, name=name, changed=changed, msg='OK', reasons=reasons)
if __name__ == '__main__':
main()