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
---
- 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
---
- 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
See also: Ansible troubleshooting - Error parser-error
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
See also: Ansible troubleshooting - AWS Failed to import the required Python library (botocore or boto3)
Common Indentation Errors
Error 1: Tasks not indented under tasks:
# ❌ WRONG — task is at same level as tasks key
- name: my playbook
hosts: all
tasks:
- name: install package
ansible.builtin.apt:
name: curl
# ✅ 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
# ❌ WRONG — path is at same level as file module
- name: create file
ansible.builtin.file:
path: /tmp/test
state: touch
# ✅ 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).
# Check for hidden tabs
$ cat -A playbook.yml | grep "\^I"
Error 4: Inconsistent indentation depth
# ❌ 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: "world" # 4 extra spaces instead of 2!
How to Prevent Indentation Errors
1. Use ansible-lint
pip install ansible-lint
ansible-lint playbook.yml
2. Use yamllint
pip install yamllint
yamllint playbook.yml
3. Configure your editor
VS Code — Install the "YAML" extension by Red Hat and add to settings.json:
{
"[yaml]": {
"editor.tabSize": 2,
"editor.insertSpaces": true,
"editor.detectIndentation": false
}
}
vim — Add to .vimrc:
autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab
4. Use --syntax-check before running
ansible-playbook playbook.yml --syntax-check
See also: Ansible Vault Error: Fix 'Attempting to Decrypt but No Vault Secrets Found'
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:
# ❌ Triggers error
msg: Error: something broke
# ✅ Fix with quotes
msg: "Error: something broke"
Related Articles
• switching users with Ansible become • understanding Ansible rolesCategory: installation
Watch the video: Ansible YAML Indentation Error: How to Fix It (Examples) — Video Tutorial