{% raw -%}
#!/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 }}"

  tasks:
  - include_vars: openshift_settings.yaml

  - 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_node_config_name is defined
    - openshift_node_config_name != ''
    block:
    - name: determine the openshift_service_type
      stat:
        path: /etc/sysconfig/atomic-openshift-node
        get_checksum: false
        get_attributes: false
        get_mime: false
      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={{ openshift_node_config_name }}"
        regexp: "^BOOTSTRAP_CONFIG_NAME=.*"
{% endraw %}

{% if openshift_cloudprovider_kind | default('') == 'aws' %}
  # need to update aws.conf file if the instance has come up in a new region
  - name: set up aws.conf
    block:
    - name: get current AZ
      uri:
        url: http://169.254.169.254/latest/meta-data/placement/availability-zone
        return_content: yes
      register: aws_out

    - name: set AZ in aws.conf
      ini_file:
        path: /etc/origin/cloudprovider/aws.conf
        section: Global
        option: Zone
        value: "{% raw %}{{ aws_out.content }}{% endraw %}"
{% endif %}
