0

Ansible is installed and is running in a virtual environment:

(env) ansible-playbook -vvvv -i localhost playbook.yml 

I'm trying to install a Python package globally, but it either installs in venv with this playbook:

---
- name: Install Docker SDK
  hosts: localhost
  vars:
    ansible_python_interpreter : '/usr/bin/python3'
  tasks:
    - name: install docker sdk
      pip:
        name: docker
      become: True

or with executable specified, copies files in global site-packages:

---
- name: Install Docker SDK
  hosts: localhost
  vars:
    ansible_python_interpreter : '/usr/bin/python3'
  tasks:
    - name: install docker sdk
      pip:
        name: docker
        executable: /usr/bin/pip3
      become: True

But pip doesn't "fully" installs as confirmed by a check in a non-venv terminal:

root@ansible-test:~# pip list|grep docker
root@ansible-test:~# python3
Python 3.8.10 (default, Sep 28 2021, 16:10:42) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import docker
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'docker'
>>> 

Unfortunately it's a project requirement to be able to also support running Ansible from a venv and also with target localhost.

Here's the output of the latter playbook:

ansible-playbook -vvvv -i localhost playbook.yml 
ansible-playbook [core 2.11.6] 
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /root/env/lib/python3.8/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /root/env/bin/ansible-playbook
  python version = 3.8.10 (default, Sep 28 2021, 16:10:42) [GCC 9.3.0]
  jinja version = 3.0.2
  libyaml = True
No config file found; using defaults
setting up inventory plugins
host_list declined parsing /root/localhost as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
script declined parsing /root/localhost as it did not pass its verify_file() method
auto declined parsing /root/localhost as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
yaml declined parsing /root/localhost as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
ini declined parsing /root/localhost as it did not pass its verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
toml declined parsing /root/localhost as it did not pass its verify_file() method
[WARNING]: Unable to parse /root/localhost as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
Loading callback plugin default of type stdout, v2.0 from /root/env/lib/python3.8/site-packages/ansible/plugins/callback/default.py
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.

PLAYBOOK: playbook.yml ************************************************************************************************************************************************************************************
Positional arguments: playbook.yml
verbosity: 4
connection: smart
timeout: 10
become_method: sudo
tags: ('all',)
inventory: ('/root/localhost',)
forks: 5
1 plays in playbook.yml

PLAY [Install Docker SDK] *********************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************************
task path: /root/playbook.yml:2
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root
<127.0.0.1> EXEC /bin/sh -c 'echo ~root && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir "` echo /root/.ansible/tmp/ansible-tmp-1635774725.779488-12287-185631404911194 `" && echo ansible-tmp-1635774725.779488-12287-185631404911194="` echo /root/.ansible/tmp/ansible-tmp-1635774725.779488-12287-185631404911194 `" ) && sleep 0'
Using module file /root/env/lib/python3.8/site-packages/ansible/modules/setup.py
<127.0.0.1> PUT /root/.ansible/tmp/ansible-local-122827g4kq6sr/tmpvoppm69b TO /root/.ansible/tmp/ansible-tmp-1635774725.779488-12287-185631404911194/AnsiballZ_setup.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1635774725.779488-12287-185631404911194/ /root/.ansible/tmp/ansible-tmp-1635774725.779488-12287-185631404911194/AnsiballZ_setup.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python3 /root/.ansible/tmp/ansible-tmp-1635774725.779488-12287-185631404911194/AnsiballZ_setup.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1635774725.779488-12287-185631404911194/ > /dev/null 2>&1 && sleep 0'
ok: [localhost]
META: ran handlers

TASK [install docker sdk] *********************************************************************************************************************************************************************************
task path: /root/playbook.yml:7
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root
<127.0.0.1> EXEC /bin/sh -c 'echo ~root && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir "` echo /root/.ansible/tmp/ansible-tmp-1635774729.4939797-12362-1030975500904 `" && echo ansible-tmp-1635774729.4939797-12362-1030975500904="` echo /root/.ansible/tmp/ansible-tmp-1635774729.4939797-12362-1030975500904 `" ) && sleep 0'
Using module file /root/env/lib/python3.8/site-packages/ansible/modules/pip.py
<127.0.0.1> PUT /root/.ansible/tmp/ansible-local-122827g4kq6sr/tmpmi7g7oa1 TO /root/.ansible/tmp/ansible-tmp-1635774729.4939797-12362-1030975500904/AnsiballZ_pip.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1635774729.4939797-12362-1030975500904/ /root/.ansible/tmp/ansible-tmp-1635774729.4939797-12362-1030975500904/AnsiballZ_pip.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python3 /root/.ansible/tmp/ansible-tmp-1635774729.4939797-12362-1030975500904/AnsiballZ_pip.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1635774729.4939797-12362-1030975500904/ > /dev/null 2>&1 && sleep 0'
changed: [localhost] => {
    "changed": true,
    "cmd": [
        "/usr/bin/pip3",
        "install",
        "docker"
    ],
    "invocation": {
        "module_args": {
            "chdir": null,
            "editable": false,
            "executable": "/usr/bin/pip3",
            "extra_args": null,
            "name": [
                "docker"
            ],
            "requirements": null,
            "state": "present",
            "umask": null,
            "version": null,
            "virtualenv": null,
            "virtualenv_command": "virtualenv",
            "virtualenv_python": null,
            "virtualenv_site_packages": false
        }
    },
    "name": [
        "docker"
    ],
    "requirements": null,
    "state": "present",
    "stderr": "",
    "stderr_lines": [],
    "stdout": "Collecting docker\n  Using cached docker-5.0.3-py2.py3-none-any.whl (146 kB)\nRequirement already satisfied: websocket-client>=0.32.0 in /usr/lib/python3.8/site-packages (from docker) (1.2.1)\nRequirement already satisfied: requests!=2.18.0,>=2.14.2 in /usr/lib/python3/dist-packages (from docker) (2.22.0)\nInstalling collected packages: docker\nSuccessfully installed docker-5.0.3\n",
    "stdout_lines": [
        "Collecting docker",
        "  Using cached docker-5.0.3-py2.py3-none-any.whl (146 kB)",
        "Requirement already satisfied: websocket-client>=0.32.0 in /usr/lib/python3.8/site-packages (from docker) (1.2.1)",
        "Requirement already satisfied: requests!=2.18.0,>=2.14.2 in /usr/lib/python3/dist-packages (from docker) (2.22.0)",
        "Installing collected packages: docker",
        "Successfully installed docker-5.0.3"
    ],
    "version": null,
    "virtualenv": null
}
META: ran handlers
META: ran handlers

PLAY RECAP ************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
3
  • Since you tried running the playbook with option -vvvv, there should be some console output showing errors and possibly some clues into what's going wrong ?
    – Kate
    Nov 1, 2021 at 11:55
  • I've updated the question with the output.
    – waverider
    Nov 1, 2021 at 13:54
  • Judging by the Ansible output execution seems to be okay. But I am wondering if you don't have competiting installs of Python3 or path issues. I see both /usr/lib/python3.8/site-packages and /usr/lib/python3/site-packages in the output. So is the docker package being installed where it should be ? /usr/bin/python3 should be a symlink to python3.8 in your case.
    – Kate
    Nov 1, 2021 at 15:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.