I'm running ansible playbooks in python venv
My playbooks often involve a mix of cloud infrastructure (AWS) and system engineering. I have configured them to run cloud infrastructure tasks with connection: local - this is to minimize access rights required on the target system.
However since using venv I have a conflict in regards to the ansible_python_interpreter location:
home = /opt/homebrew/opt/[email protected]/bin
include-system-site-packages = false
version = 3.12.5
executable = /opt/homebrew/Cellar/[email protected]/3.12.5/Frameworks/Python.framework/Versions/3.12/bin/python3.12
command = /opt/homebrew/opt/[email protected]/bin/python3.12 -m venv /Users/jd/projects/mgr2/ansible
Because of this, I cannot run a mixed playbook, either I need add
vars:
ansible_python_interpreter: /Users/jd/projects/mgr2/ansible/bin/python3
to my playbook to run local tasks or remove this line to run target system tasks.
I'm looking for a way to have python3 in the PATH variable, depending on which venv I am sourcing.
I had a similar problem, and I resolved it with interpreter fallback which can use a list of locations (attempted in order) unlike the interpreter config which is just a single path.
In your inventory set this variable:
...
...
ansible_python_interpreter_fallback:
- /Users/jd/projects/mgr2/ansible/bin/python3
- /usr/bin/python3
Note that you can not do this in the config file, unfortunately, it has to be set in inventory (or by inventory plugin).