kayobe/ansible/kayobe-target-venv.yml
Mark Goddard e924c99c52 Avoid unconditional fact gathering
One way to improve the performance of Ansible is through fact caching.
Rather than gather facts in every play, we can configure Ansible to
cache them in a persistent store. An example Ansible configuration for
doing this is as follows:

[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = ./facts
fact_caching_timeout = 86400

While this mostly just works, there are a few places where we
unconditionally gather facts using the setup module. This change
modifies these to only gather facts when necessary.

We no longer execute the MichaelRigart.interfaces role using become:
true, since it may gather facts and we do not want it to do so as root.
The role uses become where necessary.

Change-Id: I9984a187fc6c0496ada489bb8eef36e44d695aac
Story: 2007492
Task: 39216
2020-04-08 16:56:32 +00:00

116 lines
4.0 KiB
YAML

---
# Create a virtualenv for ansible modules to use on the remote target systems
# when running kayobe.
- name: Ensure a virtualenv exists for kayobe
hosts: seed:seed-hypervisor:overcloud
gather_facts: False
tags:
- kayobe-target-venv
tasks:
- name: Set a fact about the kayobe target virtualenv
set_fact:
virtualenv: "{{ ansible_python_interpreter | dirname | dirname }}"
when:
- ansible_python_interpreter is defined
- not ansible_python_interpreter.startswith('/bin')
- not ansible_python_interpreter.startswith('/usr/bin')
- block:
- name: Gather facts
setup:
when: not module_setup | default(false)
register: gather_facts
- name: Ensure the Python virtualenv package is installed
package:
name: python{{ ansible_python.version.major }}-virtualenv
state: present
become: True
- name: Ensure global virtualenv directory exists
file:
path: "{{ virtualenv_path }}"
state: directory
owner: "{{ ansible_user_uid }}"
group: "{{ ansible_user_gid }}"
mode: 0755
# Check whether the virtualenv directory is a subdirectory of the
# global virtualenv directory.
when: virtualenv.startswith(virtualenv_path)
become: True
- name: Ensure kayobe virtualenv directory exists
file:
path: "{{ virtualenv }}"
state: directory
owner: "{{ ansible_user_uid }}"
group: "{{ ansible_user_gid }}"
mode: 0700
become: True
- name: Ensure pip is installed
easy_install:
name: pip
virtualenv: "{{ virtualenv }}"
virtualenv_site_packages: True
when:
- ansible_os_family == 'RedHat'
- ansible_distribution_major_version | int == 7
- name: Ensure kayobe virtualenv has the latest version of pip installed
pip:
name: pip
state: latest
virtualenv: "{{ virtualenv }}"
# Site packages are required for using the yum and selinux python
# modules, which are not available via PyPI.
virtualenv_site_packages: True
virtualenv_python: "python{{ ansible_python.version.major }}.{{ ansible_python.version.minor }}"
# NOTE(mgoddard): SELinux python bindings available on PyPI only work
# with Python 3 on CentOS 8.
- name: Ensure kayobe virtualenv has SELinux bindings installed
pip:
name: selinux
state: latest
virtualenv: "{{ virtualenv }}"
when:
- ansible_os_family == 'RedHat'
- ansible_distribution_major_version | int >= 8
vars:
# Use the system python interpreter since the virtualenv might not
# exist.
ansible_python_interpreter: /usr/libexec/platform-python
when: virtualenv is defined
# If we gathered facts earlier it would have been with a different Python
# interpreter. For gathering modes that may use a fact cache, gather facts
# again using the interpreter from the virtual environment.
- name: Gather facts
setup:
when:
- virtualenv is defined
- gather_facts is not skipped
- lookup('config', 'DEFAULT_GATHERING') != 'implicit'
- block:
- name: Ensure Python setuptools and pip packages are installed
vars:
packages:
- python{{ ansible_python.version.major }}-setuptools
- "{% if ansible_distribution_major_version | int >= 8 %}python3-pip{% endif %}"
package:
name: "{{ packages | select }}"
state: present
become: True
- name: Ensure pip is installed
easy_install:
name: pip
become: True
when:
- ansible_os_family == 'RedHat'
- ansible_distribution_major_version | int == 7
when: virtualenv is not defined