Debian 12 upgrade python3-docker for docker_swarm_module

Hello,

I am writing an ansible role to manage & create a docker swarm cluster. The target servers are debian 12.

Using the reference page : community.docker.docker_swarm module – Manage Swarm cluster — Ansible Community Documentation

I need to set data_path_port to a specific value not the default one.

This is the code :

- name: Initialize swarm
  community.docker.docker_swarm:
    state: present
    data_path_port: "{{ docker_swarm_data_port }}"
  register: swarm_status
  when: inventory_hostname in groups['swarm-mgmt'][0]

it fails stating :

fatal: [server]: FAILED! => {"changed": false, "msg": "Docker SDK for Python version is 5.0.3 (tstsrv01's Python /usr/bin/python3.11). Minimum version required is 6.0.0 to set data_path_port option. Use `pip install --upgrade docker` to upgrade."}`

On debian 12 we have this version available :

root@server01:~# apt policy python3-docker
python3-docker:
  Installé : 5.0.3-1
  Candidat : 5.0.3-1
 Table de version :
 *** 5.0.3-1 500
        500 http://httpredir.debian.org/debian bookworm/main amd64 Packages
        100 /var/lib/dpkg/status

The thing is by default we cannot play with pip on system lelve, we need to use python venv, but in this case how can this works with ansible ?

Thank you for your help

Can you install the Docker SDK for Python in a virtual environment?

This probably won’t help but Debian Trixie is due out in a few weeks and has python3-docker (7.1.0-2).

So I was able to avoid this issue by creating a venv with required lib version & modify the ansible_python_interpretervariable before docker swarm module getting started.

- name: Install python3-venv
  ansible.builtin.package:
    name: python3-venv
    state: present

- name: Create python venv and install packages
  ansible.builtin.pip:
    virtualenv: "{{ docker_swarm_venv_path }}"
    virtualenv_command: "python3 -m venv"
    name:
      - docker

- name: Set python interpreter to use venv
  ansible.builtin.set_fact:
    ansible_python_interpreter: "{{ docker_swarm_venv_path }}/bin/python"

Don’t think this is very elegant, but it just works !

1 Like