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) — Video Tutorial

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

Watch Video

Watch "Ansible YAML Indentation Error: How to Fix It (Examples)" on YouTube

What You'll Learn

Full Tutorial Content

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 ```yaml --- - name: blockinfile module demo hosts: all become: true tasks: - name: Generate /etc/hosts file ansible.builtin.blockinfile: state: present dest: /etc/hosts content: | 192.168.0.200 Playbook demo.example.com ``` Fix ```yaml --- - name: blockinfile module demo hosts: all become: true tasks: - name: Generate /etc/hosts file ansible.builtin.blockinfile: state: present dest: /etc/hosts content: | 192.168.0.200 Playbook demo.example.com ``` [code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/troubleshooting) 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 1. **Never use tabs** — YAML only accepts spaces 2. **Be consistent** — pick 2 spaces (Ansible standard) and stick with it 3. **List items (`-`) must be at the same indentation level** as their siblings 4. **Key-value pairs under a mapping** must be indented deeper than the parent Common Indentation Errors Error 1: Tasks not indented under `tasks:` ```yaml ❌ WRONG — task is at same level as tasks key - name: my playbook hosts: all tasks: - name: install package ansible.builtin.apt: name: curl ``` ```yaml ✅ CORRECT — task is indented under tasks - name: my playbook hosts: all tasks: - name: install package ansible.builtin.apt: name: curl ``` Error 2: Module parameters not indented under module name ```yaml ❌ WRONG — path is at same level as file module - name: create file ansible.builtin.file: path: /tmp/test state: touch ``` ```yaml ✅ CORRECT — path is indented under file module - name: create file ansible.builtin.file: path: /tmp/test state: touch ``` 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`). ```bash Check for hidden tabs $ cat -A playbook.yml | grep "\^I" ``` Error 4: Inconsistent indentation depth ```yaml ❌ WRONG — mixed 2-space and 4-space indentation - name: my playbook hosts: all tasks: - name: first task ansible.builtin.debug: msg: "hello" - name: second task ansible.builtin.debug: msg: "w

About This Tutorial

Read the full written article: Ansible YAML Indentation Error: How to Fix It (Examples)

Topics Covered

Related Video Tutorials