---
- name: Create DNS and host files from dynamic inventory
  hosts: localhost
  vars:
    lb_ip: "{{hostvars[groups['tag_openshift_lb'].0]['devices']['eth0'].0 }}"
    lb_host: "{{groups['tag_openshift_lb'].0}}"
  tasks:
    - name: Clear existing files
      file:
        path: "../{{ item }}"
        state: absent
      with_items:
        - inventory.nsupdate
        - inventory.hosts
    - name: Recreate files
      file:
        path: "../{{ item }}"
        state: touch
      with_items:
        - inventory.nsupdate
        - inventory.hosts
    ###############################
    # NSUPDATE
    - name: Create NSUPDATE server line
      lineinfile:
        path: ../inventory.nsupdate
        line: "server {{ nsupdate_server }}"
        state: present
      when: nsupdate_server is defined
    ###############################
    # Reverse entries
    - name: Create NSUPDATE reverse pointer entries
      lineinfile:
        path: ../inventory.nsupdate
        line: "update add {{ '.'.join(hostvars[item]['devices']['eth0'].0.split('.')[::-1]) }}.in-addr.arpa 86400 PTR  {{ item }}"
        state: present
      with_items:
        - "{{ groups['tag_openshift_master'] }}"
        - "{{ groups['tag_openshift_infra'] }}"
        - "{{ groups['tag_openshift_node'] }}"
        - "{{ groups['tag_openshift_lb'] }}"
      when: nsupdate_server is defined
    - name: Append show/send lines
      lineinfile:
        path: ../inventory.nsupdate
        line: "show\nsend"
        state: present
      when: nsupdate_server is defined
    - name: Create NSUPDATE entries for public hosted zone
      lineinfile:
        path: ../inventory.nsupdate
        line: "zone {{public_hosted_zone}}"
        state: present
      when: nsupdate_server is defined
    - name: Create NSUPDATE wildcard A entry
      lineinfile:
        path: ../inventory.nsupdate
        line: "update add *.{{ openshift_master_default_subdomain }} 86400 A {{lb_ip}}"
        state: present
      when: nsupdate_server is defined
    - name: Create NSUPDATE public hostname entry
      lineinfile:
        path: ../inventory.nsupdate
        line: "update add {{openshift_master_cluster_public_hostname}} 86400 A {{lb_ip}}"
        state: present
      when: nsupdate_server is defined
    - name: Create NSUPDATE host A entries
      lineinfile:
        path: ../inventory.nsupdate
        line: "update add {{ item }} 86400 A {{hostvars[item]['devices']['eth0'].0}}"
        state: present
      with_items:
        - "{{ groups['tag_openshift_master'] }}"
        - "{{ groups['tag_openshift_infra'] }}"
        - "{{ groups['tag_openshift_node'] }}"
        - "{{ groups['tag_openshift_lb'] }}"
      when: nsupdate_server is defined
    - name: Append show/send lines
      lineinfile:
        path: ../inventory.nsupdate
        line: "show\nsend"
        state: present
      when: nsupdate_server is defined
    ###############################
    # Create HOST entries
    - name: Create HOSTS public hostname
      lineinfile:
        path: ../inventory.hosts
        line: "{{lb_ip}} {{lb_host}} {{openshift_master_cluster_public_hostname}}"
        state: present
    - name: Create HOSTS node entries
      lineinfile:
        path: ../inventory.hosts
        line: "{{hostvars[item]['devices']['eth0'].0}} {{ item }}"
      with_items:
        - "{{ groups['tag_openshift_master'] }}"
        - "{{ groups['tag_openshift_infra'] }}"
        - "{{ groups['tag_openshift_node'] }}"
