---
- file:
    dest: /etc/docker-compose/nginx-proxy
    state: directory
  name: create directory for nginx-proxy docker-compose
  become: yes

- template:
    src=templates/docker-compose.yml dest=/etc/docker-compose/nginx-proxy/
  name: nginx-proxy docker-compose.yml
  become: yes

- template:
    src=templates/proxy.conf dest=/etc/docker-compose/nginx-proxy/
  name: nginx-proxy proxy.conf
  become: yes

- file:
    path: /etc/docker-compose/nginx-proxy/certs
    state: directory
    mode: 0755
  name: create certs directory
  become: yes

- copy:
    src: templates/zooxlabs_wildcard.crt
    dest: /etc/docker-compose/nginx-proxy/certs/zooxlabs.com.crt
  name: install SSL certificate
  become: yes

- copy:
    content: "{{ lookup('hashi_vault', 'secret=secret/cert/zooxlabs_wildcard').private_key }}"
    dest: /etc/docker-compose/nginx-proxy/certs/zooxlabs.com.key
  name: install SSL private key
  become: yes

# Ansible 2.2 pull flag in docker_service still doesn't work as expected if
# any version of the image isn't present on the host
# https://github.com/ansible/ansible/issues/20458
- command: docker-compose pull chdir=/etc/docker-compose/nginx-proxy
  name: pull nginx-proxy image
  register: pull_result
  changed_when: "'Downloaded newer image' in pull_result.stdout"

- docker_service:
    project_src: /etc/docker-compose/nginx-proxy
    state: present
  name: run nginx-proxy
