- name: "Install packages: mosquitto, mosquitto-clients"
  package:
    name: "{{ item }}"
    state: present
  with_items:
    - mosquitto
    - mosquitto-clients
  when: mosquitto_install | bool
  tags: download

- name: Disable & Stop 'mosquitto' service
  systemd:
    name: mosquitto
    enabled: no
    state: stopped
  when: mosquitto_install | bool

- name: Create (touch) file /etc/mosquitto/passwd
  file:
    path: /etc/mosquitto/passwd
    state: touch
    mode: "u=rw,g=r,o=r"    # 0644
  when: mosquitto_install | bool

- name: Populate /etc/mosquitto/passwd with actual username/password
  shell: mosquitto_passwd -b /etc/mosquitto/passwd "{{ mosquitto_user }}" "{{ mosquitto_password  }}"
  when: mosquitto_install | bool

- name: Install /etc/mosquitto/conf.d/websockets.conf from template
  template:
    backup: yes
    src: websockets.conf.j2
    dest: /etc/mosquitto/conf.d/websockets.conf
    owner: root
    group: root
    mode: 0755
  when: mosquitto_install | bool

- name: Enable & Start 'mosquitto' service
  systemd:
    daemon_reload: yes
    name: mosquitto
    enabled: yes
    state: started
  when: mosquitto_enabled | bool
