- name: Check if proxy dir exists
  stat:
    path: "{{ DEVORCH_BASE_PATH }}/{{ DEVORCH_HOME_DIR }}/proxy"
  register: haproxy_dir

- name: delete haproxy dir file if exists
  ignore_errors: yes
  file:
    state: absent
    path: "{{ DEVORCH_BASE_PATH }}/{{ DEVORCH_HOME_DIR }}/proxy"
  when: haproxy_dir.stat.exists

- name: "Create proxy directory if not exists"
  file:
    path: "{{ DEVORCH_BASE_PATH }}/{{ DEVORCH_HOME_DIR }}/proxy"
    state: directory

- name: Generate haproxy.cfg from template 
  template:
    src: "{{ playbook_dir }}/templates/haproxy/haproxy.cfg.j2"
    dest: "{{ DEVORCH_BASE_PATH }}/{{ DEVORCH_HOME_DIR }}/proxy/haproxy.cfg"
  delegate_to: localhost

- name: Generate haproxy_map.map from template 
  template:
    src: "{{ playbook_dir }}/templates/haproxy/haproxy_map.map.j2"
    dest: "{{ DEVORCH_BASE_PATH }}/{{ DEVORCH_HOME_DIR }}/proxy/haproxy_map.map"
  delegate_to: localhost

- name: Place server.pem in haproxy config folder.
  copy:
    src: "{{ OPENSSL_CERTS_PATH }}/server.pem"
    dest: "{{ DEVORCH_BASE_PATH }}/{{ DEVORCH_HOME_DIR }}/proxy/wildcard.127.0.0.1.nip.io.pem"
  when: (SETUP_ENV == 'local')
