- name: create domain controller
  hosts: DC
  gather_facts: no

  tasks:
  - name: get network connection names
    ansible.windows.win_powershell:
      parameters:
        IPAddress: '{{ ansible_host }}'
      script: |
        param ($IPAddress)

        $Ansible.Changed = $false

        Get-CimInstance -ClassName Win32_NetworkAdapter -Filter "Netenabled='True'" |
            ForEach-Object -Process {
                $config = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter "Index='$($_.Index)'"
                if ($config.IPAddress -contains $IPAddress) {
                    $_.NetConnectionID
                }
            }
    register: connection_name

  - name: set the DNS for the internal adapters to localhost
    ansible.windows.win_dns_client:
      adapter_names:
      - '{{ connection_name.output[0] }}'
      dns_servers:
      - 127.0.0.1

  - name: ensure domain exists and DC is promoted as a domain controller
    microsoft.ad.domain:
      dns_domain_name: '{{ domain_realm }}'
      safe_mode_password: '{{ domain_password }}'
      reboot: true

  - ansible.windows.win_feature:
      name: RSAT-AD-PowerShell
      state: present

  - name: create domain username
    microsoft.ad.user:
      name: '{{ domain_username }}'
      upn: '{{ domain_user_upn }}'
      description: '{{ domain_username }} Domain Account'
      password: '{{ domain_password }}'
      password_never_expires: yes
      update_password: when_changed
      groups:
        add:
        - Domain Admins
      state: present

- name: setup test host
  hosts: TEST
  gather_facts: no

  tasks:
  - name: get network connection names
    ansible.windows.win_powershell:
      parameters:
        IPAddress: '{{ ansible_host }}'
      script: |
        param ($IPAddress)

        $Ansible.Changed = $false

        Get-CimInstance -ClassName Win32_NetworkAdapter -Filter "Netenabled='True'" |
            ForEach-Object -Process {
                $config = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter "Index='$($_.Index)'"
                if ($config.IPAddress -contains $IPAddress) {
                    $_.NetConnectionID
                }
            }
    register: connection_name

  - name: set DNS for the private adapter to point to the DC
    ansible.windows.win_dns_client:
      adapter_names:
      - '{{ connection_name.output[0] }}'
      dns_servers:
      - '{{ hostvars["DC"]["ansible_host"] }}'
