簡體   English   中英

使用 ansible 和 python3 在 ubuntu 上安裝 docker

[英]Install docker on ubuntu using ansible with python3

我想使用 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

請注意,ubuntu 18.04 僅附帶 python3。 在配置期間,添加了 python2 作為依賴項,因此現在安裝了 2 和 3。 所以我更新ansible.cfg以使用 python3: interpreter_python = /usr/bin/python3

但是docker_imagedocker_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) pip3 list

多年來,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

您是否嘗試過僅使用 shell 來運行 docker 命令? 有時 Ansible 模塊並不總是最新的,在這種情況下,執行原始命令可能更適合您。

要使用 Ansible docker 模塊,您需要在與 ansible 相同的 Python 解釋器中從 PyPA 安裝“docker”模塊。 請記住,Ansible 模塊並未實現 docker 模塊的所有功能。 例如,我確信我修復了 docker_container 模塊上的一個錯誤,該錯誤將僅包含在 Ansible 2.9(即將推出測試版)中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM