---
- name: Ensure Stubby is installed
  become: false
  community.general.homebrew:
    name: stubby
    state: present

- name: Ensure hostctl is installed
  become: false
  community.general.homebrew:
    name: guumaster/tap/hostctl
    state: present

- name: Clear the default DNS server settings # noqa 301
  become: false
  replace:
    path: "{{ stubby_config_path }}"
    backup: true
    after: 'DEFAULT UPSTREAMS  ################################'
    before: '############################ OPTIONAL UPSTREAMS'
    regexp: '^(.*)$'
    replace: '\n'

- name: Add custom DNS settings
  become: false
  blockinfile:
    path: "{{ stubby_config_path }}"
    insertafter: "^[# ]*DEFAULT UPSTREAMS[ #]*$"
    block: |
      {{ dns_setting | to_nice_yaml(indent=2) }}

- name: Start Stubby service # noqa 301 # TODO This should be run for each user (perhaps only if user_configs.use_stubby equals true)
  become_user: root
  environment:
    PATH: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:{{ ansible_env.PATH }}
  command: brew services start stubby

- name: Update DNS setting # noqa 301
  become_user: root
  command: /usr/local/opt/stubby/sbin/stubby-setdns-macos.sh

- name: Run generic Configuration tasks
  become_user: root
  block:
    - include_tasks: config-Linux.yml
