Introduction
YAMLFu is an YAML templating language that allows to embed dynamic elements, using regular Pyhon expressions and YAMLFu specific functions.
The yamlfu
command line utility processes YAMLFu documents and generates regular YAML.
Caution
|
yamlfu document rendering is equivalent to a python script execution. You should not render documents obtained from untrusted sources. |
Dynamic Elements Using Python
Strings contained within {}
«curly brackets» will be processed as dynamic elements, their content is evaluated in a python runtime, the result of that evalution replaces the dynamic string.
Constant Expressions
Constant expressions can be used to preserve calculation formulas in the source and only the result in the output.
YAMLFu | YAML |
---|---|
input.yaml
|
yamlfu input.yaml
|
Sibling References
Sibling references can be used to facilitate string composition and transformation, all sibling keys in a map are available to the python execution as variables matching their key names.
YAMLFu | YAML |
---|---|
input.yaml
|
yamlfu input.yaml
|
Internal Nodes
Internal nodes can be referenced from other nodes but they are not included in the output. They can be useful for storing data that needs to be referenced multiple times or for intermediate transformations.
A node is treated as "silent" when its key name starts wih an (_)
underscore symbol.
YAMLFu | YAML |
---|---|
|
|
Template Nodes
Templated nodes can be used to render nodes that need to be reproduced multiple with custom variables. Template nodes must be dictionaries, and they must have an _arguments
field with a (csv) string representing the fields required to render a template instance.
In order to render a template, the special render() function is available within the python runtime. It must be invoked with the template name as the first argument, followed by the template arguments.
The next example illustrates the generation of multiple documents using a list for the kind
argument.
YAMLFu | YAML |
---|---|
|
|
Include YAML files
The ability to include additional YAML files to compose a single document is useful for managing large projects, it can also be used to store configuration data that needs to be shared accross several YAML documents.
YAMLFu | YAML |
---|---|
_config.yaml
render_file.yaml
|
Result
|
Use External YAML for values
It is a common pattern to use a main template file that depends on additional values provided in an external file, yamlfu provides this capability with the -x
command line option.
e.g
yamlfu main.yaml -x services/nginx.yaml
YAMLFu | YAML |
---|---|
main.yaml
services/nginx.yaml
|
yamlfu main.yaml -x ervices/nginx.yaml
|
Use values from environment vriables
The command line option -i
can be used to inject environment variables.
e.g
yamlfu test.yaml -e NAME
YAMLFu | YAML |
---|---|
test.yaml
|
export NAME="Boat" && yamlfu test.yaml -e NAME
|