# Simple ansible playbook to deploy the latest ingest docker container
---
- name: SSH into hosts and run command
  hosts: '{{ host_group }}'
  user: zoox
  gather_facts: yes
  tasks:

  - name: Print some debug information
    debug:
      msg:
        - "Hostname: {{ ansible_hostname }}"
        - "IP Address: {{ ansible_host }}"
        - "Host Distribution: {{ ansible_distribution }}"
        - "Host Dist version: {{ ansible_distribution_version }}"

  # Remove all the previous Docker containers
  - name: Remove all Docker Images
    command:  docker image prune --all --force

  - name: Pull the Ingest Docker Image
    command: docker pull "{{ docker_image }}"
    register: docker_output
  - debug: var=docker_output.stdout_lines

  # This is a very convoluted way to basically copy a file from a Docker Image.
  #  Docker only supports copying files from Docker Containers, not images, so you first
  #  need to create a container from the Docker image, then copy the file out.
  - name: Pull wrapper shell script from image and put in place
    shell:
      cmd: docker cp $(docker create --rm "{{ docker_image }}" bash):/home/zoox/run_vehicle_data_sync_script.sh /opt/zoox/run_vehicle_data_sync_script.sh

  - name: Remove any stale containers
    command: docker container prune --force
