Kapitan is an open-source tool that generates Kubernetes manifests, Terraform JSON, scripts and documentation from one hierarchical inventory, using Jsonnet, Jinja2, Kadet or Helm templates. The help text above lists its subcommands. This page explains what each one does and shows the flags you will use most, and it includes a small working project so you can try compile, inventory, refs, lint and validate for yourself.
usage: kapitan [-h] [--version] {eval,e,compile,c,inventory,i,searchvar,sv,secrets,s,refs,r,lint,l,init,validate,v} ...
Generic templated configuration management for Kubernetes, Terraform and other things
positional arguments:
{eval,e,compile,c,inventory,i,searchvar,sv,secrets,s,refs,r,lint,l,init,validate,v}
commands
eval (e) evaluate jsonnet file
compile (c) compile targets
inventory (i) show inventory
searchvar (sv) show all inventory files where var is declared
secrets (s) (DEPRECATED) please use refs
refs (r) manage refs
lint (l) linter for inventory and refs
init initialize a directory with the recommended kapitan project skeleton.
validate (v) validates the compile output against schemas as specified in inventory
optional arguments:
-h, --help show this help message and exit
--version show program's version number and exitCode language: JavaScript (javascript)
What Kapitan does and how the commands fit together
Kapitan is built around targets. A target is one YAML file under inventory/targets/, and it usually stands for one environment or deployment, such as dev or prod-eu. A target pulls in reusable classes from inventory/classes/ and sets parameters. When you run kapitan compile, Kapitan merges those parameters into a single inventory for each target. It then renders the templates listed under parameters.kapitan.compile and writes the results to compiled/<target>/.
The subcommands in the help output fall into three groups:
| Purpose | Commands | Aliases |
|---|---|---|
| Create and render | init, compile, eval | c, e |
| Inspect the inventory | inventory, searchvar | i, sv |
| Secrets and quality | refs, lint, validate (secrets is deprecated) | r, l, v |
Note: Kapitan is not part of Terraform, even though this post sits in the Terraform category. It can write Terraform configuration as .tf.json files, which terraform init, plan and apply then read from the compiled directory.
Installing Kapitan
Kapitan is a Python package. You can install it with pip or run the official container image. Use one of these two options:
# Option 1: pip (Python 3)
pip3 install --user --upgrade kapitan
export PATH="$HOME/.local/bin:$PATH"
kapitan --version
# Option 2: Docker (run from your project root)
alias kapitan='docker run -t --rm -u $(id -u) -v $(pwd):/src:delegated kapicorp/kapitan'
kapitan --versionCode language: PHP (php)
The Docker image works in /src, so mount your project there. The -u $(id -u) option makes the compiled files belong to your user instead of root.
Command reference with examples
| Command | Common usage | What it does |
|---|---|---|
init | kapitan init | Creates a recommended project skeleton (inventory, components, templates, lib). Flags differ between releases, so check kapitan init --help. |
compile / c | kapitan compile or kapitan compile -t dev prod | Compiles every target, or only the targets listed after -t/--targets. Output goes to compiled/ unless you set --output-path. |
inventory / i | kapitan inventory -t dev | Prints the fully merged inventory for a target. Leave out -t to print all targets. Add --flat for dotted keys. |
searchvar / sv | kapitan searchvar parameters.namespace | Lists every inventory file that declares the given variable. Use it to find where a value is overridden. |
eval / e | kapitan eval file.jsonnet --output yaml | Evaluates one Jsonnet file and prints the result without compiling any target. |
refs / r | kapitan refs --write gpg:dev/db_pass -f pass.txt and kapitan refs --reveal -f <compiled file> | Writes encrypted or referenced secrets to refs/ and reveals them in compiled files. |
lint / l | kapitan lint --fail-on-warning | Looks for unused classes, YAML problems and, with --search-secrets, possible plain-text secrets. |
validate / v | kapitan validate | Checks compiled Kubernetes manifests against the schemas configured under parameters.kapitan.validate. |
secrets / s | (do not use) | Deprecated alias for the old secrets workflow. Use refs instead. Newer releases may not include it. |
Every subcommand has its own help, for example kapitan compile --help. These compile flags are useful in CI: -p/--parallelism sets how many targets compile in parallel, --fetch downloads external dependencies such as Helm charts or Git sources listed in the inventory, and --inventory-path points to an inventory outside the default ./inventory.
Hands-on: a minimal project
This project needs only three files. It renders one ConfigMap for a dev target from a shared class and a Jinja2 template.
mkdir -p kapitan-demo/inventory/classes kapitan-demo/inventory/targets kapitan-demo/templates
cd kapitan-demo
# inventory/classes/common.yml
parameters:
log_level: info
kapitan:
compile:
- output_path: manifests
input_type: jinja2
input_paths:
- templates/configmap.ymlCode language: PHP (php)
# inventory/targets/dev.yml
classes:
- common
parameters:
namespace: dev
log_level: debug # overrides the value from the common class
kapitan:
vars:
target: devCode language: PHP (php)
# templates/configmap.yml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
namespace: {{ inventory.parameters.namespace }}
data:
LOG_LEVEL: "{{ inventory.parameters.log_level }}"
- Run
kapitan inventory -t devand confirm thatlog_levelisdebug. This shows that values set in the target override values from its classes. - Run
kapitan searchvar parameters.log_level. Bothcommon.ymlanddev.ymlshould appear in the output. - Run
kapitan compile. The output looks similar toCompiled dev (0.05s). - Check the result with
cat compiled/dev/manifests/configmap.yml. It should containnamespace: devandLOG_LEVEL: "debug". - Apply it with
kubectl apply -f compiled/dev/manifests/if you have a cluster.
Note: Commit the compiled/ directory to Git. Kapitan recommends this so that pull request diffs show exactly what will change in your clusters or cloud accounts.
Managing secrets with refs
Refs keep secrets out of the inventory. You store an encrypted value under refs/, and the inventory or a template points to it with the ?{backend:path} syntax. Compiled files contain only a hashed reference until you reveal them.
# Store a value with the base64 backend (encoding only, for demos)
echo -n 's3cr3t' | kapitan refs --write base64:dev/db_password -f -
# Reference it in the inventory:
# parameters:
# db_password: ?{base64:dev/db_password}
kapitan compile
# Print the compiled file with real values substituted
kapitan refs --reveal -f compiled/dev/manifests/configmap.ymlCode language: PHP (php)
For real secrets, use an encrypting backend: gpg (with --recipients), gkms, awskms or azkms (with --key), or vaultkv. With -t <target>, Kapitan reads the key or recipients from that target's parameters.kapitan.secrets section, so you do not have to repeat them on every command.
Note: base64 and plain refs are not encrypted. Do not use them for production credentials. Only run refs --reveal in a trusted pipeline step, and never commit the revealed output.
Common errors and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
kapitan: command not found after pip install | ~/.local/bin is not on PATH | Add it to PATH, or use a virtualenv or the Docker image. |
| Class not found error during compile | A name under classes: does not match a file path | component.nginx maps to inventory/classes/component/nginx.yml. Check the spelling and the folders. |
| Template renders empty or wrong values | Wrong inventory key path | Run kapitan inventory -t <target> to see the actual structure. |
refs --reveal fails | Missing GPG private key or cloud KMS credentials | Import the key or authenticate to the cloud provider in that shell. |
| Compiled files owned by root | Docker run without -u $(id -u) | Add the user flag, or run chown on compiled/. |
Best practices
- Keep targets small. Put shared defaults in classes and override only what differs in each target.
- In CI, run
kapitan lint --fail-on-warning,kapitan compileandkapitan validate, then fail the build ifgit diff --exit-code compiled/shows uncommitted changes. - Pin the Kapitan version (a pip version or a Docker image tag) so every engineer and pipeline produces identical output.
- For Terraform output, run the usual Terraform checks on the generated directory. See pre-commit for Terraform and TFSec scanning.
Frequently asked questions
What is Kapitan used for?
Kapitan generates configuration files, such as Kubernetes manifests, Terraform JSON, scripts and docs, from one hierarchical inventory. Many environments can then share the same templates and differ only in their parameters.
How do I compile only one Kapitan target?
Run kapitan compile -t <target_name>, where the target name matches the file name in inventory/targets/. You can list several targets after -t.
Is kapitan secrets still supported?
No. The secrets command is deprecated and replaced by kapitan refs, which supports the same backends and more. Newer releases may not include secrets at all.
Can Kapitan replace Terraform?
No. Kapitan can write Terraform configuration as .tf.json files, but you still run Terraform to plan and apply them.
Where does Kapitan write its output?
By default, compiled files go to compiled/<target>/<output_path>, where output_path comes from the target's parameters.kapitan.compile entries. You can change the base directory with --output-path.
Related tutorials
- Terraform Tools for Developer Productivity Master Guide
- How to Configure pre-commit for Terraform: Complete DevOps Guide with 15 Practical Use Cases, Commands, Security Checks, and Best Practices
- Terraform Tutorials: TFSec for Security Scanning
- More Terraform tutorials
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals