---

- hosts: all
  pre_tasks:
    - name: Set project path fact
      set_fact:
        tripleo_ansible_project_path: "{{ ansible_user_dir }}/{{ zuul.projects['opendev.org/openstack/tripleo-ansible'].src_dir }}"

    - name: Set action plugin path fact
      set_fact:
        tripleo_action_plugins_paths:
          - "{{ tripleo_ansible_project_path }}/tripleo_ansible/roles.galaxy/config_template/action"
          - "{{ tripleo_ansible_project_path }}/tripleo_ansible/ansible_plugins/action"
          - "/usr/share/ansible/plugins/action"

    - name: Ensure output dirs
      file:
        path: "{{ ansible_user_dir }}/zuul-output/logs"
        state: directory

    - name: Setup bindep
      pip:
        name: "bindep"
        virtualenv: "{{ ansible_user_dir }}/test-python"
        virtualenv_site_packages: true

    - name: Run bindep
      shell: |-
        . {{ ansible_user_dir }}/test-python/bin/activate
        {{ tripleo_ansible_project_path }}/scripts/bindep-install
      become: true

    - name: Setup test-python
      pip:
        requirements: "{{ tripleo_ansible_project_path }}/molecule-requirements.txt"
        virtualenv: "{{ ansible_user_dir }}/test-python"
        virtualenv_site_packages: true

    - name: Display test-python virtualenv package versions
      shell: |-
        . {{ ansible_user_dir }}/test-python/bin/activate
        pip freeze

    # NOTE(cloudnull): This is being done because docker is not supported on RHEL-8
    #                  and tests within this repo still require docker.
    - name: Manual install container.io
      become: true
      package:
        name: https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
      when:
        - (ansible_os_family | lower) == "redhat"
        - (ansible_distribution_major_version | int) >= 8
  tasks:
    - name: Get Ansible Galaxy roles
      command: >-
        {{ ansible_user_dir }}/test-python/bin/ansible-galaxy install
        -fr
        {{ tripleo_ansible_project_path }}/tripleo_ansible/ansible-role-requirements.yml
      environment:
        ANSIBLE_ROLES_PATH: "{{ tripleo_ansible_project_path }}/tripleo_ansible/roles.galaxy"
  roles:
    - role: install-docker
      when:
        - (ansible_distribution_major_version | int) < 8
  post_tasks:
    - name: Run docker vfs setup
      shell: |-
        . {{ ansible_user_dir }}/test-python/bin/activate
        . {{ tripleo_ansible_project_path }}/ansible-test-env.rc
        ansible-playbook -i 'localhost,' \
                         -e tripleo_docker_enable_vfs={{ tripleo_docker_enable_vfs }} \
                         docker-vfs-setup.yml
      args:
        chdir: "{{ tripleo_ansible_project_path }}/tripleo_ansible/playbooks"
        executable: /bin/bash
      environment:
        ANSIBLE_ACTION_PLUGINS: "{{ tripleo_action_plugins_paths | join(':') }}"
      when:
        - tripleo_docker_enable_vfs is defined
        - (ansible_distribution_major_version | int) < 8