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

- name: Ensure EPEL repo is installed
  yum:
    name: epel-release
    state: installed
  become: True
  when:
    - ansible_os_family == 'RedHat'
    - kolla_ansible_install_epel | bool

- name: Ensure required packages are installed
  package:
    name: "{{ item }}"
    state: installed
  become: True
  with_items: "{{ kolla_ansible_package_dependencies }}"

- name: Ensure source code checkout parent directory exists
  file:
    path: "{{ kolla_ansible_source_path | dirname }}"
    state: directory
    owner: "{{ ansible_user_uid }}"
    group: "{{ ansible_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_user_uid }}"
    group: "{{ ansible_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 }}"
  with_items:
    - { name: pip }

- name: Ensure python requirements file exists
  template:
    src: requirements.txt.j2
    dest: "{{ kolla_ansible_venv }}/requirements.txt"

- name: Ensure required Python packages are installed
  pip:
    requirements: "{{ kolla_ansible_venv }}/requirements.txt"
    state: latest
    extra_args: "{% if kolla_upper_constraints_file %}-c {{ kolla_upper_constraints_file }}{% endif %}"
    virtualenv: "{{ kolla_ansible_venv }}"

# This is a workaround for the lack of a python package for libselinux-python
# on PyPI. Without using --system-site-packages to create the virtualenv, it
# seems difficult to ensure the selinux python module is available. It is a
# dependency for Ansible when selinux is enabled.
- name: Ensure selinux Python package is linked into the virtualenv
  file:
    src: "/usr/lib64/python2.7/site-packages/selinux"
    dest: "{{ kolla_ansible_venv }}/lib/python2.7/site-packages/selinux"
    state: link
  when:
    - ansible_os_family == 'RedHat'
    - ansible_selinux != False
    - ansible_selinux.status != 'disabled'
