mirror of
				https://github.com/ansible-collections/community.general.git
				synced 2025-10-26 05:50:36 -07:00 
			
		
		
		
	* fixed validation-modules for plugins/modules/packaging/language/pip_package_info.py * fixed validation-modules for plugins/modules/packaging/language/maven_artifact.py * fixed validation-modules for plugins/modules/packaging/language/bundler.py * fixed validation-modules for plugins/modules/notification/pushbullet.py * fixed validation-modules for plugins/modules/monitoring/sensu/sensu_handler.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/icinga2_host.py * fixed validation-modules for plugins/modules/monitoring/datadog/datadog_monitor.py * fixed validation-modules for plugins/modules/monitoring/datadog/datadog_event.py * fixed validation-modules for plugins/modules/clustering/znode.py * fixed validation-modules for plugins/modules/clustering/etcd3.py * fixed validation-modules for plugins/modules/clustering/consul/consul_session.py * fixed validation-modules for plugins/modules/clustering/consul/consul_kv.py * fixed validation-modules for plugins/modules/clustering/consul/consul.py * fixed validation-modules for plugins/modules/cloud/profitbricks/profitbricks.py * fixed validation-modules for plugins/modules/cloud/profitbricks/profitbricks_volume.py * fixed validation-modules for plugins/modules/cloud/packet/packet_sshkey.py * fixed validation-modules for plugins/modules/cloud/oneandone/oneandone_server.py * fixed validation-modules for plugins/modules/cloud/oneandone/oneandone_private_network.py * fixed validation-modules for plugins/modules/cloud/oneandone/oneandone_monitoring_policy.py * fixed validation-modules for plugins/modules/cloud/oneandone/oneandone_load_balancer.py * fixed validation-modules for plugins/modules/cloud/oneandone/oneandone_firewall_policy.py * fixed validation-modules for plugins/modules/cloud/webfaction/webfaction_app.py * fixed validation-modules for plugins/modules/cloud/webfaction/webfaction_db.py * fixed validation-modules for plugins/modules/cloud/webfaction/webfaction_domain.py * fixed validation-modules for plugins/modules/cloud/webfaction/webfaction_mailbox.py * fixed validation-modules for plugins/modules/cloud/webfaction/webfaction_site.py * fixed validation-modules for plugins/modules/remote_management/lxca/lxca_cmms.py * fixed validation-modules for plugins/modules/remote_management/lxca/lxca_nodes.py * missed one "elements" in sensu_handler * Tidy up batch of sanity checks ignore lines * missed lines in ignore-2.9.txt * fixed validation-modules for plugins/modules/clustering/consul/consul_acl.py * Update ignore-2.9.txt Removed consul_acl.py from ignore-2.9.txt * Apply suggestions from code review Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru> * Update plugins/modules/notification/pushbullet.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/cloud/oneandone/oneandone_monitoring_policy.py Co-authored-by: Felix Fontein <felix@fontein.de> * added changelog fragment * Update plugins/modules/cloud/oneandone/oneandone_monitoring_policy.py * Update changelogs/fragments/1885-sanity-check-fixes-batch3.yml Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>
		
			
				
	
	
		
			189 lines
		
	
	
	
		
			5.6 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			189 lines
		
	
	
	
		
			5.6 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| #!/usr/bin/python
 | |
| # -*- coding: utf-8 -*-
 | |
| #
 | |
| # Copyright: Ansible Project
 | |
| # 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
 | |
| 
 | |
| 
 | |
| DOCUMENTATION = '''
 | |
| ---
 | |
| author: "Willy Barro (@willybarro)"
 | |
| requirements: [ pushbullet.py ]
 | |
| module: pushbullet
 | |
| short_description: Sends notifications to Pushbullet
 | |
| description:
 | |
|    - This module sends push notifications via Pushbullet to channels or devices.
 | |
| options:
 | |
|     api_key:
 | |
|         type: str
 | |
|         description:
 | |
|             - Push bullet API token
 | |
|         required: true
 | |
|     channel:
 | |
|         type: str
 | |
|         description:
 | |
|             - The channel TAG you wish to broadcast a push notification,
 | |
|               as seen on the "My Channels" > "Edit your channel" at
 | |
|               Pushbullet page.
 | |
|     device:
 | |
|         type: str
 | |
|         description:
 | |
|             - The device NAME you wish to send a push notification,
 | |
|               as seen on the Pushbullet main page.
 | |
|     push_type:
 | |
|         type: str
 | |
|         description:
 | |
|           - Thing you wish to push.
 | |
|         default: note
 | |
|         choices: [ "note", "link" ]
 | |
|     title:
 | |
|         type: str
 | |
|         description:
 | |
|           - Title of the notification.
 | |
|         required: true
 | |
|     body:
 | |
|         type: str
 | |
|         description:
 | |
|           - Body of the notification, e.g. Details of the fault you're alerting.
 | |
|     url:
 | |
|         type: str
 | |
|         description:
 | |
|           - URL field, used when I(push_type) is C(link).
 | |
| 
 | |
| notes:
 | |
|    - Requires pushbullet.py Python package on the remote host.
 | |
|      You can install it via pip with ($ pip install pushbullet.py).
 | |
|      See U(https://github.com/randomchars/pushbullet.py)
 | |
| '''
 | |
| 
 | |
| EXAMPLES = '''
 | |
| - name: Sends a push notification to a device
 | |
|   community.general.pushbullet:
 | |
|     api_key: "ABC123abc123ABC123abc123ABC123ab"
 | |
|     device: "Chrome"
 | |
|     title: "You may see this on Google Chrome"
 | |
| 
 | |
| - name: Sends a link to a device
 | |
|   community.general.pushbullet:
 | |
|     api_key: ABC123abc123ABC123abc123ABC123ab
 | |
|     device: Chrome
 | |
|     push_type: link
 | |
|     title: Ansible Documentation
 | |
|     body: https://docs.ansible.com/
 | |
| 
 | |
| - name: Sends a push notification to a channel
 | |
|   community.general.pushbullet:
 | |
|     api_key: ABC123abc123ABC123abc123ABC123ab
 | |
|     channel: my-awesome-channel
 | |
|     title: Broadcasting a message to the #my-awesome-channel folks
 | |
| 
 | |
| - name: Sends a push notification with title and body to a channel
 | |
|   community.general.pushbullet:
 | |
|     api_key: ABC123abc123ABC123abc123ABC123ab
 | |
|     channel: my-awesome-channel
 | |
|     title: ALERT! Signup service is down
 | |
|     body: Error rate on signup service is over 90% for more than 2 minutes
 | |
| '''
 | |
| 
 | |
| import traceback
 | |
| 
 | |
| PUSHBULLET_IMP_ERR = None
 | |
| try:
 | |
|     from pushbullet import PushBullet
 | |
|     from pushbullet.errors import InvalidKeyError, PushError
 | |
| except ImportError:
 | |
|     PUSHBULLET_IMP_ERR = traceback.format_exc()
 | |
|     pushbullet_found = False
 | |
| else:
 | |
|     pushbullet_found = True
 | |
| 
 | |
| from ansible.module_utils.basic import AnsibleModule, missing_required_lib
 | |
| 
 | |
| 
 | |
| # ===========================================
 | |
| # Main
 | |
| #
 | |
| 
 | |
| def main():
 | |
|     module = AnsibleModule(
 | |
|         argument_spec=dict(
 | |
|             api_key=dict(type='str', required=True, no_log=True),
 | |
|             channel=dict(type='str', default=None),
 | |
|             device=dict(type='str', default=None),
 | |
|             push_type=dict(type='str', default="note", choices=['note', 'link']),
 | |
|             title=dict(type='str', required=True),
 | |
|             body=dict(type='str', default=None),
 | |
|             url=dict(type='str', default=None),
 | |
|         ),
 | |
|         mutually_exclusive=(
 | |
|             ['channel', 'device'],
 | |
|         ),
 | |
|         supports_check_mode=True
 | |
|     )
 | |
| 
 | |
|     api_key = module.params['api_key']
 | |
|     channel = module.params['channel']
 | |
|     device = module.params['device']
 | |
|     push_type = module.params['push_type']
 | |
|     title = module.params['title']
 | |
|     body = module.params['body']
 | |
|     url = module.params['url']
 | |
| 
 | |
|     if not pushbullet_found:
 | |
|         module.fail_json(msg=missing_required_lib('pushbullet.py'), exception=PUSHBULLET_IMP_ERR)
 | |
| 
 | |
|     # Init pushbullet
 | |
|     try:
 | |
|         pb = PushBullet(api_key)
 | |
|         target = None
 | |
|     except InvalidKeyError:
 | |
|         module.fail_json(msg="Invalid api_key")
 | |
| 
 | |
|     # Checks for channel/device
 | |
|     if device is None and channel is None:
 | |
|         module.fail_json(msg="You need to provide a channel or a device.")
 | |
| 
 | |
|     # Search for given device
 | |
|     if device is not None:
 | |
|         devices_by_nickname = {}
 | |
|         for d in pb.devices:
 | |
|             devices_by_nickname[d.nickname] = d
 | |
| 
 | |
|         if device in devices_by_nickname:
 | |
|             target = devices_by_nickname[device]
 | |
|         else:
 | |
|             module.fail_json(msg="Device '%s' not found. Available devices: '%s'" % (device, "', '".join(devices_by_nickname.keys())))
 | |
| 
 | |
|     # Search for given channel
 | |
|     if channel is not None:
 | |
|         channels_by_tag = {}
 | |
|         for c in pb.channels:
 | |
|             channels_by_tag[c.channel_tag] = c
 | |
| 
 | |
|         if channel in channels_by_tag:
 | |
|             target = channels_by_tag[channel]
 | |
|         else:
 | |
|             module.fail_json(msg="Channel '%s' not found. Available channels: '%s'" % (channel, "', '".join(channels_by_tag.keys())))
 | |
| 
 | |
|     # If in check mode, exit saying that we succeeded
 | |
|     if module.check_mode:
 | |
|         module.exit_json(changed=False, msg="OK")
 | |
| 
 | |
|     # Send push notification
 | |
|     try:
 | |
|         if push_type == "link":
 | |
|             target.push_link(title, url, body)
 | |
|         else:
 | |
|             target.push_note(title, body)
 | |
|         module.exit_json(changed=False, msg="OK")
 | |
|     except PushError as e:
 | |
|         module.fail_json(msg="An error occurred, Pushbullet's response: %s" % str(e))
 | |
| 
 | |
|     module.fail_json(msg="An unknown error has occurred")
 | |
| 
 | |
| 
 | |
| if __name__ == '__main__':
 | |
|     main()
 |