- name: Deploy Portainer
  hosts: all
  remote_user: root
  pre_tasks:
  - name: "Assert config_dir is provided and not empty"
    assert:
      that:
        - config_dir is not undefined and config_dir != ""

  tasks:

  ##
  - name: "Create config directory"
    file:
      path: "{{ config_dir }}"
      state: directory
      recurse: yes

  - name: Execute docker-compose
    shell: cd "{{ config_dir }}" && docker-compose down
    ignore_errors: true

  - name: "Upload docker-compose template"
    template:
      src: templates/docker-compose-portainer.yml
      dest: "{{ config_dir }}/docker-compose.yml"

  - name: "Upload gen-admin-passwd.sh"
    copy:
      src: portainer/gen-admin-passwd.sh
      dest: "{{ config_dir }}/gen-admin-passwd.sh"
      mode: a+x

  - name: "Upload reset-admin-passwd.sh"
    copy:
      src: portainer/reset-admin-passwd.sh
      dest: "{{ config_dir }}/reset-admin-passwd.sh"
      mode: a+x

  - name: Execute docker-compose
    shell: cd "{{ config_dir }}" && docker-compose up -d
