| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2013, 2014, Jan-Piet Mens <jpmens () gmail.com> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: mqtt |
| 11 |
short_description: Publish a message on an MQTT topic for the IoT |
| 12 |
description: |
| 13 |
- Publish a message on an MQTT topic. |
| 14 |
extends_documentation_fragment: |
| 15 |
- community.general._attributes |
| 16 |
attributes: |
| 17 |
check_mode: |
| 18 |
support: full |
| 19 |
diff_mode: |
| 20 |
support: none |
| 21 |
options: |
| 22 |
server: |
| 23 |
type: str |
| 24 |
description: |
| 25 |
- MQTT broker address/name. |
| 26 |
default: localhost |
| 27 |
port: |
| 28 |
type: int |
| 29 |
description: |
| 30 |
- MQTT broker port number. |
| 31 |
default: 1883 |
| 32 |
username: |
| 33 |
type: str |
| 34 |
description: |
| 35 |
- Username to authenticate against the broker. |
| 36 |
password: |
| 37 |
type: str |
| 38 |
description: |
| 39 |
- Password for O(username) to authenticate against the broker. |
| 40 |
client_id: |
| 41 |
type: str |
| 42 |
description: |
| 43 |
- MQTT client identifier. |
| 44 |
- If not specified, it uses a value C(hostname + pid). |
| 45 |
topic: |
| 46 |
type: str |
| 47 |
description: |
| 48 |
- MQTT topic name. |
| 49 |
required: true |
| 50 |
payload: |
| 51 |
type: str |
| 52 |
description: |
| 53 |
- Payload. The special string V("None") may be used to send a NULL (that is, empty) payload which is useful to simply |
| 54 |
notify with the O(topic) or to clear previously retained messages. |
| 55 |
required: true |
| 56 |
qos: |
| 57 |
type: str |
| 58 |
description: |
| 59 |
- QoS (Quality of Service). |
| 60 |
default: "0" |
| 61 |
choices: ["0", "1", "2"] |
| 62 |
retain: |
| 63 |
description: |
| 64 |
- Setting this flag causes the broker to retain (in other words keep) the message so that applications that subsequently |
| 65 |
subscribe to the topic can received the last retained message immediately. |
| 66 |
type: bool |
| 67 |
default: false |
| 68 |
ca_cert: |
| 69 |
type: path |
| 70 |
description: |
| 71 |
- The path to the Certificate Authority certificate files that are to be treated as trusted by this client. If this |
| 72 |
is the only option given then the client operates in a similar manner to a web browser. That is to say it requires |
| 73 |
the broker to have a certificate signed by the Certificate Authorities in ca_certs and communicates using TLS v1, |
| 74 |
but does not attempt any form of authentication. This provides basic network encryption but may not be sufficient |
| 75 |
depending on how the broker is configured. |
| 76 |
aliases: [ca_certs] |
| 77 |
client_cert: |
| 78 |
type: path |
| 79 |
description: |
| 80 |
- The path pointing to the PEM encoded client certificate. If this is set it is used as client information for TLS based |
| 81 |
authentication. Support for this feature is broker dependent. |
| 82 |
aliases: [certfile] |
| 83 |
client_key: |
| 84 |
type: path |
| 85 |
description: |
| 86 |
- The path pointing to the PEM encoded client private key. If this is set it is used as client information for TLS based |
| 87 |
authentication. Support for this feature is broker dependent. |
| 88 |
aliases: [keyfile] |
| 89 |
tls_version: |
| 90 |
description: |
| 91 |
- Specifies the version of the SSL/TLS protocol to be used. |
| 92 |
- By default (if the python version supports it) the highest TLS version is detected. If unavailable, TLS v1 is used. |
| 93 |
type: str |
| 94 |
choices: |
| 95 |
- tlsv1.1 |
| 96 |
- tlsv1.2 |
| 97 |
requirements: [mosquitto] |
| 98 |
notes: |
| 99 |
- This module requires a connection to an MQTT broker such as Mosquitto U(http://mosquitto.org) and the I(Paho) C(mqtt) |
| 100 |
Python client (U(https://pypi.org/project/paho-mqtt/)). |
| 101 |
author: "Jan-Piet Mens (@jpmens)" |
| 102 |
""" |
| 103 |
|
| 104 |
EXAMPLES = r""" |
| 105 |
- name: Publish a message on an MQTT topic |
| 106 |
community.general.mqtt: |
| 107 |
topic: 'service/ansible/{{ ansible_hostname }}' |
| 108 |
payload: 'Hello at {{ ansible_date_time.iso8601 }}' |
| 109 |
qos: 0 |
| 110 |
retain: false |
| 111 |
client_id: ans001 |
| 112 |
delegate_to: localhost |
| 113 |
""" |
| 114 |
|
| 115 |
# =========================================== |
| 116 |
# MQTT module support methods. |
| 117 |
# |
| 118 |
|
| 119 |
import os |
| 120 |
import ssl |
| 121 |
import traceback |
| 122 |
|
| 123 |
HAS_PAHOMQTT = True |
| 124 |
PAHOMQTT_IMP_ERR = None |
| 125 |
try: |
| 126 |
import socket |
| 127 |
|
| 128 |
import paho.mqtt.publish as mqtt |
| 129 |
except ImportError: |
| 130 |
PAHOMQTT_IMP_ERR = traceback.format_exc() |
| 131 |
HAS_PAHOMQTT = False |
| 132 |
|
| 133 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 134 |
|
| 135 |
# =========================================== |
| 136 |
# Main |
| 137 |
# |
| 138 |
|
| 139 |
|
| 140 |
def main(): |
| 141 |
# From https://docs.python.org/3/library/ssl.html#constants, this: |
| 142 |
# |
| 143 |
# > Deprecated since version 3.6: OpenSSL has deprecated all version specific protocols. Use the default protocol PROTOCOL_TLS_SERVER or |
| 144 |
# > PROTOCOL_TLS_CLIENT with SSLContext.minimum_version and SSLContext.maximum_version instead. |
| 145 |
# |
| 146 |
# @TODO: update the use of `ssl` constants |
| 147 |
tls_map = { |
| 148 |
"tlsv1.2": ssl.PROTOCOL_TLSv1_2, |
| 149 |
"tlsv1.1": ssl.PROTOCOL_TLSv1_1, |
| 150 |
} |
| 151 |
|
| 152 |
module = AnsibleModule( |
| 153 |
argument_spec=dict( |
| 154 |
server=dict(default="localhost"), |
| 155 |
port=dict(default=1883, type="int"), |
| 156 |
topic=dict(required=True), |
| 157 |
payload=dict(required=True), |
| 158 |
client_id=dict(), |
| 159 |
qos=dict(default="0", choices=["0", "1", "2"]), |
| 160 |
retain=dict(default=False, type="bool"), |
| 161 |
username=dict(), |
| 162 |
password=dict(no_log=True), |
| 163 |
ca_cert=dict(type="path", aliases=["ca_certs"]), |
| 164 |
client_cert=dict(type="path", aliases=["certfile"]), |
| 165 |
client_key=dict(type="path", aliases=["keyfile"]), |
| 166 |
tls_version=dict(choices=["tlsv1.1", "tlsv1.2"]), |
| 167 |
), |
| 168 |
supports_check_mode=True, |
| 169 |
) |
| 170 |
|
| 171 |
if not HAS_PAHOMQTT: |
| 172 |
module.fail_json(msg=missing_required_lib("paho-mqtt"), exception=PAHOMQTT_IMP_ERR) |
| 173 |
|
| 174 |
server = module.params.get("server", "localhost") |
| 175 |
port = module.params.get("port", 1883) |
| 176 |
topic = module.params.get("topic") |
| 177 |
payload = module.params.get("payload") |
| 178 |
client_id = module.params.get("client_id", "") |
| 179 |
qos = int(module.params.get("qos", 0)) |
| 180 |
retain = module.params.get("retain") |
| 181 |
username = module.params.get("username", None) |
| 182 |
password = module.params.get("password", None) |
| 183 |
ca_certs = module.params.get("ca_cert", None) |
| 184 |
certfile = module.params.get("client_cert", None) |
| 185 |
keyfile = module.params.get("client_key", None) |
| 186 |
tls_version = module.params.get("tls_version", None) |
| 187 |
|
| 188 |
if client_id is None: |
| 189 |
client_id = f"{socket.getfqdn()}_{os.getpid()}" |
| 190 |
|
| 191 |
if payload and payload == "None": |
| 192 |
payload = None |
| 193 |
|
| 194 |
auth = None |
| 195 |
if username is not None: |
| 196 |
auth = {"username": username, "password": password} |
| 197 |
|
| 198 |
tls = None |
| 199 |
if ca_certs is not None: |
| 200 |
if tls_version: |
| 201 |
tls_version = tls_map.get(tls_version, ssl.PROTOCOL_TLS) |
| 202 |
|
| 203 |
tls = { |
| 204 |
"ca_certs": ca_certs, |
| 205 |
"certfile": certfile, |
| 206 |
"keyfile": keyfile, |
| 207 |
"tls_version": tls_version, |
| 208 |
} |
| 209 |
|
| 210 |
try: |
| 211 |
mqtt.single( |
| 212 |
topic, payload, qos=qos, retain=retain, client_id=client_id, hostname=server, port=port, auth=auth, tls=tls |
| 213 |
) |
| 214 |
except Exception as e: |
| 215 |
module.fail_json(msg=f"unable to publish to MQTT broker {e}", exception=traceback.format_exc()) |
| 216 |
|
| 217 |
module.exit_json(changed=False, topic=topic) |
| 218 |
|
| 219 |
|
| 220 |
if __name__ == "__main__": |
| 221 |
main() |