I am wondering if there is a way to use a specific python virtual environment on the target host.
Reason for that is that some modules/collections require non-core python modules, but installing these modules on the host level via pip can break systems python dependencies. (Debian 12 for example explicitly warns from doing so - for good reasons)
Is there an “ansible native” way of executing the ansible built python scripts in a venv? Something like
- name: A task
venv: /path/to/a/venv
a.thirdparty.module_with_pip_dependencies:
parameter_a: 123
If not, where is the right place to discuss this feature (and contribute code later)?
I discovered today on SA, that you can simply point ansible_python_interpreter to your venv’s python.
So far it looks like it is working just fine, I’m even thinking about making this kind of default in my environment, setup venv in first ansible step and than install deps and use it.
For your use case, this should work:
- name: A task
a.thirdparty.module_with_pip_dependencies:
parameter_a: 123
vars:
ansible_python_interpreter: /path/to/a/venv/bin/python
That seems to depend a lot on how you setup a venv, using defaults a venv’s bin/python (no .bin) is a symlink to the system python. Yours seems to contain a wrapper that probably is also setting PYTHONTPATH.
My bad, just miss-typed (edited to correct value). That should of course be without the dot. I have it on /home/ansible/.venv/bin/python and just moved it by accident to bin when adjusting to ops example
So just usual bin/python inside the virtual env. I don’t set anything else, tested it so far only on community.elastic.
If a pyvenv.cfg file is found either adjacent to the Python executable or one directory above it (if the executable is a symlink, it is not dereferenced), this file is scanned for lines of the form key = value. If a home key is found, this signifies that the Python binary belongs to a virtual environment, and the value of the home key is the directory containing the Python executable used to create this virtual environment.