ansible +docker

I am trying to run a play book from host(ansible VM) to pull an image from docker host, I tried pip install docker still it not working, can you please help.

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 request.exceptions")

Can you check the ansible version on your server and let us know

thanks for getting back Rohith, its 2.8.5

I am trying to run a play book from host(ansible VM) to pull an image from docker host, I tried pip install docker still
it not working, can you please help.

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 request.exceptions")

Try to set the following variable to make sure Ansible uses Python3 on the target host:

ansible_python_interpreter: /usr/bin/python3

Regards
         Racke

Hi Stefan

I tried installing docker using pip command… pip install docker

and then when I tried to run the below command it is not recognising the docker that’s the issue.

ansible-playbook pull_image.yml -u *** -e ‘ansible_python_interpreter=/usr/bin/python’

Using cached https://files.pythonhosted.org/packages/cc/ca/699d4754a932787ef353a157ada74efd1ceb6d1fc0bfb7989ae1e7b33111/docker-4.1.0-py2.py3-none-any.whl
Requirement already satisfied: requests!=2.18.0,>=2.14.2 in /usr/lib/python2.7/site-packages (from docker) (2.22.0)
Requirement already satisfied: websocket-client>=0.32.0 in /usr/lib/python2.7/site-packages (from docker) (0.56.0)
Requirement already satisfied: ipaddress>=1.0.16; python_version < “3.3” in /usr/lib/python2.7/site-packages (from docker) (1.0.22)
Requirement already satisfied: backports.ssl-match-hostname>=3.5; python_version < “3.5” in /usr/lib/python2.7/site-packages (from docker) (3.7.0.1)
Requirement already satisfied: six>=1.4.0 in /usr/lib/python2.7/site-packages (from docker) (1.12.0)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/lib/python2.7/site-packages (from requests!=2.18.0,>=2.14.2->docker) (3.0.4)
Requirement already satisfied: idna<2.9,>=2.5 in /usr/lib/python2.7/site-packages (from requests!=2.18.0,>=2.14.2->docker) (2.8)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/lib/python2.7/site-packages (from requests!=2.18.0,>=2.14.2->docker) (1.25.6)
Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python2.7/site-packages (from requests!=2.18.0,>=2.14.2->docker) (2019.9.11)
Installing collected packages: docker
Successfully installed docker-4.1.0

Hi Stefan

I tried installing docker using pip command... pip install docker

and then when I tried to run the below command it is not recognising the docker that's the issue.

ansible-playbook pull_image.yml -u *** -e 'ansible_python_interpreter=/usr/bin/python'

Please share the contents of pull_image.yml, the actual error messages and the OS details of the target.

Regards
          Racke

Stefen,

I will do that…why cant I find python2.7 under /usr/bin

but I could see under /usr/lib…

Here is the error message

FAILED ! >= {“changed” , false ,msg failed to import docker-py - no module named exceptions , try pip install docker}

pull_image.yml

  • name: pull image
    hosts:TestVM
    tasks:
  • name: build docker image
    docker_image:
    name: xyz
    path: /var/dockertest

Stefen,

I will do that..why cant I find python2.7 under /usr/bin

Hello Sunitha,

you didn't report the OS distribution and version of the target. This is crucial
information.

Regards
          Racke

Sorry Stefen missed it, oracle linux server 7.6, RHEL7

i have the same error

`

bash-4.4# ansible-playbook 04-loadbalancer.yml

PLAY [master] **********************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [master01]

TASK [loadbalancer : make lb dir] **************************************************************************************************************************************************************************
ok: [master01]

TASK [loadbalancer : generate lb config] *******************************************************************************************************************************************************************
ok: [master01]

TASK [loadbalancer : copy lb images] ***********************************************************************************************************************************************************************
changed: [master01]

TASK [loadbalancer : load lb image] ************************************************************************************************************************************************************************
fatal: [master01]: FAILED! => {“changed”: false, “msg”: “Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on localhost.localdomain’s Python /usr/bin/python. 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 requests.exceptions”}

PLAY RECAP *************************************************************************************************************************************************************************************************
master01 : ok=4 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

`

master01 is centos 7.7

i install docker 18.09.9 with binary at master01

ansible server version:

`

bash-4.4# ansible --version
ansible 2.9.2
config file = None
configured module search path = [‘/root/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.9 (default, Oct 17 2019, 11:10:22) [GCC 8.3.0]

`

i change my task to shell it is ok.but docker_image and docker-container will error

`

  • name: load lb image
    shell: ‘docker load -i {{ dpath }}/loadbalancer/haproxy-{{ haproxy_version }}.tar.bz2’

docker_image:

load_path: ‘{{ dpath }}/loadbalancer/haproxy-{{ haproxy_version }}.tar.bz2’

name: lb

timeout: 600

`

and i find that if i install docker with yum install -y docker-ce in destination hosts, it will be success, so there must be some python package i haven’t install ,but what it is?

if i install docker use binary ,maybe some package skiped