nickjj / ansible-docker

Install / Configure Docker and Docker Compose using Ansible.
MIT License
750 stars 224 forks source link

Error with pip on basic usage #126

Closed marcbria closed 7 months ago

marcbria commented 1 year ago

Hi,

Thanks a lot for sharing this nickjj.

I defined my playbook as follows:

---
- name: Instalar Docker & Docker Compose using ansible-docker role
  hosts: all
  become: yes

  vars:
    docker__channel: ["stable"]
    docker__state: "latest"
    docker__compose_v2_version: ""
    docker__users: ["marc"]

  roles:
    - name: nickjj.docker

I ran it from my laptop to a brand new debian12 and I got the following error:

TASK [nickjj.docker : Install Python packages] ********************************************************************************************************************************************************************
failed: [myserver] (item={'name': 'docker', 'state': 'present'}) => {"ansible_loop_var": "item", "changed": false, "cmd": ["/usr/local/lib/docker/virtualenv/bin/pip3", "install", "docker"], "item": {"name": "docker", "state": "present"}, "msg": "\n:stderr: Traceback (most recent call last):\n  File \"/usr/local/lib/docker/virtualenv/bin/pip3\", line 5, in <module>\n    from pip._internal.cli.main import main\nModuleNotFoundError: No module named 'pip'\n"}
failed: [myserver] (item={'name': 'docker-compose', 'version': '', 'path': '/usr/local/bin/docker-compose', 'src': '/usr/local/lib/docker/virtualenv/bin/docker-compose', 'state': 'absent'}) => {"ansible_loop_var": "item", "changed": false, "cmd": ["/usr/local/lib/docker/virtualenv/bin/pip3", "uninstall", "-y", "docker-compose"], "item": {"name": "docker-compose", "path": "/usr/local/bin/docker-compose", "src": "/usr/local/lib/docker/virtualenv/bin/docker-compose", "state": "absent", "version": ""}, "msg": "\n:stderr: Traceback (most recent call last):\n  File \"/usr/local/lib/docker/virtualenv/bin/pip3\", line 5, in <module>\n    from pip._internal.cli.main import main\nModuleNotFoundError: No module named 'pip'\n"}

So, just for testing, I created an additional playbook to install pip3 as follows:

---
- name: Installing pip.
  hosts: all
  become: yes  # Para ejecutar como superusuario

  tasks:
    - name: Actualizar la caché de paquetes
      apt:
        update_cache: yes
      when: ansible_os_family == 'Debian'  # Si estás en un sistema basado en Debian

    - name: Installing pip
      apt:
        name:
          - python3-pip
        state: present
      when: ansible_os_family == 'Debian'

But after running this new playbook, when I ran docker-install I got the same error. Am I doing something wrong?

Thanks for your help, m.

marcbria commented 7 months ago

Ok. My approach was wrong. :-)

As far as I can see the role allows to define a virtualenv, so you don't need to add additional tasks... just use the features the role offers tot you.

If you got a similar issue, my solution was restarting it all as follows:

  1. Upgrade the role to last version.
  2. Purge any docker / docker-compose and pip reference.
  3. Remove any reference to the docker repository and it's key.
  4. Remove the /usr/local/lib/docker virtualenv folder
  5. Reboot and I installed again with this playbook:
- name: Installs Docker and Docker Compose using the ansible-docker role
  hosts: all
  become: yes

  vars:
    docker__channel: ["stable"]
    docker__state: "latest"
    docker__compose_v2_version: ""
    docker__users: ["{{ ansible_env.SUDO_USER | d('root') }}"]
    # docker__pip_virtualenv: "/usr/local/lib/docker/virtualenv"
    # docker__pip_dependencies:
    #   - "python3-pip"
    #   - "virtualenv"    

  roles:
    - name: nickjj.docker

And now, all worked like a charm.

Thanks @nickjj for your great work!

nickjj commented 7 months ago

No problem, happy to hear it worked for you in the end.

By the way, the latest version of this role uses Docker Compose v2 and doesn't pip install Docker Compose v1 anymore by default, https://github.com/nickjj/ansible-docker?tab=readme-ov-file#installing-docker-compose-v1 has more details.