#!/usr/bin/ansible-playbook
---
- hosts: localhost
  gather_facts: yes
  vars:
    origin_dns:
      file: /etc/dnsmasq.d/origin-dns.conf
      lines:
      - regex: ^listen-address
        state: present
        line: "listen-address={{ ansible_default_ipv4.address }}"
    node_dns:
      file: /etc/dnsmasq.d/node-dnsmasq.conf
      lines:
      - regex: "^server=/in-addr.arpa/127.0.0.1$"
        line: server=/in-addr.arpa/127.0.0.1
      - regex: "^server=/cluster.local/127.0.0.1$"
        line: server=/cluster.local/127.0.0.1

  tasks:
  - include_vars: openshift_settings.yaml

  - name: set the data for node_dns
    lineinfile:
      create: yes
      insertafter: EOF
      path: "{{ node_dns.file }}"
      regexp: "{{ item.regex }}"
      line: "{{ item.line | default(omit) }}"
    with_items: "{{ node_dns.lines }}"

  - name: set the data for origin_dns
    lineinfile:
      create: yes
      state: "{{ item.state | default('present') }}"
      insertafter: "{{ item.after | default(omit) }}"
      path: "{{ origin_dns.file }}"
      regexp: "{{ item.regex }}"
      line: "{{ item.line | default(omit)}}"
    with_items: "{{ origin_dns.lines }}"

  - when:
    - openshift_group_type is defined
    - openshift_group_type != ''
    - openshift_group_type != 'master'
    block:
    - name: determine the openshift_service_type
      stat:
        path: /etc/sysconfig/atomic-openshift-node
      register: service_type_results

    - name: set openshift_service_type fact based on stat results
      set_fact:
        openshift_service_type: "{{ service_type_results.stat.exists | ternary('atomic-openshift', 'origin') }}"

    - name: update the sysconfig to have necessary variables
      lineinfile:
        dest: "/etc/sysconfig/{{ openshift_service_type }}-node"
        line: "{{ item.line }}"
        regexp: "{{ item.regexp }}"
      with_items:
      - line: "BOOTSTRAP_CONFIG_NAME=node-config-{{ openshift_group_type }}"
        regexp: "^BOOTSTRAP_CONFIG_NAME=.*"
