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.
What You'll Learn
- Demo
- Error
- Fix
- Conclusion
- Understanding YAML Indentation in Ansible
- The Golden Rules
- 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
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
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 4 min
- Category: installation
Read the full written article: Ansible YAML Indentation Error: How to Fix It (Examples)
Related Video Tutorials
- Ansible 'Failed to Connect via SSH localhost:22': Fix Guide — Fix Ansible 'failed to connect to the host via ssh localhost port 22' error. Resolve SSH config, connection type, host key, and authentication issues.
- Ansible Vault Error: 'Attempting to decrypt but no vault secrets found' Fix — Fix Ansible error 'Attempting to decrypt but no vault secrets found'. Provide vault password via --ask-vault-pass, password file, or environment variable.
- Ansible troubleshooting - Destination does not exist rc 257 — Troubleshoot the "Destination does not exist" error (return code 257) in Ansible with Luca Berton on Ansible Pilot! Learn to fix file path issues effectively.
- Ansible troubleshooting - Error markupsafe — Resolve the No module named markupsafe error in Ansible by reinstalling Ansible, ansible-lint, and python-markupsafe via Homebrew.
- Ansible troubleshooting - Module Failure on Windows-target — Discover how to troubleshoot Module Failure on Windows-target in Ansible, focusing on resolving execution errors effectively.
- Ansible Permission Denied (Errno 13): Fix File Access Errors — Fix Ansible Permission denied [Errno 13] errors. Resolve file permission, become/sudo, SELinux, and directory access issues with troubleshooting steps.