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.