---
#
# Check if there is already a container from the AWX deployment
#
- name: check if containers with prefix {{ awx_prefix }} already exist
  docker_container_info:
    name: "{{ awx_prefix }}_postgres_1"
  register: postgres_container

#
# Check if there is already a volume containing the database from the AWX deployment
#
- name: check if volume with prefix {{ awx_prefix }} already exist
  docker_volume_info:
    name: "{{ awx_prefix }}_awx_db"
  register: postgres_volume

#
# Destroy all related containers from a previous deployment
# if postgres container exists and the awx_force_deployment
# is set to true
#
- name: bring down awx docker-compose project 
  docker_compose:
    project_src: "{{ awx_composedir }}"
    services: postgres
    remove_orphans: true
    state: absent
    files: [ 'docker-compose.yml', 'docker-compose.override.yml' ]
  register: compose_output
  when:
    - postgres_container.exists == True
    - awx_force_deployment|default(False)|bool == True
    - awx_keep_existing|default(True)|bool == False

#
# Destroy the awx_db volume containing the AWX database if
# the awx_force_deployment is set to true
#
- name: cleanup awx_db volume using docker volume rm
  docker_volume:
    name: "{{ awx_prefix }}_awx_db"
    state: absent
  when:
    - awx_force_deployment|default(False)|bool == True
    - awx_keep_existing|default(True)|bool == False

#
# Destroy the all redis_socket volumes. The number is determined
# by the variable 'cluster_node_count' (default=1). Volumes are only destroyed
# if the awx_force_deployment is set to true
#
- name: cleanup redis_socket volumes using docker volume rm
  docker_volume:
    name: "{{ awx_prefix }}_redis_socket_{{ item }}"
    state: absent
  with_sequence: start=1 end={{ cluster_node_count|default(1) }} stride=1
  when:
    - awx_force_deployment|default(False)|bool == True
    - awx_keep_existing|default(True)|bool == False

#
# Destroy the all receptor volumes. The number is determined
# by the variable 'cluster_node_count' (default=1). Volumes are only destroyed
# if the awx_force_deployment is set to true
#
- name: cleanup receptor volumes using docker volume rm
  docker_volume:
    name: "{{ awx_prefix }}_receptor_{{ item }}"
    state: absent
  with_sequence: start=1 end={{ cluster_node_count|default(1) }} stride=1
  when:
    - awx_force_deployment|default(False)|bool == True
    - awx_keep_existing|default(True)|bool == False

#
# run the steps to deploy AWX from a dedicated
# YAML file. This is only performed if no postgres
# container or volume exists. This can be overwritten
# by setting the awx_force_deployment to true
#
- name: deploy awx using awx2_deploy.yml
  include_tasks:
    awx2_deploy.yml
  when:
    - postgres_container.exists == False or
      (awx_force_deployment|default(False)|bool == True)
    - postgres_volume.exists == False or
      (awx_force_deployment|default(False)|bool == True)

#
# write a message if depolyment is skipped
#
- name: information
  debug:
    msg: "AWX Deployment was skipped because either the awx_db volume or a postgres_container exists. You may force deployment by setting awx_force_deployment to true."
  when:
    - (postgres_container.exists == True and awx_force_deployment|default(False)|bool == False) or
      (postgres_volume.exists == True and awx_force_deployment|default(False)|bool == False)
...
