Use jinja2.pass_context instead of contextfilter

Jinja2 3.0.0 deprecated contextfilter in favour of pass_context. 3.1.0
dropped contextfilter.

Fall back to contextfilter for Jinja2 2.x.

This change also fixes some issues caused by Ansible lint 6.0. Issues
found by the yaml plugin are fixed, while the FQCN for builtin actions
plugin is skipped.

Change-Id: I97b25551eb26da2c9100120bcd646c88fdb33ba6
This commit is contained in:
Mark Goddard 2022-03-29 11:29:20 +01:00
parent eba6b368f6
commit e47ff6bd82
7 changed files with 73 additions and 64 deletions

View File

@ -5,3 +5,4 @@ warn_list:
- no-changed-when - no-changed-when
- command-instead-of-shell # Use shell only when shell functionality is required - command-instead-of-shell # Use shell only when shell functionality is required
- experimental # all rules tagged as experimental - experimental # all rules tagged as experimental
- fqcn-builtins # Use FQCN for builtin actions.

View File

@ -16,7 +16,11 @@ import re
from ansible.errors import AnsibleFilterError from ansible.errors import AnsibleFilterError
from ansible.module_utils._text import to_text from ansible.module_utils._text import to_text
from jinja2 import contextfilter # NOTE: jinja2 3.1.0 dropped contextfilter in favour of pass_context.
try:
from jinja2 import pass_context
except ImportError:
from jinja2 import contextfilter as pass_context
class FilterModule(object): class FilterModule(object):
@ -64,7 +68,7 @@ def _get_hostvar(context, var_name, inventory_hostname=None):
return namespace.get(var_name) return namespace.get(var_name)
@contextfilter @pass_context
def set_libvirt_interfaces(context, node): def set_libvirt_interfaces(context, node):
"""Set interfaces for a node's specified physical networks. """Set interfaces for a node's specified physical networks.
""" """
@ -80,7 +84,7 @@ def set_libvirt_interfaces(context, node):
return node return node
@contextfilter @pass_context
def set_libvirt_volume_pool(context, node): def set_libvirt_volume_pool(context, node):
"""Set the Libvirt volume pool for each volume. """Set the Libvirt volume pool for each volume.
""" """
@ -118,7 +122,7 @@ def _capabilities_to_dict(capabilities):
return capabilities_dict return capabilities_dict
@contextfilter @pass_context
def set_libvirt_boot_firmware(context, node): def set_libvirt_boot_firmware(context, node):
"""Set the boot firmware for a node.""" """Set the boot firmware for a node."""
default_boot_mode = _get_hostvar(context, 'default_boot_mode', default_boot_mode = _get_hostvar(context, 'default_boot_mode',
@ -150,7 +154,7 @@ def set_libvirt_start_params(node):
return node return node
@contextfilter @pass_context
def bridge_name(context, physnet, inventory_hostname=None): def bridge_name(context, physnet, inventory_hostname=None):
"""Get the Tenks bridge name from a physical network name. """Get the Tenks bridge name from a physical network name.
""" """
@ -160,7 +164,7 @@ def bridge_name(context, physnet, inventory_hostname=None):
inventory_hostname=inventory_hostname))) inventory_hostname=inventory_hostname)))
@contextfilter @pass_context
def source_link_name(context, node, physnet, inventory_hostname=None): def source_link_name(context, node, physnet, inventory_hostname=None):
"""Get the source veth link name for a node/physnet combination. """Get the source veth link name for a node/physnet combination.
""" """
@ -170,7 +174,7 @@ def source_link_name(context, node, physnet, inventory_hostname=None):
inventory_hostname=inventory_hostname)) inventory_hostname=inventory_hostname))
@contextfilter @pass_context
def peer_link_name(context, node, physnet, inventory_hostname=None): def peer_link_name(context, node, physnet, inventory_hostname=None):
"""Get the peer veth link name for a node/physnet combination. """Get the peer veth link name for a node/physnet combination.
""" """
@ -180,7 +184,7 @@ def peer_link_name(context, node, physnet, inventory_hostname=None):
inventory_hostname=inventory_hostname)) inventory_hostname=inventory_hostname))
@contextfilter @pass_context
def source_to_peer_link_name(context, source, inventory_hostname=None): def source_to_peer_link_name(context, source, inventory_hostname=None):
"""Get the corresponding peer link name for a source link name. """Get the corresponding peer link name for a source link name.
""" """
@ -190,7 +194,7 @@ def source_to_peer_link_name(context, source, inventory_hostname=None):
inventory_hostname=inventory_hostname) inventory_hostname=inventory_hostname)
@contextfilter @pass_context
def source_link_to_physnet_name(context, source, inventory_hostname=None): def source_link_to_physnet_name(context, source, inventory_hostname=None):
""" Get the physical network name that a source veth link is connected to. """ Get the physical network name that a source veth link is connected to.
""" """
@ -265,7 +269,7 @@ def _link_name(context, node, physnet, inventory_hostname=None):
inventory_hostname=inventory_hostname))) inventory_hostname=inventory_hostname)))
@contextfilter @pass_context
def physnet_name_to_index(context, physnet, inventory_hostname=None): def physnet_name_to_index(context, physnet, inventory_hostname=None):
"""Get the ID of this physical network on the hypervisor. """Get the ID of this physical network on the hypervisor.
""" """
@ -277,7 +281,7 @@ def physnet_name_to_index(context, physnet, inventory_hostname=None):
return state[inventory_hostname]['physnet_indices'][physnet] return state[inventory_hostname]['physnet_indices'][physnet]
@contextfilter @pass_context
def physnet_index_to_name(context, idx, inventory_hostname=None): def physnet_index_to_name(context, idx, inventory_hostname=None):
"""Get the name of this physical network on the hypervisor. """Get the name of this physical network on the hypervisor.
""" """

View File

@ -23,27 +23,27 @@
loop_var: spec loop_var: spec
- block: - block:
- name: Check that OpenStack credentials exist in the environment - name: Check that OpenStack credentials exist in the environment
fail: fail:
msg: > msg: >
$OS_USERNAME was not found in the environment. Ensure the OpenStack $OS_USERNAME was not found in the environment. Ensure the OpenStack
credentials exist in your environment, perhaps by sourcing your RC file. credentials exist in your environment, perhaps by sourcing your RC file.
when: not lookup('env', 'OS_USERNAME') when: not lookup('env', 'OS_USERNAME')
- name: Gather list of OpenStack services - name: Gather list of OpenStack services
command: >- command: >-
{{ virtualenv_path }}/bin/openstack service list -f json {{ virtualenv_path }}/bin/openstack service list -f json
register: service_list_output register: service_list_output
changed_when: false changed_when: false
- name: Include the wait-for-resources role - name: Include the wait-for-resources role
include_role: include_role:
name: wait-for-resources name: wait-for-resources
vars: vars:
wait_for_resources_required_resources: "{{ tenks_expected_resources }}" wait_for_resources_required_resources: "{{ tenks_expected_resources }}"
wait_for_resources_venv: "{{ virtualenv_path }}" wait_for_resources_venv: "{{ virtualenv_path }}"
wait_for_resources_python_upper_constraints_url: >- wait_for_resources_python_upper_constraints_url: >-
{{ python_upper_constraints_url }} {{ python_upper_constraints_url }}
# Only attempt to wait for resources when the placement service is running # Only attempt to wait for resources when the placement service is running
when: service_list_output.stdout | from_json | selectattr('Type', 'equalto', 'placement') | list | length >= 1 when: service_list_output.stdout | from_json | selectattr('Type', 'equalto', 'placement') | list | length >= 1
when: tenks_expected_resources | length > 0 when: tenks_expected_resources | length > 0

View File

@ -11,14 +11,14 @@
- name: Ensure the latest versions of pip and setuptools are installed - name: Ensure the latest versions of pip and setuptools are installed
pip: pip:
name: "{{ item.name }}" name: "{{ item }}"
state: latest # noqa package-latest state: latest # noqa package-latest
virtualenv: "{{ ironic_virtualenv_path }}" virtualenv: "{{ ironic_virtualenv_path }}"
extra_args: >- extra_args: >-
-c {{ ironic_python_upper_constraints_url }} -c {{ ironic_python_upper_constraints_url }}
with_items: with_items:
- { name: pip } - pip
- { name: setuptools } - setuptools
# This is useful to get a uniquely generated temporary path. # This is useful to get a uniquely generated temporary path.
- name: Create temporary file for pip requirements - name: Create temporary file for pip requirements
@ -45,25 +45,25 @@
# argument to the os_ironic module, due to a quirk in its implementation. # argument to the os_ironic module, due to a quirk in its implementation.
# Grab the endpoint from the file. # Grab the endpoint from the file.
- block: - block:
- name: Query clouds.yaml - name: Query clouds.yaml
os_client_config: os_client_config:
clouds: "{{ lookup('env', 'OS_CLOUD') }}" clouds: "{{ lookup('env', 'OS_CLOUD') }}"
delegate_to: localhost delegate_to: localhost
vars: vars:
ansible_python_interpreter: >- ansible_python_interpreter: >-
{{ lookup('env', 'VIRTUAL_ENV') | default('/usr', true) ~ '/bin/python' }} {{ lookup('env', 'VIRTUAL_ENV') | default('/usr', true) ~ '/bin/python' }}
- name: Fail if the cloud was not found - name: Fail if the cloud was not found
fail: fail:
msg: > msg: >
Cloud {{ lookup('env', 'OS_CLOUD') }} was not found in clouds.yaml Cloud {{ lookup('env', 'OS_CLOUD') }} was not found in clouds.yaml
when: >- when: >-
openstack.clouds | length == 0 or openstack.clouds | length == 0 or
not openstack.clouds[0].get('auth', {}).get('endpoint') not openstack.clouds[0].get('auth', {}).get('endpoint')
- name: Set a fact about the ironic API endpoint - name: Set a fact about the ironic API endpoint
set_fact: set_fact:
ironic_url: "{{ openstack.clouds[0].auth.endpoint }}" ironic_url: "{{ openstack.clouds[0].auth.endpoint }}"
when: lookup('env', 'OS_CLOUD') | length > 0 when: lookup('env', 'OS_CLOUD') | length > 0
- name: Detect ironic API version - name: Detect ironic API version

View File

@ -43,16 +43,16 @@
when: veth_pair_peer_bridge_type == "linuxbridge" when: veth_pair_peer_bridge_type == "linuxbridge"
- block: - block:
- include_tasks: is-attached.yml - include_tasks: is-attached.yml
vars: vars:
bridge: "{{ veth_pair_source_bridge }}" bridge: "{{ veth_pair_source_bridge }}"
interface: "{{ veth_pair_source_link_name }}" interface: "{{ veth_pair_source_link_name }}"
- name: Plug veth into source bridge - name: Plug veth into source bridge
command: >- command: >-
ip link set {{ veth_pair_source_link_name }} master ip link set {{ veth_pair_source_link_name }} master
{{ veth_pair_source_bridge }} {{ veth_pair_source_bridge }}
when: when:
- not veth_pair_is_attached - not veth_pair_is_attached
become: true become: true
when: veth_pair_plug_into_source | bool when: veth_pair_plug_into_source | bool

View File

@ -62,7 +62,7 @@
- name: Ensure Virtual BMC systemd service is started and enabled - name: Ensure Virtual BMC systemd service is started and enabled
systemd: systemd:
name: "{{ service }}" name: "{{ service }}"
enabled: yes enabled: true
state: started state: started
daemon_reload: "{{ service_file.changed }}" daemon_reload: "{{ service_file.changed }}"
become: true become: true

View File

@ -0,0 +1,4 @@
---
fixes:
- |
Fixes an issue seen when using Jinja2 3.1.0.