📚 / Documentation / Tools / ytt


Overview

ytt is a templating engine for YAML.

Installation

To enable this tool, add this to your vars.yml:

---
tools:
  - ytt

Usage

ytt can be called within a Bash manifest, in a bundle to generate a template manifest.

Sample example

vars.yml:

---
tools:
  - ytt

packages:
  - name: generate-echo-app
    kind: bundle
  - name: echo-app
    kind: application

templates/echo-app/data.yml:

#@ load("@ytt:data", "data")

#@ def labels():
app: echo
org: test
#@ end

#@ def name(echo):
#@   return "echo-"+echo.name
#@ end

kind: Pod
apiVersion: v1
metadata:
  name: echo-app
  labels: #@ labels()
spec:
  containers:
  #@ for/end echo in data.values.echos:
  - name: #@ name(echo)
    image: hashicorp/http-echo
    args:
    - #@ "-listen=:" + str(echo.port)
    - #@ "-text=" + echo.text

#@ if/end data.values.service.enabled:
---
kind: Service
apiVersion: v1
metadata:
  name: echo-service
spec:
  selector: #@ labels()
  ports:
  #@ for/end echo in data.values.echos:
  - name: #@ name(echo)
    port: #@ echo.port

templates/echo-app/values.yml:

#@data/values
---
echos:
- name: first
  port: 8080
  text: "Hello #ytt World on 8080!"
- name: second
  port: 8081
  text: "Hello #ytt World on 8081!"
service:
  enabled: true

bundles/generate-echo-app/generate-app.sh:

#!/bin/bash

set -eux

ytt -f ./templates/echo-app > ./applications/echo-app/manifest.yml

applications/echo-app/README.md:

# Echo App

Generated by bundle `generate-echo-app`

What’s next?

Read more about ytt on their documentation.