---
- name: Include OS family-specific variables
  include_vars: "{{ ansible_facts.os_family }}.yml"

- name: Ensure EPEL repo is installed
  package:
    name: epel-release
    state: present
  become: True
  when:
    - ansible_facts.os_family == 'RedHat'
    - kolla_ansible_install_epel | bool

- name: Ensure required packages are installed
  package:
    # NOTE(mgoddard): select non-empty packages.
    name: "{{ kolla_ansible_package_dependencies | select | list }}"
    state: present
    cache_valid_time: "{{ apt_cache_valid_time if ansible_facts.os_family == 'Debian' else omit }}"
    update_cache: "{{ True if ansible_facts.os_family == 'Debian' else omit }}"
  become: True

- name: Ensure source code checkout parent directory exists
  file:
    path: "{{ kolla_ansible_source_path | dirname }}"
    state: directory
    owner: "{{ ansible_facts.user_uid }}"
    group: "{{ ansible_facts.user_gid }}"
  become: True
  when: kolla_ansible_ctl_install_type == 'source'

- name: Ensure Kolla Ansible source code checkout exists
  git:
    repo: "{{ kolla_ansible_source_url }}"
    dest: "{{ kolla_ansible_source_path }}"
    version: "{{ kolla_ansible_source_version }}"
  when: kolla_ansible_ctl_install_type == 'source'

- name: Ensure virtualenv parent directory exists
  file:
    path: "{{ kolla_ansible_venv | dirname }}"
    state: directory
    owner: "{{ ansible_facts.user_uid }}"
    group: "{{ ansible_facts.user_gid }}"
  become: True
  when: kolla_ansible_venv is not none

- name: Ensure the latest version of pip is installed
  pip:
    name: "{{ item.name }}"
    state: latest
    virtualenv: "{{ kolla_ansible_venv }}"
    virtualenv_command: "{{ kolla_ansible_venv_python }} -m venv"
  with_items:
    - { name: pip }

- block:
    - name: Gather list of installed Python packages
      pip_package_info:
        clients: "{{ kolla_ansible_pip }}"
      register: pip_packages

    # Upgrading directly from Ansible 2.9 to Ansible 2.10 or from Ansible 2.10
    # to Ansible 4 is known to cause problems. Uninstall Ansible first if its
    # version is lower than 4.0.0. Although 2.10 is allowed by version limits,
    # this is needed even it is present from Wallaby, because we request
    # `state: latest`.
    - name: Uninstall Ansible if an old version is present
      pip:
        name: ansible
        state: absent
        virtualenv: "{{ kolla_ansible_venv }}"
        virtualenv_python: "{{ kolla_ansible_venv_python }}"
      when:
        - "'ansible' in pip_packages.packages[kolla_ansible_pip]"
        - pip_packages.packages[kolla_ansible_pip].ansible[0].version is version('4.0.0', '<')

    - name: Uninstall ansible-base
      pip:
        name: ansible-base
        state: absent
        virtualenv: "{{ kolla_ansible_venv }}"
        virtualenv_python: "{{ kolla_ansible_venv_python }}"
      when: "'ansible-base' in pip_packages.packages[kolla_ansible_pip]"
  vars:
    kolla_ansible_pip: "{{ kolla_ansible_venv }}/bin/pip"

- name: Ensure required Python packages are installed
  vars:
    kolla_ansible_packages:
      # Install Kolla Ansible from source or PyPI.
      - >-
        {% if kolla_ansible_ctl_install_type == 'source' %}
        {{ kolla_ansible_source_path }}
        {% else %}
        kolla-ansible=={{ kolla_openstack_release }}
        {% endif %}
      - "{{ kolla_ansible_venv_ansible }}"
      - selinux
  pip:
    name: "{{ (kolla_ansible_packages + kolla_ansible_venv_extra_requirements) | select | list }}"
    state: latest
    extra_args: "{% if kolla_upper_constraints_file %}-c {{ kolla_upper_constraints_file }}{% endif %}"
    virtualenv: "{{ kolla_ansible_venv }}"
    virtualenv_python: "{{ kolla_ansible_venv_python }}"

- name: Ensure Ansible collections are installed
  command:
    cmd: >-
      ansible-galaxy collection install
      -r {{ kolla_ansible_requirements_yml }}
      -p {{ kolla_ansible_venv }}/share/kolla-ansible/ansible/collections/
