- name: nameserver | bind | install packages
  package:
    name: "{{ bind_package }}"
    state: present

- name: nameserver | bind | basic configuration
  replace:
    path: "{{ named_conf_options }}"
    regexp: "{{ item.regexp }}"
    replace: "{{ item.option }}"
    validate: "{{ named_checkconf }} %s"
  loop:
    - { regexp: '^\s*listen-on \{(?:[\s\n]*(?:\d{1,3}\.){3}\d{1,3};)*[\s\n]*\};', option: "\tlisten-on { {{ ansible_default_ipv4.address }}; };"}
    - { regexp: '^\s*listen-on-v6 \{[\s\n]*any;[\s\n]*\};', option: "\tlisten-on-v6 { {{ ansible_default_ipv6.address }}; };"}
    - { regexp: '^\s*listen-on-v6 \{(?:[\s\n]*[\da-z:]*;)[\s\n]*\};', option: "\tlisten-on-v6 { {{ ansible_default_ipv6.address }}; };"}
  notify: restart_named

- name: nameserver | bind | basic configuration cont'd
  lineinfile:
    path: "{{ named_conf_options }}"
    regexp: '^\s*listen-on \{.*\};'
    state: present
    line: "\tlisten-on { {{ ansible_default_ipv4.address }}; };"
    insertafter: "options {"
    validate: "{{ named_checkconf }} %s"
  notify: restart_named