ansible-collections / community.docker

Community Docker Collection for Ansible: modules and plugins for working with Docker
https://galaxy.ansible.com/ui/repo/published/community/docker/
GNU General Public License v3.0
197 stars 110 forks source link

community.docker.docker_host_info : Error connecting: Error while fetching server API version: Not supported URL scheme http+docker #868

Closed D0wn3r closed 3 months ago

D0wn3r commented 3 months ago
SUMMARY

I'm trying to get docker info through Ansible with the module community.docker.docker_host_info but it doesn't work anymore. It's on a fresh install (fresh ubuntu 22.04 with docker 26.0.X and also tried with 25.0.4)

ISSUE TYPE
COMPONENT NAME
ANSIBLE VERSION
ansible [core 2.15.4]
  config file = /home/simco-dev/PROJECTS/pca/ansible.cfg
  configured module search path = ['/home/simco-dev/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.10/dist-packages/ansible
  ansible collection location = /home/simco-dev/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.10.14 (main, Apr  6 2024, 18:45:05) [GCC 9.4.0] (/usr/bin/python3.10)
  jinja version = 3.1.2
  libyaml = True
COLLECTION VERSION
# /usr/local/lib/python3.10/dist-packages/ansible_collections
Collection        Version
----------------- -------
community.general 7.4.0

# <home_path>/.ansible/collections/ansible_collections
Collection       Version
---------------- -------
community.docker 3.10.2
CONFIGURATION
CONFIG_FILE() = <path>/ansible.cfg
DEFAULT_ROLES_PATH(<path>/ansible.cfg) = ['<path>/ansible-common-roles']
HOST_KEY_CHECKING(<path>/ansible.cfg) = False
PAGER(env: PAGER) = less
OS / ENVIRONMENT

Fresh instance ubuntu 22.04 from the cloud provider Scaleway

STEPS TO REPRODUCE

with docker_version: "5:25.0.4-1~ubuntu.20.04~focal"

---
- name: Install required system packages
  ansible.builtin.apt:
      pkg:
          - ca-certificates=20230311ubuntu0.22.04.1
          - curl=7.81.0-1ubuntu1.16
          - python3-pip=22.0.2+dfsg-1ubuntu0.4
          - gnupg=2.2.27-3ubuntu2.1
      state: present
      update_cache: true
  become: true

- name: Add Docker GPG apt Key
  ansible.builtin.apt_key:
      url: https://download.docker.com/linux/ubuntu/gpg
      state: present
  become: true

- name: Add Docker Repository
  ansible.builtin.apt_repository:
      repo: deb https://download.docker.com/linux/ubuntu focal stable
      state: present
  become: true

- name: Update apt and install docker-ce
  ansible.builtin.apt:
      name:
          - "docker-ce={{ docker_version }}"
          - "docker-ce-cli={{ docker_version }}"
      state: present
      allow_downgrade: true
      force: true
  become: true

- name: Install SDK docker and jsondiff (python)
  ansible.builtin.pip:
      name:
          - docker
          - jsondiff
      state: present
  become: true

- name: Check if Swarm cluster exists
  become: true
  community.docker.docker_host_info:
  register: docker_info
EXPECTED RESULTS

I expect to get docker info

ACTUAL RESULTS

This error happened on my 3 servers

The full traceback is:
  File "/tmp/ansible_community.docker.docker_host_info_payload_316ufw33/ansible_community.docker.docker_host_info_payload.zip/ansible_collections/community/docker/plugins/module_utils/common_api.py", line 117, in __init__
    super(AnsibleDockerClientBase, self).__init__(**self._connect_params)
  File "/tmp/ansible_community.docker.docker_host_info_payload_316ufw33/ansible_community.docker.docker_host_info_payload.zip/ansible_collections/community/docker/plugins/module_utils/_api/api/client.py", line 188, in __init__
    self._version = self._retrieve_server_version()
  File "/tmp/ansible_community.docker.docker_host_info_payload_316ufw33/ansible_community.docker.docker_host_info_payload.zip/ansible_collections/community/docker/plugins/module_utils/_api/api/client.py", line 212, in _retrieve_server_version
    raise DockerException(
fatal: [swarm-01]: FAILED! => {
    "can_talk_to_docker": false,
    "changed": false,
    "invocation": {
        "module_args": {
            "api_version": "auto",
            "ca_cert": null,
            "client_cert": null,
            "client_key": null,
            "containers": false,
            "containers_all": false,
            "containers_filters": null,
            "debug": false,
            "disk_usage": false,
            "docker_host": "unix://var/run/docker.sock",
            "images": false,
            "images_filters": null,
            "networks": false,
            "networks_filters": null,
            "ssl_version": null,
            "timeout": 60,
            "tls": false,
            "tls_hostname": null,
            "use_ssh_client": false,
            "validate_certs": false,
            "verbose_output": false,
            "volumes": false,
            "volumes_filters": null
        }
    },
    "msg": "Error connecting: Error while fetching server API version: Not supported URL scheme http+docker"
}
D0wn3r commented 3 months ago

Maybe this issue is because of the requests upgrade https://github.com/docker/docker-py/issues/3256

D0wn3r commented 3 months ago

if I force requests package version 2.31.0, it works

felixfontein commented 3 months ago

I guess you are using community.docker before 3.10.2. (You only provided the community.general version.) Either upgrade community.docker to the latest version (it fixed this issue yesterday), or stick to requests < 2.32.0.

Also please consider searching for similar error messages in the repository before creating a new issue the next time. You would have found two closed issues.

D0wn3r commented 3 months ago

I guess you are using community.docker before 3.10.2. (You only provided the community.general version.) Either upgrade community.docker to the latest version (it fixed this issue yesterday), or stick to requests < 2.32.0.

Also please consider searching for similar error messages in the repository before creating a new issue the next time. You would have found two closed issues.

# <path>/.ansible/collections/ansible_collections
Collection       Version
---------------- -------
community.docker 3.10.2

I'm using the latest version

timon-michel-scopevisio commented 3 months ago

Same here, i just did a completely fresh install and this issue appears. community.docker is on the same version: 3.10.2

I did, however, notice that i am using community.docker.docker_compose, which is deprecated (without deprecation warning on the docs), and i am not getting the same error on community.docker.docker_compose_v2.

Interestingly this issue is indexed by google, whereas the other two you are referring to are not

felixfontein commented 3 months ago

I did, however, notice that i am using community.docker.docker_compose, which is deprecated (without deprecation warning on the docs),

Apparently the latest Ansible docs weren't rebuilt for the Ansible 9.6.0 release. The docs here: https://docs.ansible.com/ansible/devel/collections/community/docker/docker_compose_module.html#deprecated and here: https://ansible-collections.github.io/community.docker/branch/main/docker_compose_module.html#deprecated show that warning.

and i am not getting the same error on community.docker.docker_compose_v2.

Which is not surprising, since that module calls the docker compose CLI tool. No Python requests or Docker SDK for Python involved.

felixfontein commented 3 months ago

I guess you are using community.docker before 3.10.2. (You only provided the community.general version.) Either upgrade community.docker to the latest version (it fixed this issue yesterday), or stick to requests < 2.32.0. Also please consider searching for similar error messages in the repository before creating a new issue the next time. You would have found two closed issues.

# <path>/.ansible/collections/ansible_collections
Collection       Version
---------------- -------
community.docker 3.10.2

I'm using the latest version

In that case I have no idea what's going wrong in your case, since everything works both fine in CI and on my local machine with the latest requests.

Are you using a special way to talk to the Docker daemon, or are you using the default way (default Unix socket)?

felixfontein commented 3 months ago

@D0wn3r just to make sure, are you sure that you got the error with docker_host_info on community.docker 3.10.2? Or did you got it with some other modules, for example docker_swarm or dockerswarm*?

felixfontein commented 3 months ago

@timon-michel-scopevisio also note that docker_compose requires Docker SDK for Python < 7.0.0 (it will crash and burn with 7.0.0), and that will never receive a fix for requests 2.32.x. If you want to stick to using docker-compose v1 (that has been End of Life for several years now) with the docker_compose module, you have to make sure you use requests < 2.32.0.

Better use docker_compose_v2 though :)

felixfontein commented 3 months ago

I did, however, notice that i am using community.docker.docker_compose, which is deprecated (without deprecation warning on the docs),

FYI, the Ansible 9 docsite has been rebuilt, now it's also in the latest docs: https://docs.ansible.com/ansible/latest/collections/community/docker/docker_compose_module.html#deprecated

D0wn3r commented 3 months ago

@D0wn3r just to make sure, are you sure that you got the error with docker_host_info on community.docker 3.10.2? Or did you got it with some other modules, for example docker_swarm or dockerswarm*?

@felixfontein I will try but in my role, I launch a docker_host_info before any other docker_swarm*

D0wn3r commented 3 months ago

I guess you are using community.docker before 3.10.2. (You only provided the community.general version.) Either upgrade community.docker to the latest version (it fixed this issue yesterday), or stick to requests < 2.32.0. Also please consider searching for similar error messages in the repository before creating a new issue the next time. You would have found two closed issues.

# <path>/.ansible/collections/ansible_collections
Collection       Version
---------------- -------
community.docker 3.10.2

I'm using the latest version

In that case I have no idea what's going wrong in your case, since everything works both fine in CI and on my local machine with the latest requests.

Are you using a special way to talk to the Docker daemon, or are you using the default way (default Unix socket)?

I have nothing special. It's a fresh install. I will try more tests. I expected to fall on people that have the same issue

D0wn3r commented 3 months ago

I tried again today and it works like a charm ... I close this issue so I don't know what did change since my last test. I'm not able to make it bugs

Hennns commented 3 months ago

I suspect this is related to the latest patch release of requests, 2.32.2.

I've tested with the following: (Using both docker_host_info and docker_container, but the results are the same for both)

ansible [core 2.16.7] config file = None configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/local/lib/python3.11/site-packages/ansible ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections executable location = /usr/local/bin/ansible python version = 3.11.7 (/usr/bin/python3.11) jinja version = 3.1.4 libyaml = True

ansible-galaxy collection list ... community.docker 3.10.1 community.general 8.6.1 ...

On the managed node: pip list installed | grep docker docker 7.1.0

felixfontein commented 3 months ago

community.docker 3.10.1 only works with requests up to 2.32.1, it does not work with requests 2.32.2 and further. You need community.docker 3.10.2 for that.

Hennns commented 3 months ago

Ahh, I was just installing everything with (basically) 'pip install ansible==9.6.0 ', which of course has 3.10.1 of community.docker. Then like an idiot I assumed I was using the latest and greatest.

Thank you for the clarification! :)

felixfontein commented 3 months ago

You unfortunately have to wait for Ansible 9.7.0 then, which should be there in a bit less than 3 weeks. Or wait for Ansible 10.0.0, which is likely out next Tuesday (assuming that no blockers show up). Ansible 10.0.0rc1 (released two days ago) already has community.docker 3.10.3.

mrsiesta commented 3 months ago

Verified updating the community.docker version resolved the issue for me.

In case someone is wondering how to do that: ansible-galaxy collection install community.docker

felixfontein commented 3 months ago

Ansible 10.0.0 is out now btw (uploaded to PyPI 33 minutes ago =) ).

Valeronlol commented 3 months ago

It looks like it was fixed in community.docker:3.10.2, current stable version is 3.10.3. So we just need to upgrade community.docker version to latest, like that: ansible-galaxy collection install community.docker --upgrade

bbezak commented 3 months ago

Ansible 10.0.0 is out now btw (uploaded to PyPI 33 minutes ago =) ).

Should we also update to 3.10.3 in Ansible 9 ? - https://github.com/ansible-community/ansible-build-data/blob/main/9/ansible-9.6.1.yaml#L89-L91 https://github.com/ansible-community/ansible-build-data/blob/main/9/ansible-9.build#L33

felixfontein commented 3 months ago

@bbezak the next regular Ansible 9 release (should be 9.7.0, and should be released in ~one week) will include it. Ansible 9.6.1 has been a hotfix for 9.6.0 to remove suspicious Windows binaries that were accidentally included in two collections. There was no other change in 9.6.1 compared to 9.6.0.