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

@ -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

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.