Ansible in virtualenv mixing python with os

Hi,

I have this error when running a playbook to connect to azure to update virtual machines.

“Failed to import the required Python library (packaging) on Python /usr/bin/python. Please read module documentation and install in the appropriate location”

I can see it is using /usr/bin/python but I’m running ansible in a virtualenv with python3 and the python path is /home//py3/bin/python. I know the packaging library is installed in the virtualenv.

So my question is: Why is ansible trying to use the os python and how do I make it use the virtualenv version?

Cheers
Phill

Hi,

I have this error when running a playbook to connect to azure to update virtual machines.

"Failed to import the required Python library (packaging) on <compname> Python /usr/bin/python. Please read module
documentation and install in the appropriate location"

I can see it is using /usr/bin/python but I'm running ansible in a virtualenv with python3 and the python path is
/home/<me>/py3/bin/python. I know the packaging library is installed in the virtualenv.

So my question is: Why is ansible trying to use the os python and how do I make it use the virtualenv version?

Cheers
Phill

Hello Phill,

it makes total sense to use the Python interpreter installed by the OS by default. If you want to use a different
interpreter on the target, you can set it via the ansible_python_interpreter variable (playbook, commandline, host, ...).

Something like that should do the trick:

- hosts: azure

  vars:
    ansible_python_interpreter: /home/<me>/py3/bin/python

  roles:
    - vm-updates

Regards
         Racke

Hi Stefan,

Thanks for the info.

I’d tried putting

ansible_python_interpreter: /home//py3/bin/python

in the ansible.cfg file in /etc/ansible/ but whilst it read the file as I could tell from the -vvv switch I was still seeing parts of the run falling back to /usr/bin/python??

Anyway I subsequently (prior to you replying) installed the missing packages in the system os python version (cheating I know bu I wanted to make sure it really was a problem with the path and this was the easiest way to test it). It worked. but this didn’t fix the original problem. Sooooo I saw your response and put the variable definition in the playbook and this worked. I could see via the -vvv switch that all the python paths were correct.

Thanks for your help.

Cheers
Phill

Hi Stefan,

Thanks for the info.

I'd tried putting

ansible_python_interpreter: /home/<me>/py3/bin/python

in the ansible.cfg file in /etc/ansible/ but whilst it read the file as I could tell from the -vvv switch I was still
seeing parts of the run falling back to /usr/bin/python??

Anyway I subsequently (prior to you replying) installed the missing packages in the system os python version (cheating I
know bu I wanted to make sure it really was a problem with the path and this was the easiest way to test it). It worked.
but this didn't fix the original problem. Sooooo I saw your response and put the variable definition in the playbook and
this worked. I could see via the -vvv switch that all the python paths were correct.

Thanks for your help.

You are welcome. According to the docs the correct name for ansible.cfg is "python_interpreter" in the [defaults]
section (https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html).

Regards
         Racke

Hi Stefan,

Thanks for the info.

I'd tried putting

ansible_python_interpreter: /home/<me>/py3/bin/python

in the ansible.cfg file in /etc/ansible/ but whilst it read the file as I could tell from the -vvv switch I was still
seeing parts of the run falling back to /usr/bin/python??

Anyway I subsequently (prior to you replying) installed the missing packages in the system os python version (cheating I
know bu I wanted to make sure it really was a problem with the path and this was the easiest way to test it). It worked.
but this didn't fix the original problem. Sooooo I saw your response and put the variable definition in the playbook and
this worked. I could see via the -vvv switch that all the python paths were correct.

Thanks for your help.

You are welcome. According to the docs the correct name for ansible.cfg is "python_interpreter" in the [defaults]
section (https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html).

Regards
         Racke

Sorry, looks I wrote to soon. This only allows to set the mode of discovery despite the variable name :-(.

Regards
         Racke

Hi Stefan,

Actually after trying your fix I went back to figure out why the config file was not working and it was then I discovered the environment name and the config ini name is different!!!

Anyway I set the variable in the config file (using the correct name) and disabled it in the playbook and it still worked and I could see with the -vvv it was using the correct paths.

I’ve been going round and round with this all day (on and off) so i’m pleased it is working now and I understand why.

Thanks again
Phill