# ansible +docker

**URL:** <https://forum.ansible.com/t/ansible-docker/31476>\
**Category:** Ansible Project\
**Created:** [October 16, 2019, 6:07pm UTC](https://forum.ansible.com/t/ansible-docker/31476 "2019-10-16T18:07:41Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sunitha\_Iyer](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@Sunitha\_Iyer](https://forum.ansible.com/u/Sunitha_Iyer)\
**Post date:** [October 16, 2019, 6:07pm UTC](https://forum.ansible.com/t/ansible-docker/31476/1 "2019-10-16T18:07:41Z")

</div>

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")

---

<div class="post-metadata">

**Author:** ![Rohith\_Pagadala](https://avatars.discourse-cdn.com/v4/letter/r/c2a13f/32.png) [@Rohith\_Pagadala](https://forum.ansible.com/u/Rohith_Pagadala)\
**Post date:** [October 16, 2019, 6:25pm UTC](https://forum.ansible.com/t/ansible-docker/31476/2 "2019-10-16T18:25:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sunitha\_Iyer](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@Sunitha\_Iyer](https://forum.ansible.com/u/Sunitha_Iyer)\
**Post date:** [October 16, 2019, 7:30pm UTC](https://forum.ansible.com/t/ansible-docker/31476/3 "2019-10-16T19:30:23Z")

</div>

thanks for getting back Rohith, its 2.8.5

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 17, 2019, 4:27am UTC](https://forum.ansible.com/t/ansible-docker/31476/4 "2019-10-17T04:27:56Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Sunitha\_Iyer](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@Sunitha\_Iyer](https://forum.ansible.com/u/Sunitha_Iyer)\
**Post date:** [October 17, 2019, 9:45am UTC](https://forum.ansible.com/t/ansible-docker/31476/5 "2019-10-17T09:45:42Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 17, 2019, 11:43am UTC](https://forum.ansible.com/t/ansible-docker/31476/6 "2019-10-17T11:43:50Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Sunitha\_Iyer](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@Sunitha\_Iyer](https://forum.ansible.com/u/Sunitha_Iyer)\
**Post date:** [October 17, 2019, 9:30pm UTC](https://forum.ansible.com/t/ansible-docker/31476/7 "2019-10-17T21:30:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 18, 2019, 5:52am UTC](https://forum.ansible.com/t/ansible-docker/31476/8 "2019-10-18T05:52:19Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Sunitha\_Iyer](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@Sunitha\_Iyer](https://forum.ansible.com/u/Sunitha_Iyer)\
**Post date:** [October 18, 2019, 7:22am UTC](https://forum.ansible.com/t/ansible-docker/31476/9 "2019-10-18T07:22:47Z")

</div>

Sorry Stefen missed it, oracle linux server 7.6, RHEL7

---

<div class="post-metadata">

**Author:** ![wei\_zhang](https://avatars.discourse-cdn.com/v4/letter/w/3d9bf3/32.png) [@wei\_zhang](https://forum.ansible.com/u/wei_zhang)\
**Post date:** [January 6, 2020, 12:39pm UTC](https://forum.ansible.com/t/ansible-docker/31476/10 "2020-01-06T12:39:36Z")

</div>

i have the same error

`

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

PLAY [master] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*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*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
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

`

---

<div class="post-metadata">

**Author:** ![wei\_zhang](https://avatars.discourse-cdn.com/v4/letter/w/3d9bf3/32.png) [@wei\_zhang](https://forum.ansible.com/u/wei_zhang)\
**Post date:** [January 6, 2020, 2:54pm UTC](https://forum.ansible.com/t/ansible-docker/31476/11 "2020-01-06T14:54:39Z")

</div>

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
