diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..540a476 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,3 @@ +mosquitto_pid_file: /var/run/mosquitto.pid +mosquitto_user: mosquitto +mosquitto_group: mosquitto diff --git a/tasks/install.yml b/tasks/install.yml index fe0b2d2..32f7052 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -14,3 +14,24 @@ with_items: - mosquitto - mosquitto-clients + +- name: Copy systemd unit files + template: + src: "etc/systemd/system/mosquitto.service.j2" + dest: "/etc/systemd/system/mosquitto.service" + owner: root + group: root + mode: 0644 + +- name: Create group '{{ mosquitto_group }}' + group: + name: '{{ mosquitto_group }}' + state: present + +- name: Create user '{{ mosquitto_user }}' + user: + name: '{{ mosquitto_user }}' + group: '{{ mosquitto_group }}' + system: yes + createhome: no + shell: /sbin/nologin diff --git a/templates/etc/systemd/system/mosquitto.service.j2 b/templates/etc/systemd/system/mosquitto.service.j2 new file mode 100644 index 0000000..2be56a9 --- /dev/null +++ b/templates/etc/systemd/system/mosquitto.service.j2 @@ -0,0 +1,21 @@ +## {{ ansible_managed }} +[Unit] +Description=Mosquitto MQTT Broker daemon +ConditionPathExists=/etc/mosquitto/mosquitto.conf +After=network.target +Requires=network.target + +[Service] +User={{ mosquitto_user }} +Group={{ mosquitto_user }} +Type=forking +RemainAfterExit=no +StartLimitInterval=0 +PIDFile={{ mosquitto_pid_file }} +ExecStart=/bin/sh -c "/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d" +ExecReload=/bin/kill -HUP $MAINPID +Restart=on-failure +RestartSec=2 + +[Install] +WantedBy=multi-user.target