---

 - name: Clone pi-hole
   become: yes
   git:
     accept_newhostkey: yes
     dest: /opt/pi-hole
     repo: https://github.com/pi-hole/pi-hole.git

 - name: Install pi-hole if needed
   become: yes
   register: pihole_install
   command:
       creates: /usr/bin/pihole-FTL
       cmd: false # bash basic-install.sh
       chdir: '/opt/pi-hole/automated install'

 - name: Ensure pihole web admin password
   become: yes
   command: "pihole -a -p {{ passwords['Nazara'] }}"
     # when: pihole_install.changed

 - name: Generate DNS/DHCP from inventory
   delegate_to: localhost
   run_once: true
   command: "python3 ../bin/generate-pihole-dns-dhcp.py {{ inventory_file }}"

 - name: Nazara DNS
   become: yes
   register: dns_updated
   copy:
     dest: /etc/pihole/custom.list
     src: dns
     owner: pihole
     group: pihole
     mode: 0644

 - name: Nazara DHCP
   become: yes
   register: dhcp_updated
   copy:
     src: dhcp
     dest: /etc/dnsmasq.d/04-pihole-static-dhcp.conf
     owner: root
     group: root
     mode: 0644

 - name: Nazara Configuration
   become: yes
   register: conf_updated
   copy:
     src: pihole-FTL.conf
     dest: /etc/pihole/pihole-FTL.conf
     owner: root
     group: root
     mode: 0644


 - name: Nazara DHCP Leases dir
   become: yes
   file:
     path: /var/lib/misc/
     state: directory
     owner: root
     group: root
     mode: 0777

 - name: Nazara DHCP Leases
   become: yes
   file:
     path: /var/lib/misc/dnsmasq.leases
     state: touch
     owner: pihole
     group: pihole
     mode: 0660

 - name: Reload services
   become: yes
   command: pihole restartdns
   when: dns_updated.changed or dhcp_updated.changed or conf_updated.changed
