From e47ff6bd82d1c6fa146b62ddb05a295575fdffc1 Mon Sep 17 00:00:00 2001 From: Mark Goddard Date: Tue, 29 Mar 2022 11:29:20 +0100 Subject: [PATCH] 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 --- .ansible-lint | 1 + ansible/filter_plugins/tenks.py | 26 +++++++----- ansible/resource_wait.yml | 42 +++++++++---------- ansible/roles/ironic-enrolment/tasks/main.yml | 40 +++++++++--------- ansible/roles/veth-pair/tasks/present.yml | 22 +++++----- .../roles/virtualbmc-daemon/tasks/main.yml | 2 +- .../notes/jinja-context-54bce72cab955ca0.yaml | 4 ++ 7 files changed, 73 insertions(+), 64 deletions(-) create mode 100644 releasenotes/notes/jinja-context-54bce72cab955ca0.yaml diff --git a/.ansible-lint b/.ansible-lint index 2a503a9..bdcc6bf 100644 --- a/.ansible-lint +++ b/.ansible-lint @@ -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. diff --git a/ansible/filter_plugins/tenks.py b/ansible/filter_plugins/tenks.py index 4857da5..544272a 100644 --- a/ansible/filter_plugins/tenks.py +++ b/ansible/filter_plugins/tenks.py @@ -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. """ diff --git a/ansible/resource_wait.yml b/ansible/resource_wait.yml index 96d2dc1..e7a6f52 100644 --- a/ansible/resource_wait.yml +++ b/ansible/resource_wait.yml @@ -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 diff --git a/ansible/roles/ironic-enrolment/tasks/main.yml b/ansible/roles/ironic-enrolment/tasks/main.yml index f46f2e0..3b8466d 100644 --- a/ansible/roles/ironic-enrolment/tasks/main.yml +++ b/ansible/roles/ironic-enrolment/tasks/main.yml @@ -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 diff --git a/ansible/roles/veth-pair/tasks/present.yml b/ansible/roles/veth-pair/tasks/present.yml index 57b9a2d..78a7cbd 100644 --- a/ansible/roles/veth-pair/tasks/present.yml +++ b/ansible/roles/veth-pair/tasks/present.yml @@ -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 diff --git a/ansible/roles/virtualbmc-daemon/tasks/main.yml b/ansible/roles/virtualbmc-daemon/tasks/main.yml index 7eb8550..15a90dc 100644 --- a/ansible/roles/virtualbmc-daemon/tasks/main.yml +++ b/ansible/roles/virtualbmc-daemon/tasks/main.yml @@ -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 diff --git a/releasenotes/notes/jinja-context-54bce72cab955ca0.yaml b/releasenotes/notes/jinja-context-54bce72cab955ca0.yaml new file mode 100644 index 0000000..3a7ecc7 --- /dev/null +++ b/releasenotes/notes/jinja-context-54bce72cab955ca0.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + Fixes an issue seen when using Jinja2 3.1.0.