Skip to content

Commit b949557

Browse files
committed
Standardize Python interpreter handling
This patch ensures that the system Python interpreter is always used during the `bootstrap-servers` phase by setting `ansible_python_interpreter=auto_silent` via `extra_vars`, while the `octavia-certificates` command now explicitly uses the same Python interpreter that was used to invoke the `kolla-ansible` CLI, ensuring compatibility with the local environment. Summary: - The `ansible_python_interpreter` is now dynamically defined in `group_vars/all.yml`, falling back to the system interpreter if no virtualenv is configured. - CLI commands now detect and respect user-specified `ansible_python_interpreter` values via `-e`, and avoid overriding them. - The `bootstrap-servers` command always uses the system Python interpreter by setting `ansible_python_interpreter=auto_silent`, ensuring a clean environment for virtualenv creation. - The `octavia-certificates` command uses the Python interpreter that invoked the `kolla-ansible` CLI, ensuring compatibility with locally installed packages (e.g. cryptography). This improves interpreter handling across commands, especially in environments with custom Python setups or virtual environments. Depends-On: https://review.opendev.org/c/openstack/ansible-collection-kolla/+/950265 Change-Id: I7dc6cf8eda3b1fd48d2000f18b4670a4ef4ce1b4
1 parent bd4542f commit b949557

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

ansible/group_vars/baremetal.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
ansible_python_interpreter: >-
3+
{{
4+
virtualenv ~ '/bin/python'
5+
if virtualenv is defined
6+
and virtualenv is not none
7+
and virtualenv | length > 0
8+
else (
9+
ansible_facts.python.executable
10+
if ansible_facts.python.executable is defined
11+
and ansible_facts.python.executable is not none
12+
and ansible_facts.python.executable | length > 0
13+
else 'auto_silent'
14+
)
15+
}}

kolla_ansible/cli/commands.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
# License for the specific language governing permissions and limitations
1111
# under the License.
1212

13+
import sys
14+
1315
from cliff.command import Command
1416

1517
from kolla_ansible import ansible
@@ -63,6 +65,14 @@ def _get_verbosity_args(self):
6365
return verbosity_args
6466

6567
def run_playbooks(self, parsed_args, *args, **kwargs):
68+
# If the user knows what they're doing and explicitly sets
69+
# ansible_python_interpreter, respect their choice and avoid
70+
# overriding it by kolla-ansible.
71+
extra = kwargs.get("extra_vars", {})
72+
for var in getattr(parsed_args, "extra_vars", []) or []:
73+
if var.startswith("ansible_python_interpreter="):
74+
extra.pop("ansible_python_interpreter", None)
75+
break
6676
kwargs.update(self._get_verbosity_args())
6777
return ansible.run_playbooks(parsed_args, *args, **kwargs)
6878

@@ -153,6 +163,7 @@ def take_action(self, parsed_args):
153163

154164
extra_vars = {}
155165
extra_vars["kolla_action"] = "bootstrap-servers"
166+
extra_vars["ansible_python_interpreter"] = "auto_silent"
156167

157168
playbooks = _choose_playbooks(parsed_args, "kolla-host")
158169

@@ -199,7 +210,9 @@ def get_parser(self, prog_name):
199210
return parser
200211

201212
def take_action(self, parsed_args):
202-
extra_vars = {}
213+
extra_vars = {
214+
"ansible_python_interpreter": sys.executable
215+
}
203216

204217
if hasattr(parsed_args, "check_expiry") \
205218
and parsed_args.check_expiry is not None:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
features:
3+
- |
4+
``bootstrap-servers`` now always uses the system Python interpreter via
5+
``auto_silent`` autodetection.
6+
7+
``octavia-certificates`` now use the same Python interpreter as the one
8+
running the ``kolla-ansible`` command itself.

tests/test-octavia.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ export PYTHONUNBUFFERED=1
1111
function check_certificate_expiry {
1212
RAW_INVENTORY=/etc/kolla/inventory
1313
source $KOLLA_ANSIBLE_VENV_PATH/bin/activate
14-
# NOTE(mnasiadka): Use venv python here for cryptography package (Ansible fails otherwise)
15-
kolla-ansible octavia-certificates -i ${RAW_INVENTORY} --check-expiry 7 -e ansible_python_interpreter=$KOLLA_ANSIBLE_VENV_PATH/bin/python
14+
kolla-ansible octavia-certificates -i ${RAW_INVENTORY} --check-expiry 7
1615
deactivate
1716
}
1817

0 commit comments

Comments
 (0)