gpt4 book ai didi

python - 使用ansible和python3在ubuntu上安装docker

转载 作者:行者123 更新时间:2023-12-01 00:35:22 25 4
gpt4 key购买 nike

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

环境:
- 本地/ Controller 服务器: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

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

但是 ansible 的 docker_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. Please read module documentation and install in the appropriate location, for example via pip install docker or pip install docker-py (Python 2.6). The error was: No module named 'docker'

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

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

最佳答案

问题是 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

关于python - 使用ansible和python3在ubuntu上安装docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57821778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com