<commit_before>### IPA
- block:
    - name: check existing record for {{ item }}
      shell: echo {{ hostvars['ipa'].ipa_admin_password }} | kinit admin 2>/dev/null 1>/dev/null && ipa dnsrecord-show {{ hailstorm_dns_domain }} {{ item }}  | awk 'match($0,/A record:\s*/) {print substr($0, RSTART+RLENGTH)}'
      register: iparecord
      failed_when: iparecord.rc != 0 and 'DNS resource record not found' not in iparecord.stderr
      changed_when: false

    # # TODO - IPA can return multiple IPs in the output, how to deal?
    # - name: remove previous record for {{ item }}
    #   shell: echo {{ hostvars['ipa'].ipa_admin_password }} | kinit admin && ipa dnsrecord-del {{ hailstorm_dns_domain }} {{ item }} --a-rec={{ iparecord.stdout }}
    #   when: item != 'ipa' and iparecord.rc == 0 and iparecord.stdout != '' and ip not in iparecord.stdout

    - name: create new record for {{ item }}
      shell: echo {{ hostvars['ipa'].ipa_admin_password }} | kinit admin && ipa dnsrecord-add {{ hailstorm_dns_domain }} {{ item }} --a-rec={{ ip }}
      register: iparecord2
      failed_when: iparecord2.rc != 0 and 'no modifications to be performed' not in iparecord2.stderr
      when: iparecord.rc != 0 or ip not in iparecord.stdout
  delegate_to: ipa

- set_fact:
    temp_external_ip: "{{ (external_network_config.services_network_dnat.mapping|selectattr('expose_machine','match','^' + name + '$')|first).on_host_prefix|ipaddr('address') }}"
  when: mode=='update_services_network' and external_network_config is defined and external_network_config.services_network_dnat.mapping|selectattr('expose_machine','match','^' + name + '$')|list|length > 0


### DNAT DNS
- block:
    - name: add or change {{ item }} in zone db
      lineinfile:
        dest: "/etc/named/zones/master/db.{{ hailstorm_dns_domain }}"
        line: "{{ item }} 				IN      A       {{ temp_external_ip|default(ip) }}"
        regexp: "^{{ item|regex_escape }}\\s+.*"
      when: >
        mode != 'update_services_network' or
        (external_network_config is defined and
        external_network_config.services_network_dnat.mapping|selectattr('expose_machine','match','^' + name + '$')|list|length > 0)
      notify:
        - create serial
        - update serial
        - restart named
  when: not skip_dnat_dns|default(false,true)
  delegate_to: infrastructure


### AD DNS
# work around the problem that delegating to AD does not seem to pull the proper variables from the AD host
- set_fact:
    ansible_winrm_server_cert_validation: ignore
- name: test if AD is available for remote management
  shell: curl -k --connect-timeout 1 https://{{ hostvars[groups['primary-ad-group'][0]].ansible_host }}:5986
  register: ad_available
  changed_when: false
  #delegate_to: "{{ infrastructure_delegate_host_used_to_test_if_layer2_host_is_available }}"
  # need to delegate to localhost since winrm cannot use jump host
  delegate_to: localhost
  failed_when: false

- block:
    # - name: query A record in AD DNS
    #   set_fact: existing_a_record="{{ lookup('dig', hostname + ' @' + hostvars[groups['primary-ad-group'][0]].ansible_host) }}"
    - name: query A record in AD DNS
      win_shell: Get-DnsServerResourceRecord -ZoneName {{ hailstorm_dns_domain }} -Name {{ item }} -RRType A | %{$_.RecordData.IPv4Address.IPAddressToString}
      failed_when: false
      changed_when: false
      register: temp_existing_a_record

    - set_fact:
        existing_a_record: 'NXDOMAIN'
      when: temp_existing_a_record.rc != 0

    - set_fact:
        existing_a_record: "{{ temp_existing_a_record.stdout_lines|map('trim')|list }}"
      when: temp_existing_a_record.rc == 0

    - debug: var=existing_a_record

    # - debug: var=item
    # - debug: var=existing_a_record

    # - name: remove existing A record
    #   win_shell: Remove-DnsServerResourceRecord -ZoneName {{ hailstorm_dns_domain }} -Name {{ item }} -RRType A -Force
    #   when: existing_a_record != 'NXDOMAIN' and existing_a_record != temp_external_ip|default(ip)

    - name: create A record in AD DNS
      win_shell: Add-DNSServerResourceRecordA -ZoneName {{ hailstorm_dns_domain }} -Name {{ item }} -IPv4Address {{ temp_external_ip|default(ip) }}
      when: existing_a_record == 'NXDOMAIN' or temp_external_ip|default(ip) not in existing_a_record

    - set_fact:
        temp_ptr_record: "{{ temp_external_ip|default(ip)|ipaddr('revdns') }}"
        temp_reverse_zone_name: "{{ temp_external_ip|default(ip)|ipaddr('revdns')|regex_replace('\\d+\\.(.*)','\\1') }}"
        temp_host_prefix: "{{ temp_external_ip|default(ip) }}/{% if temp_external_ip is defined %}{{ infrastructure_network_guests.default_gw_host_prefix|ipaddr('prefix') }}{% else %}{{ infrastructure_network_services.default_gw_host_prefix|ipaddr('prefix') }}{% endif %}"

    - name: get DNS zones
      win_shell: Get-DnsServerZone | %{$_.ZoneName}
      changed_when: false
      register: ad_existing_zones

    - debug: var=temp_reverse_zone_name
    - debug: var=ad_existing_zones.stdout_lines

    # need to cut off last character since this is a dot that is not returned from AD
    - name: create reverse lookup zone
      win_shell: Add-DnsServerPrimaryZone -NetworkId {{ temp_host_prefix|ipaddr('network') }}/{{ temp_host_prefix|ipaddr('prefix') }} -ReplicationScope Domain
      when: temp_reverse_zone_name[:-1] not in ad_existing_zones.stdout_lines

    - name: query PTR record in AD DNS
      win_shell: Get-DnsServerResourceRecord -ZoneName {{ temp_reverse_zone_name  }} -Name {{ temp_ptr_record }} -RRType PTR | %{$_.RecordData.PtrDomainName}
      failed_when: false
      changed_when: false
      register: temp_existing_ptr_record

    - set_fact:
        temp_existing_ptr_record2: "{{ temp_existing_ptr_record.stdout_lines|map('trim')|list }}"
    # - debug: var=temp_existing_ptr_record2
    # - debug: var=temp_ptr_record

    # need to cut off last character since this is a dot that is actually returned from AD
    - name: delete PTR record in AD DNS
      win_shell: Remove-DnsServerResourceRecord -ZoneName {{ temp_reverse_zone_name }} -Name {{ temp_ptr_record }} -RRType PTR -Force<commit_msg>HSRACKDEV-724 #qa fix PTR zone creation<commit_after>### IPA
- block:
    - name: check existing record for {{ item }}
      shell: echo {{ hostvars['ipa'].ipa_admin_password }} | kinit admin 2>/dev/null 1>/dev/null && ipa dnsrecord-show {{ hailstorm_dns_domain }} {{ item }}  | awk 'match($0,/A record:\s*/) {print substr($0, RSTART+RLENGTH)}'
      register: iparecord
      failed_when: iparecord.rc != 0 and 'DNS resource record not found' not in iparecord.stderr
      changed_when: false

    # # TODO - IPA can return multiple IPs in the output, how to deal?
    # - name: remove previous record for {{ item }}
    #   shell: echo {{ hostvars['ipa'].ipa_admin_password }} | kinit admin && ipa dnsrecord-del {{ hailstorm_dns_domain }} {{ item }} --a-rec={{ iparecord.stdout }}
    #   when: item != 'ipa' and iparecord.rc == 0 and iparecord.stdout != '' and ip not in iparecord.stdout

    - name: create new record for {{ item }}
      shell: echo {{ hostvars['ipa'].ipa_admin_password }} | kinit admin && ipa dnsrecord-add {{ hailstorm_dns_domain }} {{ item }} --a-rec={{ ip }}
      register: iparecord2
      failed_when: iparecord2.rc != 0 and 'no modifications to be performed' not in iparecord2.stderr
      when: iparecord.rc != 0 or ip not in iparecord.stdout
  delegate_to: ipa

- set_fact:
    temp_external_ip: "{{ (external_network_config.services_network_dnat.mapping|selectattr('expose_machine','match','^' + name + '$')|first).on_host_prefix|ipaddr('address') }}"
  when: mode=='update_services_network' and external_network_config is defined and external_network_config.services_network_dnat.mapping|selectattr('expose_machine','match','^' + name + '$')|list|length > 0


### DNAT DNS
- block:
    - name: add or change {{ item }} in zone db
      lineinfile:
        dest: "/etc/named/zones/master/db.{{ hailstorm_dns_domain }}"
        line: "{{ item }} 				IN      A       {{ temp_external_ip|default(ip) }}"
        regexp: "^{{ item|regex_escape }}\\s+.*"
      when: >
        mode != 'update_services_network' or
        (external_network_config is defined and
        external_network_config.services_network_dnat.mapping|selectattr('expose_machine','match','^' + name + '$')|list|length > 0)
      notify:
        - create serial
        - update serial
        - restart named
  when: not skip_dnat_dns|default(false,true)
  delegate_to: infrastructure


### AD DNS
# work around the problem that delegating to AD does not seem to pull the proper variables from the AD host
- set_fact:
    ansible_winrm_server_cert_validation: ignore
- name: test if AD is available for remote management
  shell: curl -k --connect-timeout 1 https://{{ hostvars[groups['primary-ad-group'][0]].ansible_host }}:5986
  register: ad_available
  changed_when: false
  #delegate_to: "{{ infrastructure_delegate_host_used_to_test_if_layer2_host_is_available }}"
  # need to delegate to localhost since winrm cannot use jump host
  delegate_to: localhost
  failed_when: false

- block:
    # - name: query A record in AD DNS
    #   set_fact: existing_a_record="{{ lookup('dig', hostname + ' @' + hostvars[groups['primary-ad-group'][0]].ansible_host) }}"
    - name: query A record in AD DNS
      win_shell: Get-DnsServerResourceRecord -ZoneName {{ hailstorm_dns_domain }} -Name {{ item }} -RRType A | %{$_.RecordData.IPv4Address.IPAddressToString}
      failed_when: false
      changed_when: false
      register: temp_existing_a_record

    - set_fact:
        existing_a_record: 'NXDOMAIN'
      when: temp_existing_a_record.rc != 0

    - set_fact:
        existing_a_record: "{{ temp_existing_a_record.stdout_lines|map('trim')|list }}"
      when: temp_existing_a_record.rc == 0

    - debug: var=existing_a_record

    # - debug: var=item
    # - debug: var=existing_a_record

    # - name: remove existing A record
    #   win_shell: Remove-DnsServerResourceRecord -ZoneName {{ hailstorm_dns_domain }} -Name {{ item }} -RRType A -Force
    #   when: existing_a_record != 'NXDOMAIN' and existing_a_record != temp_external_ip|default(ip)

    - name: create A record in AD DNS
      win_shell: Add-DNSServerResourceRecordA -ZoneName {{ hailstorm_dns_domain }} -Name {{ item }} -IPv4Address {{ temp_external_ip|default(ip) }}
      when: existing_a_record == 'NXDOMAIN' or temp_external_ip|default(ip) not in existing_a_record

    - set_fact:
        temp_ptr_record: "{{ temp_external_ip|default(ip)|ipaddr('revdns') }}"
        temp_reverse_zone_name: "{{ temp_external_ip|default(ip)|ipaddr('revdns')|regex_replace('\\d+\\.(.*)','\\1') }}"
        temp_host_prefix: "{{ temp_external_ip|default(ip) }}/{% if temp_external_ip is defined %}{{ infrastructure_network_guests.default_gw_host_prefix|ipaddr('prefix') }}{% else %}{{ infrastructure_network_services.default_gw_host_prefix|ipaddr('prefix') }}{% endif %}"

    - name: get DNS zones
      win_shell: Get-DnsServerZone | %{$_.ZoneName}
      changed_when: false
      register: ad_existing_zones

    - debug: var=temp_reverse_zone_name
    - debug: var=ad_existing_zones.stdout_lines

    # need to cut off last character since this is a dot that is not returned from AD
    - name: create reverse lookup zone
      win_shell: Add-DnsServerPrimaryZone -Name {{ temp_reverse_zone_name[:-1] }} -ReplicationScope Domain
      when: temp_reverse_zone_name[:-1] not in ad_existing_zones.stdout_lines

    - name: query PTR record in AD DNS
      win_shell: Get-DnsServerResourceRecord -ZoneName {{ temp_reverse_zone_name[:-1]  }} -Name {{ temp_ptr_record }} -RRType PTR | %{$_.RecordData.PtrDomainName}
      failed_when: false
      changed_when: false
      register: temp_existing_ptr_record

    - set_fact:
        temp_existing_ptr_record2: "{{ temp_existing_ptr_record.stdout_lines|map('trim')|list }}"
    # - debug: var=temp_existing_ptr_record2
    # - debug: var=temp_ptr_record

    # need to cut off last character since this is a dot that is actually returned from AD
    - name: delete PTR record in AD DNS
      win_shell: Remove-DnsServerResourceRecord -ZoneName {{ temp_reverse_zone_name }} -Name {{ temp_ptr_record }} -RRType PTR -Force
{'fraction_non_alphanumeric': 0.1224639452456612, 'fraction_numerical': 0.005133219261794182, 'mean_word_length': 3.79453125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2560719', 'n_tokens_mistral': 4024, 'n_tokens_neox': 3944, 'n_words': 951}