ansible-docker: Failed to import the required Python library

Given this vagrantfile:

Vagrant.configure("2") do |config|
  config.vm.box = "generic/ubuntu1804"
  config.vm.define "vagrant"

  config.vm.provision "deploy", type: 'ansible' do |ansible|
    ansible.compatibility_mode = "2.0"
    ansible.playbook = "ansible/deploy.yml"
    ansible.groups = {
      "staging" => ["vagrant"]
    }
  end
end

and this playbook:

- name: Deploy
  hosts: vagrant
  tasks:
    - name: Update and upgrade apt packages
      become: yes
      apt:
        upgrade: 'yes'
        update_cache: yes
        cache_valid_time: 3600
        # Reference: https://github.com/ansible/ansible/issues/56832
        force_apt_get: yes 

    - name: Install Docker & Docker compose
      include_role:
        name: "nickjj.docker"
        apply:
          become: yes
          tags: 
            - docker

    - name: Pip install docker for Ansible's docker_* modules
      pip:
        name: 
          - docker
          - "docker-compose"

    - name: Save services
      local_action:
        module: docker_image
        archive_path: /tmp/{{ item }}.tar
        build:
          path: ../{{ item }}
          pull: yes
        name: {{ item }}
        tag: latest
        force_source: yes
        source: build
      with_items:
        - database
        - server
        - client
        - documents

    - name: Upload services
      copy:
        src: /tmp/{{ item }}.tar
        dest: "{{ base_path }}/{{ item }}.tar"
      with_items:
        - database
        - server
        - client
        - documents

    - name: Load services
      become: yes
      docker_image:
        load_path: "{{ base_path }}/{{ item }}.tar"
        name: {{ item }}
        tag: latest
        source: load
      with_items:
        - database
        - server
        - client
        - documents
      vars:
        ansible_python_interpreter: "/usr/bin/env python-docker"

I get:

failed: [vagrant] (item=database) => {"ansible_loop_var": "item", "changed": false, "item": "database", "msg": "Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on ubuntu1804.localdomain's Python /usr/local/bin/python-docker. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter, for example via `pip install docker` or `pip install docker-py` (Python 2.6). The error was: No module named 'docker'"}

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 27 (13 by maintainers)

Most upvoted comments

Hint for everyone else who ran into this, upgrade your locally cached version 😉 ansible-galaxy install -f nickjj.docker

It just fails to connect with connect to host localhost port 2222: Connection refused and also trying to connect to the VM’s IP over port 22 fails too with the same message.

Normally with my Vagrantfiles, I wouldn’t bother with their insecure key and I would copy in my SSH key, so it’s more similar to how it would be on a real server.

But in any case, when I spin up a new DO server I’m unable to import docker in that virtualenv too, and it’s due to the docker pip package not being installed, which is interesting considering the package’s state is set to present and the task showed it was changed.

I wonder if something changed since September 2019 and today with Ansible. I’m going to investigate this further today. I only did a quick test on it yesterday.

By the way, are you using Python 2 or 3 as your Ansible interpreter?