DevOps Bootcamp · Lesson

Conditional Blocks with if and else

Rendering resources only when configured.

Lesson 1 of 413 steps

Conditional Blocks with if and else is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.

Render Only When Needed

Sometimes a resource should appear only if the user asked for it. Helm lets you wrap that YAML in a conditional so it renders on demand.

The if Action

An if block starts with {{ if SOMETHING }} and closes with {{ end }}. Everything in between renders only when the condition is true.

{{ if .Values.ingress.enabled }}
# ingress yaml here
{{ end }}

Driven by Values

The condition is usually a value the user sets. Flip ingress.enabled to true in values.yaml and the wrapped manifest suddenly appears.

ingress:
  enabled: true

Always Close With end

Every if must be paired with an {{ end }}. Forget it and Helm throws an unterminated-block error when it tries to render the chart.

Adding an else

Want a fallback? Add an {{ else }} branch. Helm renders the first block when true, otherwise it renders the else block instead.

{{ if .Values.tls }}
protocol: HTTPS
{{ else }}
protocol: HTTP
{{ end }}

Chaining With else if

For several cases, chain conditions with {{ else if }}. Helm checks each in order and renders the first branch whose condition is true.

{{ if eq .Values.tier "gold" }}
replicas: 5
{{ else if eq .Values.tier "silver" }}
replicas: 3
{{ else }}
replicas: 1
{{ end }}

What Counts as True

An if is true unless the value is empty: false, zero, an empty string, or an empty list all read as false in a condition.

Guarding Optional Fields

A common pattern wraps one optional field, not a whole resource. Here the annotation only renders when the user provides a value for it.

{{ if .Values.email }}
contact: {{ .Values.email }}
{{ end }}

Mind the Blank Lines

An if block leaves behind blank lines where the action used to sit. That whitespace can break YAML, so you will soon learn to trim it. 🧹

Preview the Branches

Render with different values and watch which branch wins. helm template with --set lets you confirm each condition before you ever deploy.

helm template ./mychart --set ingress.enabled=true

Default a Missing Flag

If a user never sets the flag, it reads as empty and your if stays false. That is why charts ship a sensible default for it in values.yaml.

ingress:
  enabled: false

Quick Check

You write {{ if .Values.cache }} but the chart fails to render.

Recap

You can now gate YAML with if, add else and else if branches, and remember the closing {{ end }}. Conditional rendering unlocked! 🎯

Free to start

Learn DevOps Bootcamp with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
142
Lessons
568

Frequently Asked Questions

Is the “Conditional Blocks with if and else” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Conditional Blocks with if and else”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.

What will I learn in “Conditional Blocks with if and else”?

Rendering resources only when configured. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Conditional Blocks with if and else” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Conditional Blocks with if and else
  2. Truthiness and Operators in Conditions
  3. Looping Over Lists and Maps with range
  4. Scoping a Block with with
← Back to DevOps Bootcamp