---

# ---
# Set some facts
# ---

- name: (systemd-resolved.yml) Set fact_resolved_nameserver (blank separated list)
  set_fact:
    fact_resolved_nameserver: "{{ resolved_nameserver | join (' ') }}"
  when:
    - resolved_nameserver is defined and resolved_nameserver | length > 0
  tags:
    - systemd-resolved

- name: (systemd-resolved.yml) Set fact_resolved_fallback_nameserver (blank separated list)
  set_fact:
    fact_resolved_fallback_nameserver: "{{ resolved_fallback_nameserver | join (' ') }}"
  when:
    - resolved_fallback_nameserver is defined and resolved_fallback_nameserver | length > 0
  tags:
    - systemd-resolved

- name: (systemd-resolved.yml) Set fact_resolved_domains (blank separated list)
  set_fact:
    fact_resolved_domains: "{{ resolved_domains | join (' ') }}"
  when:
    - resolved_domains is defined and resolved_domains | length > 0
  tags:
    - systemd-resolved



# ---
# Install/Enable systemd-resolved package
# ---

- name: (systemd-resolved.yml) Ensure  systemd-resolved package is installed.
  package:
    pkg: systemd-resolved
    state: present
  when:
    - systemd_resolved is defined and systemd_resolved|bool
  tags:
    - systemd-resolved

- name: (systemd-services.yml) Enable service
  systemd:
    name: systemd-resolved
    enabled: true
  when:
    - systemd_resolved is defined and systemd_resolved|bool
  tags:
    - systemd-resolved



# ---
# Create configuration for systemd-resolved
# ---

- name: (systemd-resolved.yml) Ensure directory '/etc/systemd/resolved.conf.d' exists
  file:
    path: /etc/systemd/resolved.conf.d
    state: directory
    mode: 0755
    group: root
    owner: root

- name: (systemd-resolved.yml) Create/Update file '/etc/systemd/resolved.conf.d/50-resolved-local.conf' from template sshd_config.j2
  template:
    src: etc/systemd/resolved.conf.d/50-resolved-local.conf
    dest: /etc/systemd/resolved.conf.d/50-resolved-local.conf
    owner: root
    group: root
    mode: 0644

- name: Restart  systemd-resolved service
  ansible.builtin.service:
    name: systemd-resolved
    state: restarted


