The Jinja propertyerror test

Objectives

In a Jinja-type calculation, it is possible to test if a variable is accessible (there is no Access control problem).

Prerequisites

  • We assume that Rougail’s library is installed on your computer.

  • It is possible to retrieve the current state of the various Rougail files manipulated in this tutorial step by checking out the corresponding tag of the rougail-tutorials git repository. Each tag corresponds to a stage of progress in the tutorial. Of course, you can also decide to copy/paste or download the tutorial files contents while following the tutorial steps.

If you want to follow this tutorial with the help of the corresponding rougail-tutorials git repository, this workshop page corresponds to the tag v1.1_140 in the repository:

git clone https://forge.cloud.silique.fr/stove/rougail-tutorials.git
git switch --detach v1.1_140

For now, the variable proxy_dns_socks5 defines these access controls as follows:

  • if the proxy_mode variable is not set to "Manual proxy configuration"

  • if socks version is v4.

The firefox/55-proxy_dns_socks5.yml
%YAML 1.2
---
version: 1.1

proxy_dns_socks5:
  description: Use proxy DNS when using SOCKS v5
  default: false
  disabled:
    jinja: |-
      {{ _.proxy_mode != "Manual proxy configuration" or _.manual.socks_proxy.version == 'v4' }}
    return_type: boolean
    description: |-
      if "_.proxy_mode" is not "Manual proxy configuration"
      or "_.manual.socks_proxy.version" is "v4"
...

In principle it works, but is that really what we want? We mean, do we want to disable it based on the proxy_mode variable?

No, we actually want to make this variable available if the socks version is accessible and equals v4.

Disabled a variable when an other variable is disabled

The most suitable syntax in Jinja for checking the status of a variable is to use the concept of a test.

Rougail offers a custom test that will verify correct access to the variable.

This test is call propertyerror.

Here is how one could write the Jinja using this feature:

The firefox/55-proxy_dns_socks5.yml structure file with some Jinja code in the hidden property
%YAML 1.2
---
version: 1.1

proxy_dns_socks5:
  description: Use proxy DNS when using SOCKS v5
  default: false
  disabled:
    jinja: |-
      {{ _.manual.socks_proxy.version is propertyerror or _.manual.socks_proxy.version == 'v4' }}
    return_type: boolean
    description: |-
      if "_.manual.socks_proxy.version" is accessible and equals "v4"
...

Key points

We have seen that we can use the test propertyerror in Jinja template.