简体   繁体   English

使用 ansible 和 python3 在 ubuntu 上安装 docker

[英]Install docker on ubuntu using ansible with python3

I want to install docker on an ubuntu server, using ansible.我想使用 ansible 在 ubuntu 服务器上安装 docker。

Environment:环境:
- local/controller server: ansible 2.8.4 - 本地/控制器服务器:ansible 2.8.4
- remote server: ubuntu 18.04, which comes with python 3.6.7 - 远程服务器:ubuntu 18.04,python 3.6.7 自带

Playbook:剧本:

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

Note that ubuntu 18.04 comes with python3 only.请注意,ubuntu 18.04 仅附带 python3。 During provisioning, something added python2 as a dependency, so now both 2 and 3 are installed.在配置期间,添加了 python2 作为依赖项,因此现在安装了 2 和 3。 So I updated ansible.cfg to use python3: interpreter_python = /usr/bin/python3 .所以我更新ansible.cfg以使用 python3: interpreter_python = /usr/bin/python3

But ansible's docker_image module fails with:但是docker_imagedocker_image模块失败了:

Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on host's Python /usr/bin/python3.无法在主机的 Python /usr/bin/python3 上导入所需的 Python 库(适用于 Python 的 Docker SDK:docker (Python >= 2.7) 或 docker-py (Python 2.6))。 Please read module documentation and install in the appropriate location, for example via pip install docker or pip install docker-py (Python 2.6).请阅读模块文档并安装在适当的位置,例如通过pip install dockerpip install docker-py (Python 2.6)。 The error was: No module named 'docker'错误是:没有名为“docker”的模块

To confirm whether it's installed, I ran pip3 list which showed docker (4.0.2) .为了确认它是否已安装,我运行了显示pip3 list docker (4.0.2) pip3 list

There have been many breaking changes to ansible over the years, so info on this topic is outdated.多年来,ansible 发生了许多重大变化,因此有关此主题的信息已过时。 What should I do?我该怎么办?

Problem was privilege issues for pip - not obvious if you're not a python user, and is poorly documented.问题是pip权限问题 - 如果您不是 python 用户,则不明显,并且记录不全。

This works:这有效:

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

Have you tried just using shell to run the docker commands?您是否尝试过仅使用 shell 来运行 docker 命令? Sometimes the Ansible modules aren't always up to date and doing a raw command may work better for you in this case.有时 Ansible 模块并不总是最新的,在这种情况下,执行原始命令可能更适合您。

To use Ansible docker modules you need to install from PyPA the “docker” module in the same python interpreter as ansible.要使用 Ansible docker 模块,您需要在与 ansible 相同的 Python 解释器中从 PyPA 安装“docker”模块。 Remember that Ansible modules do not implement all features from docker module.请记住,Ansible 模块并未实现 docker 模块的所有功能。 For example I know for sure that I fixed a bug on docker_container module that will be included only in Ansible 2.9 (beta soon).例如,我确信我修复了 docker_container 模块上的一个错误,该错误将仅包含在 Ansible 2.9(即将推出测试版)中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM