---
#
# Check if the awx image exists locally. Otherwise we cannot
# push the image to our docker registry
#
- name: check if image {{ awx_image }}:{{ awx_image_tag }} already exist
  docker_image_info:
          name: "{{ awx_image }}:{{ awx_image_tag }}"
  register: awx_image_info

- name: set var awx_image_exists based on awx_image_info
  set_fact:
    awx_image_exists: "{{ awx_image_info.images | length == 1 }}"

- name: debug
  debug:
    var: awx_image_exists

- name: tag latest
  docker_image:
    name: "{{ awx_image }}:{{awx_image_tag }}"
    repository: "{{ awx_image }}:latest"
    force_tag: yes
    source: local
  when: awx_image_exists|bool == True

- name: "push image with docker_image module to artifactory"
  docker_image:
    #repository: "{{ awx_image }}:{{ item }}"
    name: "{{ awx_image }}"
    tag: "{{ item }}"
    push: yes
    source: local
    force_tag: yes
    state: present
  when: awx_image_exists|bool == True
  with_items:
    - "{{ awx_image_tag }}"
    - "latest"

- debug:
    msg: "docker image {{ docker_registry }}/{{ awx_image }}:{{ awx_image_tag | default('latest') }} pushed"
  when: awx_image_exists|bool == True

- debug:
    msg: "docker image {{ awx_image }}:{{ awx_image_tag | default('latest') }} not found locally, set build_docker_image=True to build the image first."
  when: awx_image_exists|bool == False
...
