---
- name: Registering container name
  set_fact:
    cname: "{{ cname_prefix ~ '-iid' }}"
- name: Registering container name
  set_fact:
    cnames: "{{ cnames + [cname] }}"

- name: Pull images
  docker_image:
    name: "{{ image }}"
    source: pull
  loop:
    - "quay.io/ansible/docker-test-containers:busybox"
    - "quay.io/ansible/docker-test-containers:alpine3.8"
  loop_control:
    loop_var: image

- name: Get image ID of busybox and alpine images
  docker_image_info:
    name:
    - "quay.io/ansible/docker-test-containers:busybox"
    - "quay.io/ansible/docker-test-containers:alpine3.8"
  register: image_info

- assert:
    that:
      - image_info.images | length == 2

- name: Print image IDs
  debug:
    msg: "busybox: {{ image_info.images[0].Id }}; alpine {{ image_info.images[1].Id }}"

- name: Create container with busybox image via ID
  docker_container:
    image: "{{ image_info.images[0].Id }}"
    name: "{{ cname }}"
    state: present
    force_kill: yes
  register: create_1

- name: Create container with busybox image via ID (idempotent)
  docker_container:
    image: "{{ image_info.images[0].Id }}"
    name: "{{ cname }}"
    state: present
    force_kill: yes
  register: create_2

- name: Create container with alpine image via ID
  docker_container:
    image: "{{ image_info.images[1].Id }}"
    name: "{{ cname }}"
    state: present
    force_kill: yes
  register: create_3

- name: Create container with alpine image via ID (idempotent)
  docker_container:
    image: "{{ image_info.images[1].Id }}"
    name: "{{ cname }}"
    state: present
    force_kill: yes
  register: create_4

- name: Untag image
  # Image will not be deleted since the container still uses it
  docker_image:
    name: quay.io/ansible/docker-test-containers:alpine3.8
    force_absent: yes
    state: absent

- name: Create container with alpine image via name (check mode, will pull, same image)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    name: "{{ cname }}"
    state: present
  register: create_5
  check_mode: yes

- name: Create container with alpine image via name (will pull, same image)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    name: "{{ cname }}"
    state: present
  register: create_6

- name: Cleanup
  docker_container:
    name: "{{ cname }}"
    state: absent
    force_kill: yes
  diff: no

- assert:
    that:
      - create_1 is changed
      - create_2 is not changed
      - create_3 is changed
      - create_4 is not changed
      - create_5 is changed
      - create_6 is changed
      - create_6.container.Image == image_info.images[1].Id
      - create_6.container.Id == create_4.container.Id  # make sure container wasn't recreated

- name: set Digests
  set_fact:
    digest_busybox_v1_29: e004c2cc521c95383aebb1fb5893719aa7a8eae2e7a71f316a4410784edb00a9
    digest_busybox_v1_32: ee44b399df993016003bf5466bd3eeb221305e9d0fa831606bc7902d149c775b

- name: Create container with busybox image via old digest
  docker_container:
    image: "quay.io/ansible/docker-test-containers@sha256:{{ digest_busybox_v1_29 }}"
    name: "{{ cname }}"
    state: present
    force_kill: yes
  register: digest_1

- name: Create container with busybox image via old digest (idempotent)
  docker_container:
    image: "quay.io/ansible/docker-test-containers@sha256:{{ digest_busybox_v1_29 }}"
    name: "{{ cname }}"
    state: present
    force_kill: yes
  register: digest_2

- name: Update container with busybox image via new digest
  docker_container:
    image: "quay.io/ansible/docker-test-containers@sha256:{{ digest_busybox_v1_32 }}"
    name: "{{ cname }}"
    state: present
    force_kill: yes
  register: digest_3

- name: Cleanup
  docker_container:
    name: "{{ cname }}"
    state: absent
    force_kill: yes
  diff: no

- assert:
    that:
      - digest_1 is changed
      - digest_2 is not changed
      - digest_3 is changed
