Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
01e1991b
Commit
01e1991b
authored
Feb 19, 2014
by
Jan-Piet Mens
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
module update: mqtt notification now uses Paho as mosquitto.py being deprecated
parent
18152684
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
42 deletions
+28
-42
library/notification/mqtt
+28
-42
No files found.
library/notification/mqtt
View file @
01e1991b
#!/usr/bin/python
# -*- 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
#
...
...
@@ -80,7 +80,7 @@ options:
requirements: [ mosquitto ]
notes:
- 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
'''
...
...
@@ -97,34 +97,12 @@ EXAMPLES = '''
# MQTT module support methods.
#
HAS_
MOSQUITTO
=
True
HAS_
PAHOMQTT
=
True
try
:
import
socket
import
mosquitto
import
paho.mqtt.publish
as
mqtt
except
ImportError
:
HAS_MOSQUITTO
=
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
()
HAS_PAHOMQTT
=
False
# ===========================================
# Main
...
...
@@ -132,10 +110,6 @@ def publish(module, topic, payload, server='localhost', port='1883', qos='0',
def
main
():
if
not
HAS_MOSQUITTO
:
module
.
fail_json
(
msg
=
"mosquitto is not installed"
)
module
=
AnsibleModule
(
argument_spec
=
dict
(
server
=
dict
(
default
=
'localhost'
),
...
...
@@ -151,15 +125,18 @@ def main():
supports_check_mode
=
True
)
server
=
module
.
params
[
"server"
]
port
=
module
.
params
[
"port"
]
topic
=
module
.
params
[
"topic"
]
payload
=
module
.
params
[
"payload"
]
client_id
=
module
.
params
[
"client_id"
]
qos
=
module
.
params
[
"qos"
]
retain
=
module
.
params
[
"retain"
]
username
=
module
.
params
[
"username"
]
password
=
module
.
params
[
"password"
]
if
not
HAS_PAHOMQTT
:
module
.
fail_json
(
msg
=
"Paho MQTT is not installed"
)
server
=
module
.
params
.
get
(
"server"
,
'localhost'
)
port
=
module
.
params
.
get
(
"port"
,
1883
)
topic
=
module
.
params
.
get
(
"topic"
)
payload
=
module
.
params
.
get
(
"payload"
)
client_id
=
module
.
params
.
get
(
"client_id"
,
''
)
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
:
client_id
=
"
%
s_
%
s"
%
(
socket
.
getfqdn
(),
os
.
getpid
())
...
...
@@ -167,9 +144,18 @@ def main():
if
payload
and
payload
==
'None'
:
payload
=
None
auth
=
None
if
username
is
not
None
:
auth
=
{
'username'
:
username
,
'password'
:
password
}
try
:
publish
(
module
,
topic
,
payload
,
server
,
port
,
qos
,
client_id
,
retain
,
username
,
password
)
rc
=
mqtt
.
single
(
topic
,
payload
,
qos
=
qos
,
retain
=
retain
,
client_id
=
client_id
,
hostname
=
server
,
port
=
port
,
auth
=
auth
)
except
Exception
,
e
:
module
.
fail_json
(
msg
=
"unable to publish to MQTT broker
%
s"
%
(
e
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment