manage docker via Ansible - question on the python docker library/package on the target

hi everybody,

I try to handle docker containers via Ansible and am getting this error:

msg: 'Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on vm-51150-0191’‘s Python /usr/libexec/platform-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 ‘‘docker’’’

I assume that means the target host needs to have a python docker module/library/package (sorry I am not 100% aware what the correcet term is here).
I read pip install docker to be the common solution, but am advised in my workplace to go around pip if possible (I miss the knowledge to discuss the reasons for this though).

So, is the alternative to install it manually (I found these instructions)?

  1. Download the package
  2. unzip it if it is zipped
  3. cd into the directory containing setup.py
  4. If there are any installation instructions contained in documentation, read and follow the instructions OTHERWISE
  5. type in python setup.py install
    and would this be the package I need: https://github.com/docker/**docker-py**/releases ?

Hi,

I try to handle docker containers via Ansible and am getting this
error:
     msg: 'Failed to import the required Python library (Docker SDK
for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on
vm-51150-0191''s Python /usr/libexec/platform-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
''docker'''

I assume that means the target host needs to have a python docker
module/library/package (sorry I am not 100% aware what the correcet
term is here).

yes, that is correct. Generally the requirements for a module are
needed in the environment the module is executed. Most modules run on
the target host. (If you don't use `delegate_to: localhost` or
something like that, or have one of the rare modules which are actually
implemented as action plugins and thus run on the controller.)

I read pip install docker to be the common solution, but am advised
in my workplace to go around pip if possible (I miss the knowledge to
discuss the reasons for this though).

So, is the alternative to install it manually (I found these
instructions)?

The best alternative is usually to install it from system packages. For
example, on Ubuntu and Debian, there's the python3-docker system
package you can install. For other OSes similar packages might exist.

Depending on which modules/plugins you need, you might also be
interested in the current 3.0.0-a1 prerelease of community.docker,
where some of the modules were rewritten to work with only requests
(and some more dependencies, depending on your Python version, and how
exactly you connect to the Docker daemon; see
https://github.com/ansible-collections/community.docker#external-requirements
for more details). For a list of plugins/modules that were adjusted so
far see
https://github.com/ansible-collections/community.docker/issues/364#issuecomment-1172957628
Please note that these new versions haven't yet been extensively
tested, so there might be bugs.

Best regards,
Felix

The best alternative is usually to install it from system packages. For
example, on Ubuntu and Debian, there's the python3-docker system
package you can install. For other OSes similar packages might exist.

same same (python3-docker) on this rhel8 host. Will go for that then. thx for the wisdom on this

Hi,

> I try to handle docker containers via Ansible and am getting this
> error:
> msg: 'Failed to import the required Python library (Docker SDK
> for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on
> vm-51150-0191''s Python /usr/libexec/platform-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
> ''docker'''
>
> I assume that means the target host needs to have a python docker
> module/library/package (sorry I am not 100% aware what the correcet
> term is here).

yes, that is correct. Generally the requirements for a module are
needed in the environment the module is executed. Most modules run on
the target host. (If you don't use `delegate_to: localhost` or
something like that, or have one of the rare modules which are actually
implemented as action plugins and thus run on the controller.)

Sounds like you've got a different ansible-core, and a different
python, than wherever you manually installed this module. Don't do
that, it's very confusing to do manually for each module this way.

I'm guessing that you used "/usr/bin/python" on an older OS, such as
RHEL 7, for which it is python 2.7. Don't! Use python3. For example,
if you need to use a more modern ansible-core:

I'm also guessing that you want the "python-docker" module, and not
the "docker" python module. Yes, it's really foolish to put the word
"python" in the name of a python module, I didn't do it!

     pip3 install --user ansible-core
     pip3 install --user python-docker

Always double check python modules with the word "python" in their
name, because RHEL is inconsistent about leaving in or stripping that
word "python" in the RPM name.

Don't bother with the plain "pip" or python", it just gets confusing,
always use pip3 or python3, unless you want your brain to hurt with
just this sort of confusion.

Also, don't bother with the full "ansible" package. IIt's confusing,
and it does not contain ansible. The packaging has become somewhat
byzantine. Install "ansible-core", and use that consistently, since it
contains all the basic ansible executable and python modules anyway.

> I read pip install docker to be the common solution, but am advised
> in my workplace to go around pip if possible (I miss the knowledge to
> discuss the reasons for this though).

The name of the module you're looking for is "python-docker". And yes,
it's confusing as heck, why for goddess's sake is there an extra
python in the name? You may as well ask why ansible elected to split
into "ansible-core", that actually contains ansible, and "ansible"
that has at least 300 MBytes of modules mostly used by so very few
people, and just leaft them alone as the also individually published
versions?

> So, is the alternative to install it manually (I found these
> instructions)?

The best alternative is usually to install it from system packages. For
example, on Ubuntu and Debian, there's the python3-docker system
package you can install. For other OSes similar packages might exist.

On RHEL 7, which is what I'm guessing you're using due to the
out-of-date but still system default python 2.7, There isn't a
published RPM for ansible-core on RHEL 7, and *I'm* not going to do
one, ansible-core

For RHEL 7, *I* publish tools to build ansible-core RPMs, but in your
shoes I'd jump to RHEL 8 and python 3.8 and get a contemporary,
supported version of ansible-core. My tools are at
https://github.com/nkadel/ansiblerepo/ and you're welcome to play with
them, but I'd suggest you save some time hop to an RPM supported
operating system.

Depending on which modules/plugins you need, you might also be
interested in the current 3.0.0-a1 prerelease of community.docker,

If I may suggest? If you're not aware of the distinct versions of
python, then playing with bleeding edge version of docker is likely to
waste quitea a lot of your time climbing a steep learning curve, much
as you're encountering with ansible's confusing and inconsistent
support of python 2 and python 3. I'd instead invest the time in
getting off any base operating system which still uses python 2.7 as
its default.

If that's what he needs. "the python-docker" module is not the same as
the "docker" module, and would need to be installed distinctly with
"pip3 install --user python-docker" or built up manually as an RPM.
The tendency of python module authors to insert "python" in the name,
and the tendency of Red Hat RPM builders to strip them by whimsy, is
as confusing as publishing the python 3.8 based version of
"ansiblc-core" as "ansible-core" instead of as
"python38-ansible-core". It's one of Red Hat's more confusing
practices.

Hi,

> > The best alternative is usually to install it from system
> > packages. For example, on Ubuntu and Debian, there's the
> > python3-docker system package you can install. For other OSes
> > similar packages might exist.
>
> same same (python3-docker) on this rhel8 host. Will go for that
> then. thx for the wisdom on this

If that's what he needs. "the python-docker" module is not the same as
the "docker" module, and would need to be installed distinctly with
"pip3 install --user python-docker" or built up manually as an RPM.
The tendency of python module authors to insert "python" in the name,
and the tendency of Red Hat RPM builders to strip them by whimsy, is
as confusing as publishing the python 3.8 based version of

the Python library is called "docker"
(https://pypi.org/project/docker/), and the DEB and RPM packages are
called "python3-docker".

Cheers,
Felix

I agree with your reasoning, and hope it's borne out by the original
poster. Unfortunately, based on the python 2.7 references, I'm
assuming he's on RHEL 7. or a similar OS. Those don't have a publicly
published python3-docker package. Unless you see one that I don't?