mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #6062 from jpmens/mqttpaho
module update: mqtt notification now uses Paho as mosquitto.py being deprecated
This commit is contained in:
commit
0bc3cec1c8
1 changed files with 28 additions and 42 deletions
|
@ -1,7 +1,7 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# (c) 2013, Jan-Piet Mens <jpmens () gmail.com>
|
# (c) 2013, 2014, Jan-Piet Mens <jpmens () gmail.com>
|
||||||
#
|
#
|
||||||
# This file is part of Ansible
|
# This file is part of Ansible
|
||||||
#
|
#
|
||||||
|
@ -80,7 +80,7 @@ options:
|
||||||
requirements: [ mosquitto ]
|
requirements: [ mosquitto ]
|
||||||
notes:
|
notes:
|
||||||
- This module requires a connection to an MQTT broker such as Mosquitto
|
- This module requires a connection to an MQTT broker such as Mosquitto
|
||||||
U(http://mosquitto.org) and the C(mosquitto) Python module (U(http://mosquitto.org/python)).
|
U(http://mosquitto.org) and the I(Paho) C(mqtt) Python client (U(https://pypi.python.org/pypi/paho-mqtt)).
|
||||||
author: Jan-Piet Mens
|
author: Jan-Piet Mens
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
@ -97,34 +97,12 @@ EXAMPLES = '''
|
||||||
# MQTT module support methods.
|
# MQTT module support methods.
|
||||||
#
|
#
|
||||||
|
|
||||||
HAS_MOSQUITTO = True
|
HAS_PAHOMQTT = True
|
||||||
try:
|
try:
|
||||||
import socket
|
import socket
|
||||||
import mosquitto
|
import paho.mqtt.publish as mqtt
|
||||||
except ImportError:
|
except ImportError:
|
||||||
HAS_MOSQUITTO = False
|
HAS_PAHOMQTT = False
|
||||||
import os
|
|
||||||
|
|
||||||
def publish(module, topic, payload, server='localhost', port='1883', qos='0',
|
|
||||||
client_id='', retain=False, username=None, password=None):
|
|
||||||
'''Open connection to MQTT broker and publish the topic'''
|
|
||||||
|
|
||||||
mqttc = mosquitto.Mosquitto(client_id, clean_session=True)
|
|
||||||
|
|
||||||
if username is not None and password is not None:
|
|
||||||
mqttc.username_pw_set(username, password)
|
|
||||||
|
|
||||||
rc = mqttc.connect(server, int(port), 5)
|
|
||||||
if rc != 0:
|
|
||||||
module.fail_json(msg="unable to connect to MQTT broker")
|
|
||||||
|
|
||||||
mqttc.publish(topic, payload, int(qos), retain)
|
|
||||||
rc = mqttc.loop()
|
|
||||||
if rc != 0:
|
|
||||||
module.fail_json(msg="unable to send to MQTT broker")
|
|
||||||
|
|
||||||
mqttc.disconnect()
|
|
||||||
|
|
||||||
|
|
||||||
# ===========================================
|
# ===========================================
|
||||||
# Main
|
# Main
|
||||||
|
@ -132,10 +110,6 @@ def publish(module, topic, payload, server='localhost', port='1883', qos='0',
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
if not HAS_MOSQUITTO:
|
|
||||||
module.fail_json(msg="mosquitto is not installed")
|
|
||||||
|
|
||||||
|
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
argument_spec=dict(
|
argument_spec=dict(
|
||||||
server = dict(default = 'localhost'),
|
server = dict(default = 'localhost'),
|
||||||
|
@ -151,15 +125,18 @@ def main():
|
||||||
supports_check_mode=True
|
supports_check_mode=True
|
||||||
)
|
)
|
||||||
|
|
||||||
server = module.params["server"]
|
if not HAS_PAHOMQTT:
|
||||||
port = module.params["port"]
|
module.fail_json(msg="Paho MQTT is not installed")
|
||||||
topic = module.params["topic"]
|
|
||||||
payload = module.params["payload"]
|
server = module.params.get("server", 'localhost')
|
||||||
client_id = module.params["client_id"]
|
port = module.params.get("port", 1883)
|
||||||
qos = module.params["qos"]
|
topic = module.params.get("topic")
|
||||||
retain = module.params["retain"]
|
payload = module.params.get("payload")
|
||||||
username = module.params["username"]
|
client_id = module.params.get("client_id", '')
|
||||||
password = module.params["password"]
|
qos = int(module.params.get("qos", 0))
|
||||||
|
retain = module.params.get("retain")
|
||||||
|
username = module.params.get("username", None)
|
||||||
|
password = module.params.get("password", None)
|
||||||
|
|
||||||
if client_id is None:
|
if client_id is None:
|
||||||
client_id = "%s_%s" % (socket.getfqdn(), os.getpid())
|
client_id = "%s_%s" % (socket.getfqdn(), os.getpid())
|
||||||
|
@ -167,9 +144,18 @@ def main():
|
||||||
if payload and payload == 'None':
|
if payload and payload == 'None':
|
||||||
payload = None
|
payload = None
|
||||||
|
|
||||||
|
auth=None
|
||||||
|
if username is not None:
|
||||||
|
auth = { 'username' : username, 'password' : password }
|
||||||
|
|
||||||
try:
|
try:
|
||||||
publish(module, topic, payload, server, port, qos, client_id, retain,
|
rc = mqtt.single(topic, payload,
|
||||||
username, password)
|
qos=qos,
|
||||||
|
retain=retain,
|
||||||
|
client_id=client_id,
|
||||||
|
hostname=server,
|
||||||
|
port=port,
|
||||||
|
auth=auth)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
module.fail_json(msg="unable to publish to MQTT broker %s" % (e))
|
module.fail_json(msg="unable to publish to MQTT broker %s" % (e))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue