Skip to content

Mosquitto

Overview

Official documentation can be found here

As the official site says Mosquitto is a "message broker that implements the MQTT protocol".

Eclipse Mosquitto docs

Eclipse Mosquitto is an open source message broker that implements the MQTT protocol. Mosquitto is lightweight and is suitable for use on all devices from low power single board computers to full servers.
The MQTT protocol provides a lightweight method of carrying out messaging using a publish/subscribe model. This makes it suitable for Internet of Things messaging such as with low power sensors or mobile devices such as phones, embedded computers or microcontrollers.

It's needed to gather data from IoT devices, which Telegraf collects and sends to the database.

Structure

Reverse Proxy

Since it's LAN only, there's no need for reverse proxy. Hooray 🥳

Quadlet

It's a single container, with the following .container template:

[Unit]
Description=Mosquitto - MQTT Broker
After=network-online.target
Wants=network-online.target

[Container]
Image={{ mosquitto_image }}

PublishPort={{ mosquitto_port }}:{{ mosquitto_port }}

Volume=/{{ parent_dir }}/{{ mosquitto_conf }}:/mosquitto/config/mosquitto.conf:Z,ro

Network={{ podman_network }}

[Service]
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

Required variables

  1. podman-network variable filled in global vars.yml file.

Directory layout

If you leave the defaults, the directory layout will look like this:

/stack/
  mosquitto.conf
Variable mapping:
/stack is parent_dir
mosquitto.conf is mosquitto_conf

Customizations

Only change the roles/mosquitto/defaults/main.yml. Do NOT mess around in the .j2 files unless you know exactly what you are doing.

There are things in the .container.j2 files I could have made a variable but didn't, because I saw no purpose to it.

It doesn't have a ton of options but here's what you change:
2. You can change the name of the mosquitto.conf file by editing mosquitto_conf in roles/mosquitto/defaults/main.yml. 3. To lock in a certain Docker image version, change the mosquitto_image in roles/mosquitto/defaults/main.yml. 4. You can change the permissions of the .conf file by editing mosquitto_file_permissions in roles/mosquitto/defaults/main.yml.

Container name

You can change the container names, but be careful as container names aren't dynamic so other containers might break.

Authentication

Note

I know MQTT supports authentication, but I personally had not set it up.
If you want, you can. Update the mosquitto.conf file and the IoT device configuration to reflect that changes.
In the future, I might add that as an optional part of the role easily toggled with a bool.

Dependencies

It really does not have many dependencies, as Ansible handles them anyways. The only thing you have to do is following the Required variables section.

Commands

Note

Please try to refrain from manual intervention as mush as possible, but if needed these commands can help you diagnose the issues so that you can fix the playbook (to rerun it) not the service.

containername will usually be systemd-mosquitto but you can use podman ps to check

Use podman exec -it <containername> mosquitto_sub -t "#" to subscribe to any topics to see if it gets any data. You can change out # to anything to narrow it, like sensors/# will only sub to data in the sensors topic.
Use journalctl -u <containername> --no-pager to see logs.

Troubleshooting

This is a very simple service, and almost never has issues. If it doesn't gather the data it's supposed to, usually the IoT device configuration is the culprit.

Bugs

None as far as I know