---
- 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

        - name: Install podman
          package:
            name:
              - podman
              - buildah
              - git
              - vim
              - dnsmasq
              - slirp4netns
            state: latest

        - name: Discover slirp4netns version
          shell: slirp4netns -v | grep version | awk {'print $3'}
          register: version
          when:
            - ansible_distribution == 'Ubuntu'
            - ansible_distribution_major_version is version('22', '>=')

        - name: Install newest slirp4netns version
          shell: >-
            curl -o /tmp/slirp4netns --fail -L https://github.com/rootless-containers/slirp4netns/releases/download/v1.2.0/slirp4netns-$(uname -m);
            chmod +x /tmp/slirp4netns;
            mv /tmp/slirp4netns /usr/bin/slirp4netns
          when:
            - ansible_distribution == 'Ubuntu'
            - ansible_distribution_major_version is version('22', '>=')
            - version.stdout is version('1.1.0', '<')

        - name: Discover conmon version
          shell: conmon --version | grep version | awk {'print $3'}
          register: conmon_version
          when:
            - ansible_distribution == 'Ubuntu'
            - ansible_distribution_major_version is version('22', '>=')

        - name: Downgrade broken conmon
          shell: >-
            wget https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_22.04/amd64/conmon_2.1.2~0_amd64.deb -O /tmp/conmon_2.1.2.deb;
            apt install -y /tmp/conmon_2.1.2.deb
          when:
            - ansible_distribution == 'Ubuntu'
            - ansible_distribution_major_version is version('22', '>=')
            - conmon_version.stdout is version('2.1.9', '==')

    - 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
