---
- hosts: "{{ host|default('all') }}"
  gather_facts: true
  vars:
    ansible_pip_version: '<2.10'
    distro: "{{ ansible_distribution }}{{ ansible_distribution_major_version }}"
  tasks:

    - when: os_setup|default(true)|bool
      become: true
      block:

        - name: Install Python3 and pip
          package:
            name:
              - python3
              - python3-pip
          when:
            - distro != 'CentOS7'
            - setup_python|default(true)|bool

        - name: Install EPEL for CentOS 7
          package:
            name:
              - epel-release
          when:
            - distro == 'CentOS7'
            - setup_python|default(true)|bool

        - name: Install Python and pip for CentOS 7
          package:
            name:
              - python
              - python-pip
          when:
            - distro == 'CentOS7'
            - setup_python|default(true)|bool

        - name: Install Python packaging
          command: pip install --upgrade setuptools packaging pip
          environment:
            PATH: /usr/local/bin:{{ ansible_env.PATH }}

        - name: Install virtualenv
          pip:
            name:
              - virtualenv
          environment:
            PATH: /usr/local/bin:{{ ansible_env.PATH }}
          when: ansible_venv is defined

        - name: Install repositories if need
          include_tasks: install_repos.yml

        - when:
            - distro == 'Ubuntu24'
          block:

            - name: Remove existing container packages
              package:
                name:
                  - podman
                  - buildah
                  - skopeo
                  - crun
                  - golang-github-containers-common
                state: absent

            - name: Run autoremove
              ansible.builtin.apt:
                autoremove: true

        - name: Install podman
          package:
            name:
              - podman
              - buildah
              - skopeo
              - git
              - vim
              - dnsmasq
              - slirp4netns
              - netavark
              - uidmap
              - passt
              - fuse-overlayfs
            state: latest

    - name: Remove virtualenv if need
      file:
        path: "{{ ansible_venv }}"
        state: absent
      when:
        - clean_venv|default(false)|bool
        - ansible_venv is defined

    - name: Install ansible
      pip:
        name: ansible
        version: "{{ ansible_pip_version }}"
        extra_args: --upgrade
        virtualenv: "{{ ansible_venv|default(omit) }}"
        virtualenv_site_packages: "{{ ansible_venv_site_packages|default(omit) }}"
      environment:
        PATH: /usr/local/bin:{{ ansible_env.PATH }}
      when:
        - ansible_venv is defined

    - name: Get podman version
      shell: |
        podman version || true
        podman version
        buildah version
        podman info --debug
        buildah info --debug
        conmon --version
        runc --version
      changed_when: false
