使用ansible和python3在ubuntu上安装docker

lon*_*nix 6 python ubuntu ansible docker

我想使用 ansible 在 ubuntu 服务器上安装 docker。

环境:
- 本地/控制器服务器:ansible 2.8.4
- 远程服务器:ubuntu 18.04,自带python 3.6.7

剧本:

##### provision brand new ubuntu 18.04 server
# ...

##### setup docker
- name: install packages required by docker
  apt:
    update_cache: yes
    state: latest
    name:
    - apt-transport-https
    - ca-certificates
    - curl
    - gpg-agent
    - software-properties-common

- name: add docker GPG key
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
    state: present

- name: add docker apt repo
  apt_repository:
    repo: deb https://download.docker.com/linux/ubuntu bionic stable
    state: present

- name: install docker
  apt:
    update_cache: yes
    state: latest
    name:
    - docker-ce
    - docker-ce-cli
    - containerd.io

##### setup ansible <---> docker

- apt:
    update_cache: yes
    state: latest
    name: python3-pip

- pip:
    name: docker

##### test

- docker_image:
    name: hello-world
    source: pull
- docker_container:
    name: hello-world
    state: started
Run Code Online (Sandbox Code Playgroud)

请注意,ubuntu 18.04 仅附带 python3。在配置过程中,添加了 python2 作为依赖项,因此现在 2 和 3 都已安装。所以我更新ansible.cfg为使用 python3: interpreter_python = /usr/bin/python3

但是 ansible 的docker_image模块失败了:

无法在主机的 Python /usr/bin/python3 上导入所需的 Python 库(适用于 Python 的 Docker SDK:docker (Python >= 2.7) 或 docker-py (Python 2.6))。请阅读模块文档并安装在适当的位置,例如通过pip install dockerpip install docker-py(Python 2.6)。错误是:没有名为“docker”的模块

为了确认它是否已安装,我运行了pip3 list显示docker (4.0.2).

多年来,ansible 发生了许多重大变化,因此有关此主题的信息已经过时。我应该怎么办?

lon*_*nix 5

问题是权限问题pip- 如果您不是 python 用户,则不明显,并且记录很少。

这有效:

##### provision brand new ubuntu 18.04 server

# ...

##### setup group and user

- name: create docker group
  become: true
  group:
    name: docker
    state: present

- name: add user to group 
  become: true
  user:
    name: "{{ansible_user}}"
    groups: docker
    append: true

- meta: reset_connection                # <--- must do this if using pipelining

##### setup docker

- name: install packages required by docker
  become: true
  apt:
    update_cache: yes
    state: latest
    name:
    - apt-transport-https
    - ca-certificates
    - curl
    - gpg-agent
    - software-properties-common

- name: add docker GPG key
  become: true
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
    state: present

- name: add docker apt repo
  become: true
  apt_repository:
    repo: deb https://download.docker.com/linux/ubuntu bionic stable
    state: present

- name: install docker
  become: true
  apt:
    update_cache: yes
    state: latest
    name:
    - docker-ce
    - docker-ce-cli
    - containerd.io

##### setup ansible <---> docker

- name: install python dependencies
  become: true
  apt:
    update_cache: yes
    state: latest
    name: python3-pip

- name: install 'Docker SDK for Python'
  #become: true               <--- DO NOT DO THIS!!!
  pip:
    name: docker

##### test

- docker_image:
    name: hello-world
    source: pull
- docker_container:
    name: hello-world
    state: started
Run Code Online (Sandbox Code Playgroud)