---
# Create a bind master
- hosts: bind
  become: true

  vars:

  tasks:
    - name: firewall daemon
      package:
        name: firewalld
        state: installed

    - name: firewall python module
      package:
        name: python-firewall
        state: installed

    - name: install bind utilities
      package:
        name: bind-utils
        state: installed

    - name: install bind server
      package:
        name: bind
        state: installed

    - name: firewall service
      systemd:
        name: firewalld
        enabled: yes
        state: started

    - name: dns service
      systemd:
        name: named
        enabled: yes
        state: started

    - name: dns port
      firewalld:
        zone: public
        service: dns
        state: enabled
        permanent: yes
        immediate: yes

    - name: named configuration file
      template:
        src: templates/named.conf.j2
        dest: /etc/named.conf

- hosts: masters
  become: true
  vars:

  tasks:

    # On RHEL7 this is python-dns.  On F25 it's python2-dns
    - name: install python DNS package
      package:
        name: python2-dns
        state: installed
      when: (ansible_distribution == "Fedora")

    - name: install python DNS package
      package:
        name: python-dns
        state: installed
      when: (ansible_distribution == "RHEL" or ansible_distribution == "CentOS")

    - name: named master zone configuration file(s)
      template:
        src: templates/zones.conf-master.j2
        dest: /etc/named/zones.conf
      notify:
        - restart named

    - name: set zone update key
      template:
        src: templates/update.key.j2
        dest: /etc/named/update.key
      notify:
        - restart named

    - name: set zone data file(s)
      template:
        src: templates/zone.db.j2
        dest: /var/named/dynamic/zone.db
        owner: named
        group: named
      tags:
        - zonedata
      notify:
        - restart named

  handlers:
    - name: restart named
      systemd: name=named state=restarted

- hosts: slaves
  become: true

  vars:

  tasks:
    - name: named slave zone configuration file(s)
      template:
        src: templates/zones.conf-slave.j2
        dest: /etc/named/zones.conf
      notify:
        - restart named

    - name: set zone update key
      template:
        src: templates/update.key.j2
        dest: /etc/named/update.key
      notify:
        - restart named

  handlers:
    - name: restart named
      systemd: name=named state=restarted
