---
# ---
# Install 'bind' apt based OS
# ---

- name: (caching-nameserver.yml) update
  ansible.builtin.apt:
    update_cache: true
    cache_valid_time: "{{ 0 if common_apt_config_updated is defined and common_apt_config_updated.changed else apt_update_cache_valid_time }}"
  when:
    - ansible_facts["distribution"] == "Debian"
    - apt_update|bool
  tags:
    - apt-caching-nameserver

- name: (caching-nameserver.yml) Configure any half-installed packages 'dpkg --configure -a'
  ansible.builtin.command: dpkg --configure -a
  register: common_dpkg_configure
  changed_when: (common_dpkg_configure.stdout | default('')) | length > 0
  failed_when: common_dpkg_configure.rc != 0
  when:
    - ansible_facts["distribution"] == "Debian"
    - apt_update|bool
  tags:
    - apt-caching-nameserver

- name: (caching-nameserver.yml) upgrade
  ansible.builtin.apt:
    upgrade: "{{ apt_upgrade_type }}"
    update_cache: true
    dpkg_options: "{{ apt_upgrade_dpkg_options | join(',') }}"
  when:
    - ansible_facts["distribution"] == "Debian"
    - apt_dpkg_configure|bool
  tags:
    - apt-caching-nameserver

- name: (caching-nameserver.yml) Install bind packages (using apt)
  ansible.builtin.apt:
    name: "{{ apt_bind_pkgs }}"
    state: present
  when:
    - ansible_facts["distribution"] == "Debian"
    - install_bind_packages|bool
  tags:
    - apt-caching-nameserver

# ---
# Install 'bind' - yum based OS
# ---

- name: (yum.yml) Install system updates for centos systems
  ansible.builtin.dnf:
    name: "*"
    state: present
    update_cache: true
    # cache_valid_time: 3600
  when:
    - ansible_facts.os_family == "RedHat"
    - ansible_facts["distribution"] == "CentOS"
  tags:
    - yum-update

- name: (yum.yml) Install bind packages (using yum)
  ansible.builtin.dnf:
    name: "{{ yum_bind_pks }}"
    state: "{{ yum_install_state }}"
  when:
    - ansible_facts.os_family == "RedHat"
    - ansible_facts["distribution"] == "CentOS"
  tags:
    - yum-caching-nameserver

# ---
# Configure bind on debian systems
# --

- name: (caching-nameserver.yml) Create directory /var/log/named if it does not exist
  ansible.builtin.file:
    path: /var/log/named
    state: directory
    owner: bind
    group: bind
    mode: "0755"
  when:
    - ansible_facts["distribution"] == "Debian"

- name: (caching-nameserver.yml) update named.conf.options configuration file (normal server)
  ansible.builtin.template:
    src: etc/bind/named.conf.options.j2
    dest: /etc/bind/named.conf.options
    backup: true
    owner: root
    group: bind
    mode: "0644"
    # validate: visudo -cf %s
  notify: Reload bind9
  tags:
    - apt-caching-nameserver
    - caching-nameserver
  when:
    - ansible_facts["distribution"] == "Debian"
    - inventory_hostname not in groups["gateway_server"]

# --------------------

# In case of gateway gateway servers ONLY if bind ption file NOT exists
#
- name: Check if file '/etc/bind/named.conf.options' exists
  ansible.builtin.stat:
    path: /etc/bind/named.conf.options
  register: common_file_named_conf_options

- name: (caching-nameserver.yml) update named.conf.options configuration file (gateway server)
  ansible.builtin.template:
    src: etc/bind/named.conf.options.gateway.j2
    dest: /etc/bind/named.conf.options
    backup: true
    owner: root
    group: bind
    mode: "0644"
    # validate: visudo -cf %s
  notify: Reload bind9
  tags:
    - apt-caching-nameserver
    - caching-nameserver
  when:
    - ansible_facts["distribution"] == "Debian"
    - inventory_hostname in groups["gateway_server"]
#    - not common_file_named_conf_options.stat.exists

# --------------------

- name: (caching-nameserver.yml) Add 127.0.0.1 as first nameserver entry to /etc/resolv.conf
  ansible.builtin.lineinfile:
    path: /etc/resolv.conf
    line: nameserver 127.0.0.1
    firstmatch: true
    insertbefore: "^nameserver"
    state: present
    owner: root
    group: root
    mode: "0644"
  tags:
    - apt-caching-nameserver
  when:
    - ansible_facts["distribution"] == "Debian"
    - not systemd_resolved
