rougail/docs/tutorial/dynfam.rst

401 lines
13 KiB
ReStructuredText
Raw Normal View History

2025-09-03 15:35:37 +02:00
.. _dynfam:
2025-07-28 14:50:01 +02:00
2026-01-23 16:54:36 +01:00
A dynamically built family
==========================
2025-07-28 14:50:01 +02:00
.. objectives:: Objectives
2025-09-16 18:51:35 +02:00
In this section we will learn how to create a dynamically built family.
2025-07-28 14:50:01 +02:00
2025-11-05 09:47:42 +01:00
In a dynamically built family, instead of duplicating the definition of
identical variables in several families, they can be generated automatically.
2026-01-22 17:47:24 +01:00
.. prerequisites:: Prerequisites
2025-07-28 14:50:01 +02:00
2026-01-22 17:47:24 +01:00
- We assume that Rougail's library is :ref:`installed <installation>` 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 :tutorial:`rougail-tutorials git repository <src/branch/1.1>`,
this workshop page corresponds to the tags :tutorial:`v1.1_060 <src/tag/v1.1_060>` to :tutorial:`v1.1_061 <src/tag/v1.1_061>`
in the repository.
::
git clone https://forge.cloud.silique.fr/stove/rougail-tutorials.git
git switch --detach v1.1_060
We handled the HTTPS mode in the previous section. But there's more modes to handle.
Let's turn back to the firefox's configuration page:
2025-07-28 14:50:01 +02:00
.. image:: images/soksv5.png
2025-09-16 18:51:35 +02:00
We see that we need to handle the SOCKS configuration in addition to the HTTPS configuration.
2026-01-26 09:36:25 +01:00
Moreover, we can see that these two groups of variables (also known as family) are similar in the structure:
2025-09-03 15:35:37 +02:00
they both have a host and a port.
2025-07-28 14:50:01 +02:00
There are two proxies that are to be configured :
2025-09-16 18:51:35 +02:00
- the HTTPS proxy
2025-07-28 14:50:01 +02:00
- the SOCKS proxy
2025-09-16 18:51:35 +02:00
As they have the same structure, would it be possible to define the two of them
in one shot?
.. note:: It's not the place here to describe what the HTTP and SOCKS protocols are.
The interesting point here is that they are very similar in our firefox's
configuration and that we can do batch processing.
2025-07-28 14:50:01 +02:00
2026-01-23 16:54:36 +01:00
.. index:: dynamically built family
2026-01-22 18:50:29 +01:00
2026-01-23 16:54:36 +01:00
Family: A dynamically built family
-------------------------------------
2026-01-22 17:47:24 +01:00
2025-09-16 18:51:35 +02:00
With Rougail, it is possible to create some kind of a model of family.
Kind of a generic family declaration.
2026-01-22 17:47:24 +01:00
We call this generic family creation process a dynamic creation because as we will see below,
2025-09-16 18:51:35 +02:00
these families exist at the very moment we define their **identifiers**.
2025-07-28 14:50:01 +02:00
2025-09-16 18:51:35 +02:00
First, here is what we need to make (without identifiers):
2025-07-28 14:50:01 +02:00
.. code-block:: yaml
https_proxy:
description: HTTPS Proxy
...
address:
description: HTTPS address
...
port:
description: HTTPS Port
...
sock_proxy:
description: SOCKS Proxy
...
address:
description: SOCKS address
...
port:
description: SOCKS Port
...
2025-09-16 18:51:35 +02:00
Now with identifiers, we have the ability to declare our families this way:
2025-07-28 14:50:01 +02:00
2025-09-16 18:51:35 +02:00
.. code-block:: yaml
"{{ identifier }}_proxy":
description: "{{ identifier }} Proxy"
dynamic:
- HTTPS
- SOCKS
2026-01-26 09:36:25 +01:00
...
2025-09-16 18:51:35 +02:00
address:
description: "{{ identifier }} address"
2026-01-26 09:36:25 +01:00
...
2025-09-16 18:51:35 +02:00
port:
description: "{{ identifier }} port"
2026-01-26 09:36:25 +01:00
...
2026-01-22 17:47:24 +01:00
.. index:: identifier
2025-07-28 14:50:01 +02:00
2026-01-22 17:47:24 +01:00
.. type-along:: What is exactly an identifier?
If you used the YAML declaration tool named `Ansible <https://docs.ansible.com>`_,
2025-09-03 15:35:37 +02:00
the variable used to iterate over multiple values in a task is called an **`item`**.
2026-01-22 17:47:24 +01:00
We call it an identifier.
2025-09-03 15:35:37 +02:00
2026-01-22 17:47:24 +01:00
It is a symbol used in the context of a loop. For example:
2025-09-03 15:35:37 +02:00
.. code-block:: yaml
2026-01-22 17:47:24 +01:00
:caption: A code example of an ansible loop
2025-09-03 15:35:37 +02:00
- name: Loop example with 'item'
ansible.builtin.debug:
msg: "The current value is {{ item }}"
loop:
- value1
- value2
- value3
This code will output:
.. code-block:: text
The current value is value1
The current value is value2
The current value is value3
In the Rougail context, we name this item an identifier because it is an item
that allow us to define dynamically family names.
.. glossary::
identifier
2026-01-23 16:54:36 +01:00
In the :ref:`dynamically built family creation field <dynfam>` we call an identifier
2025-09-03 15:35:37 +02:00
an item that defines a family name. An item is a variable on which an iteration
on keywords will be carried out.
An :term:`identifier` is a local variable, used only for creating multiple
iterations, used for creating multiple families in only one declaration.
It allows us to declare very similar families in a more generic way.
Here is the syntax we are using that allows the declaration of multiple families at one time:
2025-07-28 14:50:01 +02:00
.. code-block:: yaml
"{{ identifier }}_proxy":
description: "{{ identifier }} Proxy"
dynamic:
- HTTPS
- SOCKS
2025-09-03 15:35:37 +02:00
This identifier is a parameter that enables us to create two families named `https_proxy` and `socks_proxy`:
2025-07-28 14:50:01 +02:00
2025-09-03 15:35:37 +02:00
.. code-block:: yaml
https_proxy:
description: "HTTPS Proxy"
socks_proxy:
description: "SOCKS Proxy"
2025-09-16 18:51:35 +02:00
.. attention:: Be careful when choosing your identifiers items: pay attention that the family
2026-01-26 09:36:25 +01:00
names that will be dynamically created have not been declared before in some other
2025-09-16 18:51:35 +02:00
YAML structure file.
2026-01-26 09:36:25 +01:00
Here the family name is: `"{{ identifier }}_proxy"`.
2026-01-23 16:54:36 +01:00
If you define a dynamically built family with the `https` item that will
2025-09-16 18:51:35 +02:00
build a `https_proxy` family and if this familiy already exists,
then rougail will raise a family/variable override warning.
2026-01-26 09:36:25 +01:00
When choosing a dynamically built family name, rougail will replace spaces, accents, uppercases...
by valid character and put all in lowercase. Only ASCII and underscore ("`_`") characters are allowed.
As you can see here, the identifier is `HTTPS`, but the name clearly contains `https` (in lowercase).
2025-09-03 15:35:37 +02:00
2026-01-23 16:54:36 +01:00
Here is our dynamically built familiy in situation in the :file:`firefox/20-manual.yml` structure file.
2026-01-22 17:47:24 +01:00
.. extinclude:: https://forge.cloud.silique.fr/stove/rougail-tutorials/raw/commit/v1.1_060/firefox/20-manual.yml
:language: yaml
2026-01-23 16:54:36 +01:00
:caption: The :file:`firefox/20-manual.yml` structure file with the dynamically built families
2026-01-22 17:47:24 +01:00
..
%YAML 1.2
---
version: 1.1
manual:
use_for_https: true # Also use this proxy for HTTPS
'{{ identifier }}_proxy':
description: '{{ identifier }} Proxy'
hidden:
variable: _.use_for_https
dynamic:
- HTTPS
- SOCKS
address:
description: '{{ identifier }} address'
default:
variable: __.http_proxy.address
port:
description: '{{ identifier }} port'
default:
variable: __.http_proxy.port
...
2025-09-03 15:35:37 +02:00
2026-01-22 18:50:29 +01:00
Here is the user data file on which we will launch the Rougail CLI:
2026-01-22 17:47:24 +01:00
.. extinclude:: https://forge.cloud.silique.fr/stove/rougail-tutorials/raw/tag/v1.1_060/config/01/config.yml
:language: yaml
2026-01-22 18:50:29 +01:00
:caption: The :file:`config/01/config.yml` user data file
2026-01-22 17:47:24 +01:00
..
---
proxy_mode: Manual proxy configuration
manual:
http_proxy:
address: http.proxy.net
port: 3128
use_for_https: false
https_proxy:
address: https.proxy.net
.. raw:: html
:url: https://forge.cloud.silique.fr/stove/rougail-tutorials/raw/tag/v1.1_060/config/01/cmd_ro.txt
:class: terminal
..
rougail -m firefox/ -u yaml -yf config/01/config.yml
.. raw:: html
:url: https://forge.cloud.silique.fr/stove/rougail-tutorials/raw/tag/v1.1_060/config/01/output_ro.html
:class: output
..
2026-01-22 18:50:29 +01:00
rougail -m firefox/proxy.yml -u yaml --yaml.filename config/proxy.yml
2025-09-03 15:35:37 +02:00
╭─────────────────── Caption ────────────────────╮
│ Variable Default value │
│ Unmodifiable variable Modified value │
│ (Original default value) │
╰────────────────────────────────────────────────╯
Variables:
┗━━ 📂 Manual proxy configuration
┣━━ 📂 HTTP Proxy
┃ ┣━━ 📓 HTTP address: ... (loaded from the YAML file "userdata/proxy.yml")
┃ ┗━━ 📓 HTTP Port: ... (8080 - loaded from the YAML file "userdata/proxy.yml")
┣━━ 📓 Also use this proxy for HTTPS: true
┣━━ 📂 HTTPS Proxy
┃ ┣━━ 📓 HTTPS address: ...
┃ ┗━━ 📓 HTTPS port: ...
┗━━ 📂 SOCKS Proxy
┣━━ 📓 SOCKS address: ...
┗━━ 📓 SOCKS port: ...
2026-01-23 16:54:36 +01:00
We can see that the dynamically built families that have been created:
2025-09-03 15:35:37 +02:00
- an `HTTPS Proxy` family
- a `SOCKS Proxy` family
2026-01-22 17:47:24 +01:00
and, as we expected, containing an address and a port.
2025-09-03 15:35:37 +02:00
2026-01-22 18:50:29 +01:00
A conditional disabled variable with dynamic identifier
--------------------------------------------------------
2025-09-18 17:17:02 +02:00
2026-01-22 18:50:29 +01:00
.. type-along:: For those who follow the tutorial with the help of the git repository
2025-09-18 17:17:02 +02:00
2026-01-22 18:50:29 +01:00
Now you need to checkout the `v1.1_061` version::
git switch --detach v1.1_061
2025-09-03 15:35:37 +02:00
2026-01-26 09:51:38 +01:00
Here is the final version of the HTTPS and SOCKS structure file, we have added:
2025-09-16 18:51:35 +02:00
- a conditional hidden family property
- a default value
2026-01-22 18:50:29 +01:00
.. extinclude:: https://forge.cloud.silique.fr/stove/rougail-tutorials/raw/tag/v1.1_061/firefox/20-manual.yml
2025-09-16 18:51:35 +02:00
:language: yaml
2026-01-22 18:50:29 +01:00
:caption: The final :file:`firefox/20-proxy.yml` structure file
2025-09-16 18:51:35 +02:00
..
2026-01-22 18:50:29 +01:00
%YAML 1.2
2025-09-16 18:51:35 +02:00
---
2026-01-22 18:50:29 +01:00
version: 1.1
2025-09-16 18:51:35 +02:00
manual:
2026-01-22 18:50:29 +01:00
use_for_https: true # Also use this proxy for HTTPS
2025-09-16 18:51:35 +02:00
2026-01-22 18:50:29 +01:00
'{{ identifier }}_proxy':
description: '{{ identifier }} Proxy'
hidden:
variable: _.use_for_https
2025-09-16 18:51:35 +02:00
dynamic:
- HTTPS
- SOCKS
address:
2026-01-22 18:50:29 +01:00
description: '{{ identifier }} address'
2025-09-16 18:51:35 +02:00
default:
2026-01-22 18:50:29 +01:00
variable: __.http_proxy.address
2025-09-16 18:51:35 +02:00
port:
2026-01-22 18:50:29 +01:00
description: '{{ identifier }} port'
2025-09-16 18:51:35 +02:00
default:
2026-01-22 18:50:29 +01:00
variable: __.http_proxy.port
version:
description: SOCKS host version used by proxy
choices:
- v4
- v5
default: v5
disabled:
type: identifier
when: HTTPS
...
2025-09-16 18:51:35 +02:00
2026-01-22 18:50:29 +01:00
The disabled `property` is assigned here to the `version` variable
2026-01-26 09:51:38 +01:00
in the case where the identifier is `HTTPS`.
This means that when the current dynamically built family is determined by the identifier `HTTPS`, the variable `version` is disabled
(therefore considered as non-existent) and when the identifier is `SOCKS` the variable is present (it is accessible).
2025-09-16 18:51:35 +02:00
2026-01-22 18:50:29 +01:00
..
version:
description: SOCKS host version used by proxy
choices:
- v4
- v5
default: v5
disabled:
type: identifier
when: HTTPS
.. extinclude:: https://forge.cloud.silique.fr/stove/rougail-tutorials/raw/tag/v1.1_061/config/01/config.yml
:language: yaml
:caption: The :file:`config/01/config.yml` user data file
2025-09-16 18:51:35 +02:00
2026-01-22 18:50:29 +01:00
..
---
proxy_mode: Manual proxy configuration
manual:
http_proxy:
address: http.proxy.net
port: 3128
use_for_https: false
https_proxy:
address: https.proxy.net
2025-09-16 18:51:35 +02:00
2026-01-22 18:50:29 +01:00
.. raw:: html
:url: https://forge.cloud.silique.fr/stove/rougail-tutorials/raw/tag/v1.1_061/config/01/cmd_ro.txt
:class: terminal
..
rougail -m firefox/ -u yaml -yf config/01/config.yml
2025-09-16 18:51:35 +02:00
2026-01-22 18:50:29 +01:00
The Rougail CLI outputs this:
2025-09-16 18:51:35 +02:00
2026-01-22 18:50:29 +01:00
.. raw:: html
:url: https://forge.cloud.silique.fr/stove/rougail-tutorials/raw/tag/v1.1_061/config/01/output_ro.html
:class: output
2025-09-16 18:51:35 +02:00
2026-01-22 18:50:29 +01:00
..
Variables:
┣━━ 📓 Configure Proxy Access to the Internet: Manual proxy configuration ◀ loaded from the YAML file "config/01/config.yml" (⏳ No proxy)
┗━━ 📂 Manual proxy configuration
┣━━ 📂 HTTP Proxy
┃ ┣━━ 📓 HTTP address: http.proxy.net ◀ loaded from the YAML file "config/01/config.yml"
┃ ┗━━ 📓 HTTP Port: 3128 ◀ loaded from the YAML file "config/01/config.yml" (⏳ 8080)
┣━━ 📓 Also use this proxy for HTTPS: false ◀ loaded from the YAML file "config/01/config.yml" (⏳ true)
┣━━ 📂 HTTPS Proxy
┃ ┣━━ 📓 HTTPS address: https.proxy.net ◀ loaded from the YAML file "config/01/config.yml" (⏳ http.proxy.net)
┃ ┗━━ 📓 HTTPS port: 3128
┗━━ 📂 SOCKS Proxy
┣━━ 📓 SOCKS address: http.proxy.net
┣━━ 📓 SOCKS port: 3128
┗━━ 📓 SOCKS host version used by proxy: v5
2025-07-28 14:50:01 +02:00
2025-09-16 18:51:35 +02:00
.. keypoints:: Key points
2025-07-28 14:50:01 +02:00
2026-01-23 16:54:36 +01:00
- We now know how to declare a dynamically built family, with setting its identifier.
2026-01-22 18:50:29 +01:00
- we have a new use case for the `disabled` property.
2026-01-26 09:36:25 +01:00
We know how to disable a dynamically built variable based on a familiy identifier.
2025-09-16 18:51:35 +02:00