diff --git a/defaults/main.yml b/defaults/main.yml index 81e7c47..abb732d 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -3,3 +3,12 @@ mosquitto_accounts: - username: user password_hash: '$6$abcde1234==' open_ufw_to_mosquitto: no +mosquitto_config: + allow_anonymous: false + password_file: /etc/mosquitto/passwd + log_dest: stdout + pid_file: /var/run/mosquitto/mosquitto.pid +mosquitto_listeners: + - port: 1883 + address: localhost + diff --git a/tasks/configure.yml b/tasks/configure.yml index 0e31516..0e60fc9 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -1,31 +1,41 @@ --- -- name: Ensure Mosquitto doesn't allow anonymous access - ansible.builtin.lineinfile: - path: /etc/mosquitto/conf.d/default.conf - regexp: '^allow_anonymous ' - line: 'allow_anonymous false' +- name: Configure Mosquitto + template: + dest: /etc/mosquitto/conf.d/default.conf + src: templates/default.conf.j2 owner: mosquitto group: root mode: 0660 - create: yes backup: yes notify: Restart Mosquitto -- name: Ensure Mosquitto log to standard output (journald) - ansible.builtin.lineinfile: - path: /etc/mosquitto/conf.d/default.conf - regexp: '^log_dest stdout' - line: 'log_dest stdout' - owner: mosquitto - group: root - mode: 0660 - create: yes - backup: yes - notify: Restart Mosquitto +# - name: Ensure Mosquitto doesn't allow anonymous access +# ansible.builtin.lineinfile: +# path: /etc/mosquitto/conf.d/default.conf +# regexp: '^allow_anonymous ' +# line: 'allow_anonymous false' +# owner: mosquitto +# group: root +# mode: 0660 +# create: yes +# backup: yes +# notify: Restart Mosquitto + +# - name: Ensure Mosquitto log to standard output (journald) +# ansible.builtin.lineinfile: +# path: /etc/mosquitto/conf.d/default.conf +# regexp: '^log_dest stdout' +# line: 'log_dest stdout' +# owner: mosquitto +# group: root +# mode: 0660 +# create: yes +# backup: yes +# notify: Restart Mosquitto - name: Ensure password file for Mosquitto exists template: - dest: /etc/mosquitto/passwd + dest: "{{ mosquitto_config['password_file'] | default('/etc/mosquitto/passwd')}}" src: templates/mosquitto_passwd.j2 owner: mosquitto group: root @@ -33,20 +43,30 @@ backup: yes notify: Restart Mosquitto -- name: Ensure Mosquitto use password file - ansible.builtin.lineinfile: - path: /etc/mosquitto/conf.d/default.conf - regexp: '^password_file ' - line: 'password_file /etc/mosquitto/passwd' +- name: Ensure PID file for Mosquitto exists + file: + path: "{{ mosquitto_config['pid_file'] | default('/var/run/mosquitto.pid')}}" + state: touch owner: mosquitto - group: root mode: 0660 - create: yes - backup: yes - notify: Restart Mosquitto + +# - name: Ensure Mosquitto use password file +# ansible.builtin.lineinfile: +# path: /etc/mosquitto/conf.d/default.conf +# regexp: '^password_file ' +# line: 'password_file /etc/mosquitto/passwd' +# owner: mosquitto +# group: root +# mode: 0660 +# create: yes +# backup: yes +# notify: Restart Mosquitto - name: Ensure Mosquitto port is accessible ufw: rule: allow - port: 1883 - when: open_ufw_to_mosquitto + port: "{{ item.port }}" + when: + - open_ufw_to_mosquitto + - item.port != 0 + loop: "{{ mosquitto_listeners }}" diff --git a/templates/default.conf.j2 b/templates/default.conf.j2 new file mode 100644 index 0000000..66a52fb --- /dev/null +++ b/templates/default.conf.j2 @@ -0,0 +1,7 @@ +{{% for key, value in mosquitto_config.items() %}} +{{ key }} {{ value }} +{{% endfor %}} + +{{% for listener in mosquitto_listeners %}} +listener {{ listener.port }} {{ listener.address }} +{{% endfor %}}