2026-06-15 22:13:26 +02:00
.. _calculated_variable:
2023-12-17 20:25:53 +01:00
Calculated default values
==============================
Synopsis
-----------
A value can be calculated. In this case we have four possibilities:
2026-06-15 22:13:26 +02:00
- calculation via Jinja template
2023-12-17 20:25:53 +01:00
- calculation via a variable
2026-06-15 22:13:26 +02:00
- calculation via an information
- calculation via a identifier: in a :term: `dynamically built family` returns the current identifier
- calculation via an index: in the case of a :term: `follower` variable returns the current index
- calculation via :ref: `the current namespace name <namespace>`
2023-12-17 20:25:53 +01:00
If the user modifies the value of the variable, the default value is no longer used, so the calculation is no longer carried out. This is also the case if the variable has the `auto_save` attribute.
On the other hand, if the variable is hidden (with the `hidden` parameter), it is the default value that is used and not the value customized by the user.
.. note :: A follower variable cannot be calculated automatically.
2026-06-15 22:13:26 +02:00
.. seealso :: The tutorial with a real world sample :ref: `calculation <tutorial_calc_variable>`
2023-12-17 20:25:53 +01:00
Parameters
--------------
2026-06-15 22:13:26 +02:00
Jinja template
~~~~~~~~~~~~~~
2023-12-17 20:25:53 +01:00
2024-10-14 19:17:44 +02:00
.. list-table ::
2023-12-17 20:25:53 +01:00
:header-rows: 1
2024-10-14 19:17:44 +02:00
2026-06-15 22:13:26 +02:00
* - **Calculation type**
- **Comment**
* - **type**
`string`
- The value is `jinja`
2023-12-17 20:25:53 +01:00
2026-06-15 22:13:26 +02:00
* - **jinja**
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`string`
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`mandatory`
2026-06-15 22:13:26 +02:00
- Please set a Jinja template.
2024-10-14 19:17:44 +02:00
2026-06-15 22:13:26 +02:00
* - **params**
`dict`
- Additional parameters passed to the Jinja template (see below).
* - **description**
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`string`
2026-06-15 22:13:26 +02:00
- Additional information for the :ref: `documentation <data_documentation>` and the error message in case of return_type is a boolean.
2024-10-14 19:17:44 +02:00
2026-06-15 22:13:26 +02:00
Variable
~~~~~~~~
.. list-table ::
:header-rows: 1
2024-10-14 19:17:44 +02:00
2026-06-15 22:13:26 +02:00
* - Parameter
- Comment
- Sample
2023-12-17 20:25:53 +01:00
2026-06-15 22:13:26 +02:00
* - type
2023-12-17 20:25:53 +01:00
2026-06-15 22:13:26 +02:00
`string`
- Calculation type.
This parameter is optional. It is deduced from the presence of the parameter "variable" in a calculation.
- variable
2023-12-17 20:25:53 +01:00
2026-06-15 22:13:26 +02:00
* - variable
2023-12-17 20:25:53 +01:00
`string`
2026-06-15 22:13:26 +02:00
`mandatory`
- Name of the associated variable.
It's better to use :term: `relative path` notation.
- _.my_variable
* - propertyerror
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`boolean`
2026-06-15 22:13:26 +02:00
- If access to the variable is not possible due to a property
(for example `disabled` ) by default an error is returned.
The overall coherence is not guaranteed.
2023-12-17 20:25:53 +01:00
2026-06-15 22:13:26 +02:00
Default value: `true`
2024-10-14 19:17:44 +02:00
- false
2026-06-15 22:13:26 +02:00
* - optional
2024-10-14 19:17:44 +02:00
2026-06-15 22:13:26 +02:00
`boolean`
- The variable in calculation may not exist. This generates an error. Overall coherence is not guaranteed.
To avoid this set the optional parameter to `true` . In this case, if variable in calculation is not exists, the variable is always accessible.
- true
2023-12-17 20:25:53 +01:00
2026-06-15 22:13:26 +02:00
* - default
2023-12-17 20:25:53 +01:00
2026-06-15 22:13:26 +02:00
`boolean`
- If optional parameter is set to `true` and the variable in calculation is not exist, the variable is accessible.
If you do not want this behavior, set this parameter to `false` .
- false
2023-12-17 20:25:53 +01:00
2026-06-15 22:13:26 +02:00
* - description
2023-12-17 20:25:53 +01:00
2026-06-15 22:13:26 +02:00
`string`
- Rougail engine create a description like "depends on a calculation".
You can personnalize here the calculation description
-
2023-12-17 20:25:53 +01:00
2026-06-15 22:13:26 +02:00
Information
~~~~~~~~~~~
2023-12-17 20:25:53 +01:00
2024-10-14 19:17:44 +02:00
.. list-table ::
2023-12-17 20:25:53 +01:00
:header-rows: 1
2024-10-14 19:17:44 +02:00
2026-06-15 22:13:26 +02:00
* - Parameter
2023-12-17 20:25:53 +01:00
- Comments
- Sample
2026-06-15 22:13:26 +02:00
* - information
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`string`
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`mandatory`
2026-06-15 22:13:26 +02:00
- Name of the information whose value we want to retrieve.
- doc
2024-10-14 19:17:44 +02:00
2026-06-15 22:13:26 +02:00
* - variable
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`string`
2026-06-15 22:13:26 +02:00
- Variable's name. If not specified, the information is retrieve in :term: `Tiramisu` Config.
2024-10-14 19:17:44 +02:00
2026-06-15 22:13:26 +02:00
It's better to use :term: `relative path` notation.
- _.my_variable
2024-10-14 19:17:44 +02:00
2026-06-15 22:13:26 +02:00
Params
~~~~~~
2024-10-14 19:17:44 +02:00
2026-06-15 22:13:26 +02:00
In the case of a Jinja type calculation, it is possible to have parameters.
2024-10-14 19:17:44 +02:00
2026-06-15 22:13:26 +02:00
There are two types of params:
2023-12-17 20:25:53 +01:00
2026-06-15 22:13:26 +02:00
- the standard parameters (string, boolean, integer, float or null), in this case just do: "key: value"
- advanced settings (with something like "key: {}":
2024-10-14 19:17:44 +02:00
2026-06-15 22:13:26 +02:00
- parameter via a variable
- parameter via an information
- parameter via an identifier: in a :term: `dynamically built family` returns the current identifier
- parameter via an index: in the case of a :term: `follower` variable returns the current index
- parameter via :ref: `the current namespace name <namespace>`
2024-10-14 19:17:44 +02:00
2026-06-15 22:13:26 +02:00
Variable params
"""""""""""""""
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
.. list-table ::
:header-rows: 1
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
* - **Parameter**
- **Comments**
- **Sample**
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
* - **type**
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
`string`
- Type of parameter, which is variable.
- variable
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
* - **variable**
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
`string`
2024-10-14 19:17:44 +02:00
2026-06-15 22:13:26 +02:00
`mandatory`
- Variable's path.
- my_variable
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
* - **propertyerror**
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
`boolean`
- If access to the variable is not possible due to a property
(for example `disabled` ) by default an error is returned.
If the attribute is `false` , the parameter is not passed to the Jinja template.
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
**Default value** : `false`
- `true`
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
* - **optional**
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
`boolean`
- If the variable is not defined the value is always `null` .
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
**Default value** : `false`
- `true`
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
* - **whole**
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
`boolean`
- In :term: `dynamically built family` only the value of the current index is retrieve. If this parameter is set to `true` it returns all
If this parameter is set to `true` , it returns the set of values for all indices.
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
**Default value** : `false`
- `true`
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
Information params
~~~~~~~~~~~~~~~~~~
2024-10-14 19:17:44 +02:00
2026-06-15 22:13:26 +02:00
.. list-table ::
:header-rows: 1
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
* - **Parameter**
- **Comments**
- **Sample**
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
* - **type**
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
`string`
- Type of parameter, which is information
- information
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
* - **information**
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
`string`
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
`mandatory`
- Name of the information whose value we want to retrieve.
- doc
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
* - **variable**
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
`string`
- By default, the information is a context information. It is possible to get a variable information. In this case just specify a path.
- my_variable
2024-02-01 22:11:40 +01:00
2026-06-15 22:13:26 +02:00
Sample
2023-12-17 20:25:53 +01:00
-----------
Calculation via a Jinja template
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Let's start with an example from a simple Jinja template:
.. code-block :: yaml
2026-06-15 22:13:26 +02:00
%YAML 1.2
2023-12-17 20:25:53 +01:00
---
2026-06-15 22:13:26 +02:00
version: 1.1
2023-12-17 20:25:53 +01:00
my_calculated_variable:
default:
jinja: 'no'
2026-06-15 22:13:26 +02:00
...
2023-12-17 20:25:53 +01:00
2024-10-14 19:17:44 +02:00
Here is a second example with a boolean variable:
2023-12-17 20:25:53 +01:00
.. code-block :: yaml
2026-06-15 22:13:26 +02:00
%YAML 1.2
2023-12-17 20:25:53 +01:00
---
2026-06-15 22:13:26 +02:00
version: 1.1
2023-12-17 20:25:53 +01:00
my_calculated_variable:
type: boolean
default:
jinja: 'false'
2026-06-15 22:13:26 +02:00
...
2023-12-17 20:25:53 +01:00
2025-10-26 13:31:47 +01:00
And a multiple value of the integer type:
2023-12-17 20:25:53 +01:00
.. code-block :: yaml
2026-06-15 22:13:26 +02:00
%YAML 1.2
2023-12-17 20:25:53 +01:00
---
2026-06-15 22:13:26 +02:00
version: 1.1
2023-12-17 20:25:53 +01:00
my_calculated_variable:
2025-10-26 13:31:47 +01:00
type: integer
2023-12-17 20:25:53 +01:00
multi: true
default:
jinja: |
1
2
2024-10-14 19:17:44 +02:00
3
2026-06-15 22:13:26 +02:00
...
2023-12-17 20:25:53 +01:00
Let's create a variable whose value is returned by a python function:
.. code-block :: yaml
2026-06-15 22:13:26 +02:00
%YAML 1.2
2023-12-17 20:25:53 +01:00
---
2026-06-15 22:13:26 +02:00
version: 1.1
2023-12-17 20:25:53 +01:00
my_calculated_variable:
default:
jinja: '{{ return_no() }}'
2026-06-15 22:13:26 +02:00
...
2023-12-17 20:25:53 +01:00
Then let's create the `return_no` function:
2024-10-14 19:17:44 +02:00
.. code-block :: python
2023-12-17 20:25:53 +01:00
def return_no():
return 'no'
An example with parameters:
.. code-block :: yaml
2026-06-15 22:13:26 +02:00
%YAML 1.2
2023-12-17 20:25:53 +01:00
---
2026-06-15 22:13:26 +02:00
version: 1.1
2023-12-17 20:25:53 +01:00
my_calculated_variable:
description: my description
default:
jinja: |
{{ param1 }}{% if param2 is defined %}_{{ param2 }}{% endif %}_{{ param3 }}
params:
param1: value
param2:
type: variable
2024-02-01 22:11:40 +01:00
variable: _.unknown_variable
2023-12-17 20:25:53 +01:00
optional: true
param3:
type: information
information: doc
2024-02-01 22:11:40 +01:00
variable: _.my_calculated_variable
2026-06-15 22:13:26 +02:00
...
2023-12-17 20:25:53 +01:00
2026-06-15 22:13:26 +02:00
An example with a `identifier` type parameter:
2023-12-17 20:25:53 +01:00
.. code-block :: yaml
2026-06-15 22:13:26 +02:00
%YAML 1.2
2023-12-17 20:25:53 +01:00
---
2026-06-15 22:13:26 +02:00
version: 1.1
my_dyn_family_{{ identifier }}:
dynamic:
2023-12-17 20:25:53 +01:00
- val1
- val2
2026-06-15 22:13:26 +02:00
description: 'Describe {{ identifier }}'
2023-12-17 20:25:53 +01:00
my_dyn_var:
default:
2026-06-15 22:13:26 +02:00
jinja: 'the identifier is: {{ param1 }}'
2023-12-17 20:25:53 +01:00
params:
param1:
2026-06-15 22:13:26 +02:00
type: identifier
2023-12-17 20:25:53 +01:00
2026-06-15 22:13:26 +02:00
In this example, we see a :term: `dynamically built family` . Two families will be created: `my_dyn_family_val1.my_dyn_var` and `my_dyn_family_val2.my_dyn_var` .
2023-12-17 20:25:53 +01:00
2026-06-15 22:13:26 +02:00
The value of the variable inside this family 'this identifier is: ' + the value of the identifier (`val1` and `val2` respectively).
2023-12-17 20:25:53 +01:00
An example with an index type parameter:
.. code-block :: yaml
2026-06-15 22:13:26 +02:00
%YAML 1.2
2023-12-17 20:25:53 +01:00
---
2026-06-15 22:13:26 +02:00
version: 1.1
2023-12-17 20:25:53 +01:00
family:
2026-06-10 21:06:37 +02:00
type: sequence
2026-06-15 22:13:26 +02:00
2023-12-17 20:25:53 +01:00
leader:
2026-06-15 22:13:26 +02:00
- val1
- val2
2023-12-17 20:25:53 +01:00
follower1:
default:
type: jinja
jinja: 'the index is: {{ param1 }}'
params:
param1:
type: index
2026-06-15 22:13:26 +02:00
...
2023-12-17 20:25:53 +01:00
Calculation via a variable
-----------------------------
2024-10-14 19:17:44 +02:00
Copy a variable in another:
2023-12-17 20:25:53 +01:00
.. code-block :: yaml
2026-06-15 22:13:26 +02:00
%YAML 1.2
2023-12-17 20:25:53 +01:00
---
2026-06-15 22:13:26 +02:00
version: 1.1
2023-12-17 20:25:53 +01:00
my_variable:
default:
- val1
- val2
2026-06-15 22:13:26 +02:00
2023-12-17 20:25:53 +01:00
my_calculated_variable:
default:
type: variable
2024-02-01 22:11:40 +01:00
variable: _.my_variable
2026-06-15 22:13:26 +02:00
...
2023-12-17 20:25:53 +01:00
2024-09-04 16:31:45 +02:00
Copy the default value from a variable, means copy type, params and multi attribute too if not define in second variable.
.. code-block :: yaml
2026-06-15 22:13:26 +02:00
%YAML 1.2
2024-09-04 16:31:45 +02:00
---
version: 1.1
2026-06-15 22:13:26 +02:00
2024-09-04 16:31:45 +02:00
my_variable:
multi: true
type: domainname
params:
allow_ip: true
2026-06-15 22:13:26 +02:00
2024-09-04 16:31:45 +02:00
my_calculated_variable:
default:
type: variable
variable: _.var1
Here my_calculated_variable is a domainname variable with parameter allow_ip=True and multi to true.
2023-12-17 20:25:53 +01:00
Copy one variable to another if the source has no `property` problem:
.. code-block :: yaml
2026-06-15 22:13:26 +02:00
%YAML 1.2
2023-12-17 20:25:53 +01:00
---
2026-06-15 22:13:26 +02:00
version: 1.1
2023-12-17 20:25:53 +01:00
my_variable:
default: val1
disabled: true
2026-06-15 22:13:26 +02:00
2023-12-17 20:25:53 +01:00
my_calculated_variable:
multi: true
default:
2024-02-01 22:11:40 +01:00
variable: _.my_variable
2023-12-17 20:25:53 +01:00
propertyerror: false
2026-06-15 22:13:26 +02:00
...
2023-12-17 20:25:53 +01:00
Copy two non-multiple variables into a multiple variable:
.. code-block :: yaml
2026-06-15 22:13:26 +02:00
%YAML 1.2
2023-12-17 20:25:53 +01:00
---
2026-06-15 22:13:26 +02:00
version: 1.1
my_variable_1: val1
my_variable_2: val2
2023-12-17 20:25:53 +01:00
my_calculated_variable:
default:
2026-06-15 22:13:26 +02:00
- variable: _.my_variable_1
- variable: _.my_variable_2
2023-12-17 20:25:53 +01:00
2026-06-15 22:13:26 +02:00
A variable in a :term: `dynamically built family` can also be used in a calculation.
2023-12-17 20:25:53 +01:00
2026-06-15 22:13:26 +02:00
For example using the variable for a particular identifier:
2023-12-17 20:25:53 +01:00
.. code-block :: yaml
2026-06-15 22:13:26 +02:00
%YAML 1.2
2023-12-17 20:25:53 +01:00
---
2026-06-15 22:13:26 +02:00
version: 1.1
2023-12-17 20:25:53 +01:00
varname:
2026-06-15 22:13:26 +02:00
- val1
- val2
my_dyn_family_{{ identifier }}:
dynamic:
2023-12-17 20:25:53 +01:00
- val1
- val2
2026-06-15 22:13:26 +02:00
description: 'Describe {{ identifier }}'
my_dyn_var_{{ identifier }}:
2023-12-17 20:25:53 +01:00
default:
2026-06-15 22:13:26 +02:00
type: identifier
2023-12-17 20:25:53 +01:00
all_dyn_var:
default:
2024-02-01 22:11:40 +01:00
variable: _.my_dyn_family_val1.my_dyn_var_val1
2023-12-17 20:25:53 +01:00
In this case, we recover the value `val1` .
2026-06-15 22:13:26 +02:00
Second example using the variable for all identifieres:
2023-12-17 20:25:53 +01:00
.. code-block :: yaml
2026-06-15 22:13:26 +02:00
%YAML 1.2
2023-12-17 20:25:53 +01:00
---
2026-06-15 22:13:26 +02:00
version: 1.1
2023-12-17 20:25:53 +01:00
varname:
2026-06-15 22:13:26 +02:00
my_dyn_family_{{ identifier }}:
dynamic:
2023-12-17 20:25:53 +01:00
- val1
- val2
2026-06-15 22:13:26 +02:00
description: 'Describe {{ identifier }}'
my_dyn_var_{{ identifier }}:
2023-12-17 20:25:53 +01:00
default:
2026-06-15 22:13:26 +02:00
type: identifier
2023-12-17 20:25:53 +01:00
all_dyn_var:
multi: true
default:
2024-02-01 22:11:40 +01:00
variable: _.my_dyn_family_.my_dyn_var_
2026-06-15 22:13:26 +02:00
...
2023-12-17 20:25:53 +01:00
In this case, we recover the `val1` and `val2` list.
2026-06-15 22:13:26 +02:00
Calculation via a identifier
----------------------------
2023-12-17 20:25:53 +01:00
.. code-block :: yaml
2026-06-15 22:13:26 +02:00
%YAML 1.2
2023-12-17 20:25:53 +01:00
---
2026-06-15 22:13:26 +02:00
version: 1.1
my_dyn_family_{{ identifier }}:
dynamic:
2023-12-17 20:25:53 +01:00
- val1
- val2
2026-06-15 22:13:26 +02:00
description: 'Describe {{ identifier }}'
my_dyn_var_{{ identifier }}:
2023-12-17 20:25:53 +01:00
type: string
default:
2026-06-15 22:13:26 +02:00
type: identifier
...
2023-12-17 20:25:53 +01:00
Calculation via an index
--------------------------
.. code-block :: yaml
2026-06-15 22:13:26 +02:00
%YAML 1.2
2023-12-17 20:25:53 +01:00
---
2026-06-15 22:13:26 +02:00
version: 1.1
2023-12-17 20:25:53 +01:00
family:
2026-06-10 21:06:37 +02:00
type: sequence
2026-06-15 22:13:26 +02:00
2023-12-17 20:25:53 +01:00
leader:
2026-06-15 22:13:26 +02:00
- val1
- val2
2023-12-17 20:25:53 +01:00
follower1:
2025-10-26 13:31:47 +01:00
type: integer
2023-12-17 20:25:53 +01:00
default:
type: index
2026-06-15 22:13:26 +02:00
...