- name: "Create smb user: {{ smbuser }}"
  user:
    name: "{{ smbuser }}"
    shell: /sbin/nologin
    password: "{{ smbpassword }}"

- name: "Create public folder: {{ shared_dir }}"
  file:
    path: "{{ shared_dir }}"
    owner: "{{ smbuser }}"
    group: "{{ smbuser }}"
    mode: 0777
    state: directory

# Install and configure samba server (requires ports 137, 138, 139, 445 open).
- name: "Install 4 packages: samba, samba-client, samba-common, cifs-client"
  package:
    name:
      - samba
      - samba-client
      - samba-common
      - cifs-utils
    state: present
  tags:
    - samba
    - download

- name: Install /etc/samba/smb.conf from template
  template:
    src: smb.conf.j2
    dest: /etc/samba/smb.conf

- name: Enable & Start Samba systemd service
  service:
    name: "{{ smb_service }}"
    state: started
    enabled: yes
  tags:
    - samba
  when: samba_enabled

- name: Enable & Start NetBIOS name server ({{ nmb_service }})
  service:
    name: "{{ nmb_service }}"
    state: started
    enabled: yes
  tags:
    - samba
  when: samba_enabled

- name: Disable Samba if not samba_enabled
  service:
    name: "{{ smb_service }}"
    state: stopped
    enabled: no
  tags:
    - samba
  when: not samba_enabled

- name: Disable NetBIOS name server ({{ nmb_service }}) if not samba_enabled
  service:
    name: "{{ nmb_service }}"
    state: stopped
    enabled: no
  tags:
    - samba
  when: not samba_enabled

- name: Add 'samba' variable values to {{ iiab_ini_file }}
  ini_file:
    path: "{{ iiab_ini_file }}"
    section: samba
    option: "{{ item.option }}"
    value: "{{ item.value }}"
  with_items:
    - option: name
      value: Samba
    - option: description
      value: '"Samba is a Microsoft-compatible network file system that re-implements SMB/CIFS (Common Internet File System)."'
    - option: enabled
      value: "{{ samba_enabled }}"
