---

### tags:
###   update_deployment

- name: "Check if /etc/resolv.conf is link"
  stat:
    path: "/etc/resolv.conf"
  register: link

- name: "Stopping systemd-resolved if running"
  ansible.builtin.systemd:
    name: systemd-resolved.service
    state: stopped

- name: "Remove symbolic link to /run/systemd/resolve/stub-resolv.conf"
  file:
    path: "/etc/resolv.conf"
    state: absent
  when: link.stat.islnk is defined and link.stat.islnk

- name: "Copy /run/systemd/resolve/stub-resolv.conf"
  ansible.builtin.copy:
    src: "/run/systemd/resolve/stub-resolv.conf"
    remote_src: true
    dest: "/etc/resolv.conf"
    owner: root
    group: root
    mode: '0644'
  when: link.stat.islnk is defined and link.stat.islnk

- name: Adding 8.8.8.8 as fallback to resolv.conf
  ansible.builtin.lineinfile:
    path: /etc/resolv.conf
    regexp: '^nameserver 8\.8\.8\.8'
    insertafter: '^nameserver 127\.0\.0.*'
    line: nameserver 8.8.8.8

- name: "Disabling systemd-resolved if enabled"
  ansible.builtin.systemd:
    name: systemd-resolved.service
    enabled: no

- name: "Deploying shared service dns to <{{ current_host }}><{{ current_server_ip }}>"
  include_role:
    name: shared_service
  vars:
    current_service: "pdns"
    current_server_ip : "{{ stage_server_ip }}"
    current_dns_entry: "{{ inventory_hostname }}"
    current_dns_entries : "{{ pdns_public_dns_entries | default([]) }}"
    current_service_id : "{{ pdns_id }}"
    current_service_docker : "{{ pdns_docker }}"

- name: Verify resolv.conf
  ansible.builtin.lineinfile:
    path: /etc/resolv.conf
    regexp: '^nameserver 127\.0\.0.*'
    line: nameserver 127.0.0.1
 
- name: "Delete search line if present"
  ansible.builtin.lineinfile:
    path: /etc/resolv.conf
    regexp: '^search *'
    state: absent
