Use ansible_facts to reference facts

By default, Ansible injects a variable for every fact, prefixed with
ansible_. This can result in a large number of variables for each host,
which at scale can incur a performance penalty. Ansible provides a
configuration option [0] that can be set to False to prevent this
injection of facts. In this case, facts should be referenced via
ansible_facts.<fact>.

This change updates all references to Ansible facts within Tenks
from using individual fact variables to using the items in the
ansible_facts dictionary. This allows users to disable fact variable
injection in their Ansible configuration, which may provide some
performance improvement.

This change disables fact variable injection in the ansible
configuration used in CI, to catch any attempts to use the injected
variables.

[0] https://docs.ansible.com/ansible/latest/reference_appendices/config.html#inject-facts-as-vars

Change-Id: I4831769909c341c72bb178daf8df3309773a56ad
Co-Authored-By: Radosław Piliszek <radoslaw.piliszek@gmail.com>
This commit is contained in:
Mark Goddard 2022-02-21 14:43:28 +00:00 committed by Radosław Piliszek
parent d25067dd1a
commit 9111328e8a
16 changed files with 58 additions and 38 deletions

View File

@ -2,5 +2,6 @@ skip_list:
- '106' # Role name {} does not match ``^[a-z][a-z0-9_]+$`` pattern'
warn_list:
- experimental
- no-changed-when
- command-instead-of-shell # Use shell only when shell functionality is required
- experimental # all rules tagged as experimental

View File

@ -1,9 +1,9 @@
---
# Path to virtualenv used to install Python requirements. If a virtualenv does
# not exist at this location, one will be created.
virtualenv_path: "{{ '/'.join([ansible_env['HOME'], 'tenks-venv']) }}"
virtualenv_path: "{{ '/'.join([ansible_facts.env['HOME'], 'tenks-venv']) }}"
# The URL of the upper constraints file to pass to pip when installing Python
# packages.
python_upper_constraints_url: >-
https://releases.openstack.org/constraints/upper/{% if ansible_python.version.major == 2 %}train{% else %}master{% endif %}
https://releases.openstack.org/constraints/upper/{% if ansible_facts.python.version.major == 2 %}train{% else %}master{% endif %}

View File

@ -4,7 +4,7 @@
physnet_mappings: {}
system_requirements:
- "python{% if ansible_python.version.major == 3 %}3{% endif %}-virtualenv"
- "python{% if ansible_facts.python.version.major == 3 %}3{% endif %}-virtualenv"
# Tenks bridge type. Options are "openvswitch", "linuxbridge". Default is
# "openvswitch". Note that this relates to bridges created by Tenks, not the

View File

@ -5,8 +5,8 @@ libvirt_pool_type: dir
# Capacity is irrelevant for directory-based pools.
libvirt_pool_capacity:
libvirt_pool_mode: 755
libvirt_pool_owner: "{{ ansible_user_id }}"
libvirt_pool_group: "{{ ansible_user_id }}"
libvirt_pool_owner: "{{ ansible_facts.user_id }}"
libvirt_pool_group: "{{ ansible_facts.user_id }}"
# By default, allow QEMU without hardware virtualisation since this is a
# development tool.

View File

@ -53,8 +53,8 @@
# NOTE(mgoddard): On CentOS 8 if SELinux is enabled, install
# virtualbmc to the system rather than a virtualenv. SELinux
# prevents systemd from accessing files in users' home directories.
selinux_enabled: "{{ ansible_selinux.status | default('disabled') == 'enabled' }}"
is_centos8: "{{ ansible_os_family == 'RedHat' and ansible_distribution_major_version | int == 8 }}"
selinux_enabled: "{{ ansible_facts.selinux.status | default('disabled') == 'enabled' }}"
is_centos8: "{{ ansible_facts.os_family == 'RedHat' and ansible_facts.distribution_major_version | int == 8 }}"
vbmcd_virtualenv_path: "{{ '' if is_centos8 and selinux_enabled else virtualenv_path }}"
vbmcd_python_upper_constraints_url: >-
{{ python_upper_constraints_url }}

View File

@ -3,9 +3,9 @@
include_vars: "{{ item }}"
with_first_found:
- files:
- "{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yml"
- "{{ ansible_distribution }}.yml"
- "{{ ansible_os_family }}.yml"
- "{{ ansible_facts.distribution }}-{{ ansible_facts.distribution_major_version }}.yml"
- "{{ ansible_facts.distribution }}.yml"
- "{{ ansible_facts.os_family }}.yml"
skip: true
tags: vars

View File

@ -48,8 +48,8 @@
# NOTE(mgoddard): On CentOS 8 if SELinux is enabled, install virtualbmc
# to the system rather than a virtualenv. SELinux prevents systemd from
# accessing files in users' home directories.
selinux_enabled: "{{ ansible_selinux.status | default('disabled') == 'enabled' }}"
is_centos8: "{{ ansible_os_family == 'RedHat' and ansible_distribution_major_version | int == 8 }}"
selinux_enabled: "{{ ansible_facts.selinux.status | default('disabled') == 'enabled' }}"
is_centos8: "{{ ansible_facts.os_family == 'RedHat' and ansible_facts.distribution_major_version | int == 8 }}"
vbmc_virtualenv_path: "{{ '' if is_centos8 and selinux_enabled else virtualenv_path }}"
vbmc_log_directory: "{{ log_directory }}"
vbmc_state: "{{ domain.get('state', 'present') }}"

View File

@ -4,9 +4,9 @@
include_vars: "{{ item }}"
with_first_found:
- files:
- "{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yml"
- "{{ ansible_distribution }}.yml"
- "{{ ansible_os_family }}.yml"
- "{{ ansible_facts.distribution }}-{{ ansible_facts.distribution_major_version }}.yml"
- "{{ ansible_facts.distribution }}.yml"
- "{{ ansible_facts.os_family }}.yml"
skip: true
tags: vars
@ -15,7 +15,7 @@
msg: >
The interface {{ source_interface }} specified for the physical network
{{ network_name }} does not exist.
when: source_interface not in ansible_interfaces
when: source_interface not in ansible_facts.interfaces
### Firstly, some fact gathering.
# Start off by assuming the source interface is direct, unless proven

View File

@ -32,7 +32,7 @@
- name: Set Ironic port attributes
vars:
port_attributes: "{{ port_attributes_output.stdout | from_json }}"
switch_id: "{{ hostvars[ironic_hypervisor]['ansible_' + bridge].macaddress }}"
switch_id: "{{ hostvars[ironic_hypervisor].ansible_facts[bridge].macaddress }}"
switch_info: "{{ bridge }}"
port_id: >-
{{ source_interface

View File

@ -3,9 +3,9 @@
include_vars: "{{ item }}"
with_first_found:
- files:
- "{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yml"
- "{{ ansible_distribution }}.yml"
- "{{ ansible_os_family }}.yml"
- "{{ ansible_facts.distribution }}-{{ ansible_facts.distribution_major_version }}.yml"
- "{{ ansible_facts.distribution }}.yml"
- "{{ ansible_facts.os_family }}.yml"
skip: true
tags: vars
@ -29,7 +29,7 @@
- name: Ensure Python requirements are installed
pip:
name:
- "virtualbmc>=1.4.0{% if ansible_python.version.major == 2 %},<2{% endif %}"
- "virtualbmc>=1.4.0{% if ansible_facts.python.version.major == 2 %},<2{% endif %}"
# NOTE(priteau): Ignore PyYAML when installing system-wide to avoid the
# following error: Cannot uninstall 'PyYAML'. It is a distutils installed
# project and thus we cannot accurately determine which files belong to it

View File

@ -29,8 +29,8 @@
include_role:
name: fkautz.openvswitch-install
when:
- ansible_os_family == "RedHat"
- ansible_distribution_major_version is version(8, '<')
- ansible_facts.os_family == "RedHat"
- ansible_facts.distribution_major_version is version(8, '<')
- block:
- name: Install the Delorean repositories
@ -50,8 +50,8 @@
name: openvswitch
state: started
when:
- ansible_os_family == "RedHat"
- ansible_distribution_major_version is version(8, '>=')
- ansible_facts.os_family == "RedHat"
- ansible_facts.distribution_major_version is version(8, '>=')
- block:
- name: Install packages
@ -68,5 +68,5 @@
service:
name: openvswitch-switch
state: started
when: ansible_os_family == "Debian"
when: ansible_facts.os_family == "Debian"

View File

@ -23,13 +23,25 @@
become: true
package:
name: python3
when: ansible_python.version.major == 3
when: ansible_facts.python.version.major == 3
- name: Create virtualenv for tenks
pip:
requirements: "{{ tenks_src_dir }}/requirements.txt"
virtualenv: "{{ tenks_venv }}"
virtualenv_python: "{{ ansible_python.executable }}"
virtualenv_python: "{{ ansible_facts.python.executable }}"
- name: Ensure /etc/ansible exists
file:
path: /etc/ansible
state: directory
become: true
- name: Template ansible.cfg
template:
src: "ansible.cfg.j2"
dest: /etc/ansible/ansible.cfg
become: true
- name: Template requirements overrides
template:

View File

@ -0,0 +1,7 @@
[defaults]
# Ensure that facts are referenced via ansible_facts.<fact>.
inject_facts_as_vars = False
[ssh_connection]
pipelining = True
retries = 3

View File

@ -1,6 +1,6 @@
---
# Use python3 on Ubuntu remote hosts.
ansible_python_interpreter: "{{ '/usr/bin/python3' if ansible_python.version.major == 3 else '/usr/bin/python2' }}"
ansible_python_interpreter: "{{ '/usr/bin/python3' if ansible_facts.python.version.major == 3 else '/usr/bin/python2' }}"
# This file holds the config given to Tenks when running the zuul job,
# tenks-deploy-teardown. It assumes the existence of the bridge `breth1`.

View File

@ -2,14 +2,14 @@
# Variables shared between the playbooks
tenks_src_dir: "{{ ansible_env.HOME ~ '/' ~ zuul.projects['opendev.org/openstack/tenks'].src_dir }}"
tenks_src_dir: "{{ ansible_facts.env.HOME ~ '/' ~ zuul.projects['opendev.org/openstack/tenks'].src_dir }}"
stackhpc_libvirt_host_src_dir: >-
{{ ansible_env.HOME ~ '/' ~ zuul.projects['github.com/stackhpc/ansible-role-libvirt-host'].src_dir }}
{{ ansible_facts.env.HOME ~ '/' ~ zuul.projects['github.com/stackhpc/ansible-role-libvirt-host'].src_dir }}
stackhpc_libvirt_vm_src_dir: >-
{{ ansible_env.HOME ~ '/' ~ zuul.projects['github.com/stackhpc/ansible-role-libvirt-vm'].src_dir }}
upper_constraints_path: "{{ ansible_env.HOME ~ '/' ~ zuul.projects['opendev.org/openstack/requirements'].src_dir ~ '/upper-constraints.txt' }}"
tenks_venv: "{{ ansible_env.HOME ~ '/' ~ 'venv-tenks' }}"
config_dir: "{{ ansible_env.HOME ~ '/' ~ 'tenks-config' }}"
{{ ansible_facts.env.HOME ~ '/' ~ zuul.projects['github.com/stackhpc/ansible-role-libvirt-vm'].src_dir }}
upper_constraints_path: "{{ ansible_facts.env.HOME ~ '/' ~ zuul.projects['opendev.org/openstack/requirements'].src_dir ~ '/upper-constraints.txt' }}"
tenks_venv: "{{ ansible_facts.env.HOME ~ '/' ~ 'venv-tenks' }}"
config_dir: "{{ ansible_facts.env.HOME ~ '/' ~ 'tenks-config' }}"
tenks_overrides_path: "{{ config_dir ~ '/' ~ 'tenks-overrides.yml' }}"
tenks_requirements_overrides_path: "{{ tenks_src_dir }}/requirements-overrides.yml"
logs_dir: "/tmp/logs"

View File

@ -10,7 +10,7 @@
- name: Enable the EPEL yum repository
command: yum-config-manager --enable epel
when: ansible_os_family == 'RedHat'
when: ansible_facts.os_family == 'RedHat'
become: true
- name: Install Python3 modules
@ -20,4 +20,4 @@
- python3-pip
- python3-setuptools
- python3-wheel
- "{% if ansible_os_family == 'Debian' %}virtualenv{% else %}python3-virtualenv{% endif %}"
- "{% if ansible_facts.os_family == 'Debian' %}virtualenv{% else %}python3-virtualenv{% endif %}"