- name: Nameserver | powerdns-server | install packages
  package:
    name: "{{ powerdns_server_package }}"
    state: present

- name: Nameserver | powerdns-server | install backends
  package:
    name: "{{ item.package }}"
    state: latest
  with_items: "{{ powerdns_backends }}"

- name: Nameserver | powerdns-server | install and configure database
  when:
    - powerdns_primary is defined
    - powerdns_primary is true
  block:
    - include_role: name=database # required. The name of the role to be executed.

    - name: Nameserver | powerdns-server | install python mysql client
      package:
        name: "{{ python_mysql_client }}"
        state: present
    
    - name: Nameserver | powerdns-server | create database
      mysql_db:
        name: "{{ pdns_db_name }}"
        state: present
        encoding: "utf8"
        config_file: "/etc/mysql/my.cnf"
        login_unix_socket: "{{ pdns_mysql_socket }}"
      register: database_exists
    
    - name: Nameserver | powerdns-server | import database
      mysql_db:
        name: "{{ pdns_db_name }}"
        state: import
        target: "{{ pdns_mysql_schema }}" 
        config_file: "/etc/mysql/my.cnf"
        login_unix_socket: "{{ pdns_mysql_socket }}"
      when: database_exists.changed
    
    - name: Nameserver | powerdns-server | create database user
      mysql_user:
        name: "{{ pdns_db_user }}" 
        password: "{{ pdns_db_passwd | default(lookup('password', '/etc/powerdns/pdns.d/.mysqlpw length=20')) }}" # not required. Set the user's password..
        host: localhost
        priv: "{{ pdns_db_name }}.*:ALL"
        append_privs: False
        sql_log_bin: True
        state: present
        update_password: on_create
        config_file: "/etc/mysql/my.cnf"
        login_unix_socket: "{{ pdns_mysql_socket }}"
    
    - name: Nameserver | powerdns-server | create database users for secondaries
      mysql_user:
        name: "{{ pdns_db_user }}"
        password: "{{ pdns_db_passwd | default(lookup('password', '/etc/powerdns/pdns.d/.mysqlpw length=20')) }}"
        host: "{{ item }}"
        priv: "{{ pdns_db_name }}.*:SELECT"
        append_privs: False
        sql_log_bin: True
        state: present
        update_password: on_create
        config_file: "/etc/mysql/my.cnf"
        login_unix_socket: "{{ pdns_mysql_socket }}"
      loop: "{{ pdns_secondaries }}"

    - name: Nameserver | powerdns-server | configure mysql for network
      ini_file:
        path: "/etc/mysql/mariadb.conf.d/50-server.cnf"
        section: "mysqld"
        option: "bind-address"
        value: "{{ ansible_default_ipv4.address }}"
        state: present
        no_extra_spaces: False # not required. Do not insert spaces before and after '=' symbol.
      notify: restart_mysqld

    - name: Nameserver | powerdns-server | copy config
      template:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
        mode: "0644"
        owner: "root"
        group: "root"
        force: no
      loop:
        - { src: "powerdns-server-pri-mysql-config.j2", dest: "/etc/powerdns/pdns.d/mysql.conf" }
        - { src: "powerdns-server-pri-api-config.j2", dest: "/etc/powerdns/pdns.d/api.conf"}
  notify: restart_pdns

- name: Nameserver | powerdns-server | configure backends for secondaries
  when:
    - powerdns_primary is defined
    - powerdns_primary is false
    - powerdns_secondary is defined
    - powerdns_secondary is true
  template:
    src: "powerdns-server-sec-mysql-config.j2"
    dest: "/etc/powerdns/pdns.d/mysql.conf"
    mode: "0644"
    owner: "root"
    group: "root"
    force: no

- name: Nameserver | powerdns-server | set listening IPs
  when:
    - powerdns_secondary is defined
    - powerdns_secondary is true
    - powerdns_recursor is defined
    - powerdns_recursor is true
  template:
    src: "powerdns-server-sec-mysql-config.j2"
    dest: "/etc/powerdns/pdns.conf"
    owner: "root"
    group: "root"
    force: yes

- name: Nameserver | powerdns-server | disable bind
  block:
    - name: Nameserver | powerdns-server | get running services
      service_facts:

    - name: Nameserver | powerdns-server | disable bind
      service:
        name: "named"
        state: stopped
        enabled: false
      when: "'bind' in service_names"
      vars:
        service_names: "{{ services|dict2items|map(attribute='value.name')|list }}"

- name: Nameserver | powerdns-server | enable service
  service:
    name: "pdns"
    state: started
    enabled: true