For any questions related to YTT (YAML templating tool), a third-party extension to YAML, adding support for variables, functions, include by reference, and other features not found in standard YAML.
Given a set of YAML files, you want to:
- inject variables, conditionally include parts, or loop over a list and generate chunks of YAML; and/or
- apply a set of edits (like a patch) that are guaranteed to result in valid YAML.
and, you want it to be:
- maintainable — even when templated, YAML files are human-readable
- deterministic — given the same inputs, get the same output
- scalable — as you wrangle more YAML, you can keep it sensibly organized
- general-purpose — manage all your YAML (Kubernetes manifests, Docker Compose, Concourse Pipeline, Envoy config, Travis build config, etc.) with one tool
ytt does that:
- templates are written using intuitive Python-like syntax
- extract whole chunks of YAML into functions that can be invoked without thinking about escaping or indentation
- group things into libraries to be reused and shared
- ensures execution is deterministic by giving no access to the filesystem, network, time, randomness, or operating system interfaces
ytt
is part of the Carvel suite (pronounced /ˈkɑr vəl/
; rhymes with "marvel"), an Open Source project containing a set of small sharp tools that support packaging and deploying software on Kubernetes.
Example
(See this example and others in the online ytt
Playground)
Given a ytt
template:
config.yml
#@ load("@ytt:data", "data")
#@ def/end labels():
app: apple
kind: Pod
apiVersion: v1
metadata:
name: apple-app
labels: #@ labels()
spec:
containers:
- name: apple-app
image: hashicorp/http-echo
args:
- #@ "-listen=:" + str(data.values.port)
- #@ "-text=" + data.values.text
---
kind: Service
apiVersion: v1
metadata:
name: apple-service
spec:
selector: #@ labels()
ports:
- port: #@ data.values.port
... and a set of values ...
values.yml
#@data/values
---
port: 8080
#! note how text is quoted automatically in the result file
text: "Hello #ytt World!"
Running ytt
...
$ ytt -f config.yml -f values.yml
...yields...
kind: Pod
apiVersion: v1
metadata:
name: apple-app
labels:
app: apple
spec:
containers:
- name: apple-app
image: hashicorp/http-echo
args:
- -listen=:8080
- '-text=Hello #ytt World!'
---
kind: Service
apiVersion: v1
metadata:
name: apple-service
spec:
selector:
app: apple
ports:
- port: 8080