AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible YAML Indentation Error: How to Fix It (Examples)

By Luca Berton · Published 2024-01-01 · Category: installation

Fix Ansible YAML indentation errors fast. Learn the root causes, common patterns, and how to prevent indentation issues in playbooks with practical examples.

Today we’re going to talk about Ansible troubleshooting and specifically about indentation errors. I’m Luca Berton and welcome to today’s episode of Ansible Pilot

Demo The best way of talking about Ansible troubleshooting is to jump in a live Playbook to show you practically the error and how to solve it!

Error

Fix

code with ❤️ in GitHub

Conclusion Now you know better how to troubleshoot the most common Ansible error about indentation.

Understanding YAML Indentation in Ansible

YAML (YAML Ain't Markup Language) is whitespace-sensitive. Ansible playbooks are written in YAML, which means indentation determines the structure of your configuration. Unlike Python (which uses 4 spaces by default), YAML is flexible about the number of spaces, but you must be consistent.

The Golden Rules Never use tabs — YAML only accepts spaces Be consistent — pick 2 spaces (Ansible standard) and stick with it List items (-) must be at the same indentation level as their siblings Key-value pairs under a mapping must be indented deeper than the parent

Common Indentation Errors

Error 1: Tasks not indented under tasks:

Error 2: Module parameters not indented under module name

Error 3: Mixing tabs and spaces

This is invisible but devastating. Your editor might insert tabs that look like spaces. Use cat -A playbook.yml to reveal tabs (shown as ^I).

Error 4: Inconsistent indentation depth

How to Prevent Indentation Errors

1. Use ansible-lint

2. Use yamllint

3. Configure your editor

VS Code — Install the "YAML" extension by Red Hat and add to settings.json:

vim — Add to .vimrc:

4. Use --syntax-check before running

FAQ

Why does Ansible use YAML?

YAML is human-readable, widely supported, and doesn't require closing tags or brackets. However, its whitespace sensitivity means indentation errors are the #1 most common Ansible problem.

Can I use 4 spaces instead of 2?

Yes, but the Ansible community standard is 2 spaces. The important thing is consistency within a file.

How do I fix "mapping values are not allowed here"?

This almost always means a colon (:) in a value that YAML is interpreting as a key-value separator. Either quote the value or fix the indentation:

Related ArticlesAnsible Become GuideAnsible Roles Guide

Category: installation

Watch the video: Ansible YAML Indentation Error: How to Fix It (Examples) — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home