---
- name: Ensure that all non-node hosts are accessible
  hosts: oo_masters_to_config:oo_etcd_to_config:oo_lb_to_config:oo_nfs_to_config
  any_errors_fatal: true
  tasks:

- name: Initialize host facts
  hosts: oo_all_hosts
  tasks:
  - name: load openshift_facts module
    include_role:
      name: openshift_facts
    static: yes

  # TODO: Should this role be refactored into health_checks??
  - name: Run openshift_sanitize_inventory to set variables
    include_role:
      name: openshift_sanitize_inventory

  - name: Detecting Operating System from ostree_booted
    stat:
      path: /run/ostree-booted
    register: ostree_booted

  # Locally setup containerized facts for now
  - name: initialize_facts set fact l_is_atomic
    set_fact:
      l_is_atomic: "{{ ostree_booted.stat.exists }}"

  - name: initialize_facts set fact for containerized and l_is_*_system_container
    set_fact:
      l_is_containerized: "{{ (l_is_atomic | bool) or (containerized | default(false) | bool) }}"
      l_is_openvswitch_system_container: "{{ (openshift_use_openvswitch_system_container | default(openshift_use_system_containers | default(false)) | bool) }}"
      l_is_node_system_container: "{{ (openshift_use_node_system_container | default(openshift_use_system_containers | default(false)) | bool) }}"
      l_is_master_system_container: "{{ (openshift_use_master_system_container | default(openshift_use_system_containers | default(false)) | bool) }}"
      l_is_etcd_system_container: "{{ (openshift_use_etcd_system_container | default(openshift_use_system_containers | default(false)) | bool) }}"

  - name: initialize_facts set facts for l_any_system_container
    set_fact:
      l_any_system_container: "{{ l_is_etcd_system_container or l_is_openvswitch_system_container or l_is_node_system_container or l_is_master_system_container }}"

  - name: initialize_facts set fact for l_etcd_runtime
    set_fact:
      l_etcd_runtime: "{{ 'runc' if l_is_etcd_system_container else 'docker' if l_is_containerized else 'host' }}"

  # TODO: Should this be moved into health checks??
  # Seems as though any check that happens with a corresponding fail should move into health_checks
  - name: Validate python version - ans_dist is fedora and python is v3
    fail:
      msg: |
        openshift-ansible requires Python 3 for {{ ansible_distribution }};
        For information on enabling Python 3 with Ansible, see https://docs.ansible.com/ansible/python_3_support.html
    when:
    - ansible_distribution == 'Fedora'
    - ansible_python['version']['major'] != 3

  # TODO: Should this be moved into health checks??
  # Seems as though any check that happens with a corresponding fail should move into health_checks
  - name: Validate python version - ans_dist not Fedora and python must be v2
    fail:
      msg: "openshift-ansible requires Python 2 for {{ ansible_distribution }}"
    when:
    - ansible_distribution != 'Fedora'
    - ansible_python['version']['major'] != 2

  # TODO: Should this be moved into health checks??
  # Seems as though any check that happens with a corresponding fail should move into health_checks
  # Fail as early as possible if Atomic and old version of Docker
  - when:
    - l_is_atomic | bool
    block:

    # See https://access.redhat.com/articles/2317361
    # and https://github.com/ansible/ansible/issues/15892
    # NOTE: the "'s can not be removed at this level else the docker command will fail
    # NOTE: When ansible >2.2.1.x is used this can be updated per
    # https://github.com/openshift/openshift-ansible/pull/3475#discussion_r103525121
    - name: Determine Atomic Host Docker Version
      shell: 'CURLY="{"; docker version --format "$CURLY{json .Server.Version}}"'
      register: l_atomic_docker_version

    - name: assert atomic host docker version is 1.12 or later
      assert:
        that:
        - l_atomic_docker_version.stdout | replace('"', '') | version_compare('1.12','>=')
        msg: Installation on Atomic Host requires Docker 1.12 or later. Please upgrade and restart the Atomic Host.

  - when:
    - not l_is_atomic | bool
    block:
    - name: Ensure openshift-ansible installer package deps are installed
      package:
        name: "{{ item }}"
        state: present
      with_items:
      - iproute
      - "{{ 'python3-dbus' if ansible_distribution == 'Fedora' else 'dbus-python' }}"
      - "{{ 'python3-PyYAML' if ansible_distribution == 'Fedora' else 'PyYAML' }}"
      - yum-utils

    - name: Ensure various deps for running system containers are installed
      package:
        name: "{{ item }}"
        state: present
      with_items:
      - atomic
      - ostree
      - runc
      when:
      - l_any_system_container | bool

  - name: Default system_images_registry to a enterprise registry
    set_fact:
      system_images_registry: "registry.access.redhat.com"
    when:
    - system_images_registry is not defined
    - openshift_deployment_type == "openshift-enterprise"

  - name: Default system_images_registry to community registry
    set_fact:
      system_images_registry: "docker.io"
    when:
    - system_images_registry is not defined
    - openshift_deployment_type == "origin"

  - name: Gather Cluster facts and set is_containerized if needed
    openshift_facts:
      role: common
      local_facts:
        deployment_type: "{{ openshift_deployment_type }}"
        deployment_subtype: "{{ openshift_deployment_subtype | default(None) }}"
        cli_image: "{{ osm_image | default(None) }}"
        hostname: "{{ openshift_hostname | default(None) }}"
        ip: "{{ openshift_ip | default(None) }}"
        is_containerized: "{{ l_is_containerized | default(None) }}"
        is_openvswitch_system_container: "{{ l_is_openvswitch_system_container | default(false) }}"
        is_node_system_container: "{{ l_is_node_system_container | default(false) }}"
        is_master_system_container: "{{ l_is_master_system_container | default(false) }}"
        is_etcd_system_container: "{{ l_is_etcd_system_container | default(false) }}"
        etcd_runtime: "{{ l_etcd_runtime }}"
        system_images_registry: "{{ system_images_registry }}"
        public_hostname: "{{ openshift_public_hostname | default(None) }}"
        public_ip: "{{ openshift_public_ip | default(None) }}"
        portal_net: "{{ openshift_portal_net | default(openshift_master_portal_net) | default(None) }}"
        http_proxy: "{{ openshift_http_proxy | default(None) }}"
        https_proxy: "{{ openshift_https_proxy | default(None) }}"
        no_proxy: "{{ openshift_no_proxy | default(None) }}"
        generate_no_proxy_hosts: "{{ openshift_generate_no_proxy_hosts | default(True) }}"

  - name: Set fact of no_proxy_internal_hostnames
    openshift_facts:
      role: common
      local_facts:
        no_proxy_internal_hostnames: "{{ hostvars | oo_select_keys(groups['oo_nodes_to_config']
                                             | union(groups['oo_masters_to_config'])
                                             | union(groups['oo_etcd_to_config'] | default([])))
                                         | oo_collect('openshift.common.hostname') | default([]) | join (',')
                                         }}"
    when:
    - openshift_http_proxy is defined or openshift_https_proxy is defined
    - openshift_generate_no_proxy_hosts | default(True) | bool

  - name: initialize_facts set_fact repoquery command
    set_fact:
      repoquery_cmd: "{{ 'dnf repoquery --latest-limit 1 -d 0' if ansible_pkg_mgr == 'dnf' else 'repoquery --plugins' }}"

  - name: initialize_facts set_fact on openshift_docker_hosted_registry_network
    set_fact:
      openshift_docker_hosted_registry_network: "{{ '' if 'oo_first_master' not in groups else hostvars[groups.oo_first_master.0].openshift.common.portal_net }}"
