lonix
lonix

Reputation: 21021

Install docker on ubuntu using ansible with python3

I want to install docker on an ubuntu server, using ansible.

Environment:
- local/controller server: ansible 2.8.4
- remote server: ubuntu 18.04, which comes with 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. During provisioning, something added python2 as a dependency, so now both 2 and 3 are installed. So I updated ansible.cfg to use python3: interpreter_python = /usr/bin/python3.

But ansible's docker_image module fails with:

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'

To confirm whether it's installed, I ran pip3 list which showed docker (4.0.2).

There have been many breaking changes to ansible over the years, so info on this topic is outdated. What should I do?

Upvotes: 6

Views: 9548

Answers (3)

lonix
lonix

Reputation: 21021

Problem was privilege issues for pip - not obvious if you're not a python user, and is poorly documented.

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

Upvotes: 5

sorin
sorin

Reputation: 170698

To use Ansible docker modules you need to install from PyPA the “docker” module in the same python interpreter as ansible. Remember that Ansible modules do not implement all features from docker module. 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).

Upvotes: 0

SerialEnabler
SerialEnabler

Reputation: 1130

Have you tried just using shell to run the docker commands? Sometimes the Ansible modules aren't always up to date and doing a raw command may work better for you in this case.

Upvotes: 0

Related Questions