scripts: Use ansible_python_interpreter on localhost

Commit 50c7edb9d8 ("Only install
libvirt-python and python-lxml via pip") added support for installing
libvirt-python and lxml on the virtualenv but this leads to problem like
the following one when executing all the virt_* modules.

The `libvirt` module is not importable. Check the requirements.

The problem is that libvirt-python and python-lxml are installed in the
virtualenv, but when Ansible operates on localhost, the default
interpreter is /usr/bin/python. As such, it's unable to find the
necessary requirements for all the virt_* Ansible modules. In order to
fix that, we need to pass the correct Ansible python interpreter when
operating on localhost.

Since we now pass the venv interp to Ansible, we also need to have
python-apt in the virtualenv in order to be able to use the 'apt'
module. However, in [1] it's clear that python-apt can't be installed in
a virtualenv so we have no other option than switching the ansible
python interpreter back to the host one for the tasks which need the
'apt' module.

fatal: [127.0.0.1]: FAILED! => {"changed": false, "msg": "Could not
import python modules: apt, apt_pkg. Please install python-apt
package."}

[1]: https://github.com/ansible/ansible/issues/14468
Change-Id: I59949b0ff821aac1767f56844082c2e8a32035f6
This commit is contained in:
Markos Chandras 2018-05-21 16:28:20 +01:00
parent ad0cbe99be
commit 7417619eaa
2 changed files with 15 additions and 0 deletions

View File

@ -42,9 +42,15 @@
when: (ansible_os_family | search("SUSE LINUX")) or
(ansible_os_family | search("openSUSE Leap"))
# NOTE(hwoarang) The 'apt' module needs python-apt installed in the virtualenv
# but it's not possible to do that. See https://github.com/ansible/ansible/issues/14468
# python-apt only works if it's installed on the local system so we need to switch the
# Ansible python interpreter.
- name: "Update apt cache if Ubuntu/Debian"
apt:
update_cache: yes
vars:
ansible_python_interpreter: '/usr/bin/python'
when: ansible_os_family == "Debian"
- name: "Load distribution defaults"
@ -72,9 +78,15 @@
ansible_pkg_mgr: "dnf"
when: ansible_distribution == 'Fedora' and test_dnf.stat.exists|bool
# NOTE(hwoarang) The 'apt' module needs python-apt installed in the virtualenv
# but it's not possible to do that. See https://github.com/ansible/ansible/issues/14468
# python-apt only works if it's installed on the local system so we need to switch the
# Ansible python interpreter.
- name: "Install required packages"
action: "{{ ansible_pkg_mgr }} name={{ item }} state=present"
with_items: "{{ required_packages }}"
vars:
ansible_python_interpreter: '/usr/bin/python'
- include: prepare_libvirt.yml

View File

@ -105,9 +105,11 @@ if [ ${USE_VENV} = "true" ]; then
set -u
ANSIBLE=${VENV}/bin/ansible-playbook
ENABLE_VENV="true"
ANSIBLE_PYTHON_INTERP=${VENV}/bin/python
else
$SCRIPT_HOME/env-setup.sh
ANSIBLE=${HOME}/.local/bin/ansible-playbook
ANSIBLE_PYTHON_INTERP=$(which python)
fi
# Adjust options for DHCP, VM, or Keystone tests
@ -157,6 +159,7 @@ done
${ANSIBLE} -vvvv \
-i inventory/localhost \
test-bifrost-create-vm.yaml \
-e ansible_python_interpreter="${ANSIBLE_PYTHON_INTERP}" \
-e test_vm_num_nodes=${TEST_VM_NUM_NODES} \
-e test_vm_memory_size=${VM_MEMORY_SIZE} \
-e test_vm_domain_type=${VM_DOMAIN_TYPE} \