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
- command-instead-of-shell # Use shell only when shell functionality is required
- 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.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):
@ -64,7 +68,7 @@ def _get_hostvar(context, var_name, inventory_hostname=None):
return namespace.get(var_name)
@contextfilter
@pass_context
def set_libvirt_interfaces(context, node):
"""Set interfaces for a node's specified physical networks.
"""
@ -80,7 +84,7 @@ def set_libvirt_interfaces(context, node):
return node
@contextfilter
@pass_context
def set_libvirt_volume_pool(context, node):
"""Set the Libvirt volume pool for each volume.
"""
@ -118,7 +122,7 @@ def _capabilities_to_dict(capabilities):
return capabilities_dict
@contextfilter
@pass_context
def set_libvirt_boot_firmware(context, node):
"""Set the boot firmware for a node."""
default_boot_mode = _get_hostvar(context, 'default_boot_mode',
@ -150,7 +154,7 @@ def set_libvirt_start_params(node):
return node
@contextfilter
@pass_context
def bridge_name(context, physnet, inventory_hostname=None):
"""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)))
@contextfilter
@pass_context
def source_link_name(context, node, physnet, inventory_hostname=None):
"""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))
@contextfilter
@pass_context
def peer_link_name(context, node, physnet, inventory_hostname=None):
"""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))
@contextfilter
@pass_context
def source_to_peer_link_name(context, source, inventory_hostname=None):
"""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)
@contextfilter
@pass_context
def source_link_to_physnet_name(context, source, inventory_hostname=None):
""" 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)))
@contextfilter
@pass_context
def physnet_name_to_index(context, physnet, inventory_hostname=None):
"""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]
@contextfilter
@pass_context
def physnet_index_to_name(context, idx, inventory_hostname=None):
"""Get the name of this physical network on the hypervisor.
"""

View File

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

View File

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

View File

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

View File

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

View File

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