---
# tasks file for nsd

- name: Assertions
  assert:
    that:
        - ansible_os_family in nsd_user
        - ansible_os_family in aliases_file
        - nsd_server_block is defined
        - nsd_remote_control_block is defined
        - nsd_patterns is iterable
        - nsd_keys is iterable
        - nsd_zones is iterable

- name: APT install NSD
  when: ansible_pkg_mgr == 'apt'
  apt:
      name: nsd
      state: present
      update_cache: yes
      cache_valid_time: 3600

- name: Create zones directory
  file:
      path: '{{ nsd_zones_dir }}'
      state: directory
      owner: root
      group: 0
      mode: 0o755

- name: Configure
  template:
      src: nsd.conf.j2
      dest: '{{ nsd_conf }}'
      owner: root
      group: 0
      mode: 0o0644
  notify:
      - Restart NSD

- name: Copy zone templates
  with_fileglob:
      - '{{ role_path }}/templates/nsd/zones/*'
      - '{{ playbook_dir }}/templates/nsd/zones/*'
  template:
      src: '{{ item }}'
      dest: '{{ nsd_zones_dir }}/'
      owner: root
      group: 0
      mode: 0o644
  notify:
      - Restart NSD

- name: Alias emails to root
  when: nsd_mail_alias is defined
  lineinfile:
      dest: '{{ aliases_file[ansible_os_family] }}'
      create: yes
      line: '{{ nsd_user[ansible_os_family] }}: {{ nsd_mail_alias }}'
      regexp: '{{ nsd_user[ansible_os_family] }}:'
  notify:
      - Update SMTPd database

- name: Validate config
  command: 'nsd-checkconf {{ nsd_conf }}'
  changed_when: False

- name: Enable NSD
  service:
      name: nsd
      enabled: yes
      state: started

- meta: flush_handlers

- name: Wait for NSD to recieve requests
  wait_for:
      port: 53
