---
# install and configure bind9/named


- name: install bind package
  apt:
    name: "{{ item }}"
    state: latest
    cache_valid_time: 3600
  with_items:
    - bind9

- name: copy zone files to /etc/bind/
  template:
    src: "{{ item }}.j2"
    dest: /etc/bind/db.{{ item }}
    owner: root
    group: root
    mode: 0644
  with_items: "{{ bind.zonefiles }}"
  notify:
    - restart bind

- name: test zone files
  command: named-checkzone {{ item }}
  changed_when: false
  with_items:
    - xai-corp.net /etc/bind/db.xai-corp.net.internal
    - 4.168.192.IN-ADDR.ARPA. /etc/bind/db.xai-corp.net.reverse

- name: copy named.confs to /etc/bind/
  template:
    src: "{{ item }}.j2"
    dest: "/etc/bind/{{ item }}"
    owner: root
    group: "{{ bind.group }}"
    mode: 0644
  with_items: "{{ bind.namedfiles }}"
  notify:
    - restart bind

- name: test config files
  command: named-checkconf /etc/bind/{{ item }}
  changed_when: false
  with_items: "{{ bind.namedfiles }}"

- name: set named startup options
  ansible.builtin.copy:
    content: OPTIONS="{{ bind.named.options }}"
    dest: /etc/default/named

- name: remove old named sysconfig options
  file:
    state: absent
    path: "{{item}}"
  with_items: "{{ bind.cleanup }}"

- name: Force all notified handlers to run
  ansible.builtin.meta: flush_handlers
