Skip to content
DevOps AI ToolKit
Newsletter
All guides
AI for Ansible By James Joyner IV · · 9 min read

Ansible Error Guide: 'Syntax Error while loading YAML' — Fix Bad YAML

Quick answer

Fix Ansible's 'Syntax Error while loading YAML: mapping values are not allowed in this context' error: unquoted colons, bad indentation, tabs, and unbalanced Jinja braces.

Part of the Ansible Playbook & Module Errors hub
  • #ansible
  • #automation
  • #troubleshooting
  • #errors
Free toolkit

Fixing errors like this? Get 500 free DevOps AI prompts

500 copy-paste AI prompts for the stack you actually run — one PDF, free.

Overview

This error means Ansible could not even parse your playbook, vars file, or inventory as YAML — the file is malformed before any task logic is considered. The most common variant is an unquoted colon inside a value, which YAML reads as a second key/value mapping where it does not belong.

The literal message:

ERROR! Syntax Error while loading YAML.
  mapping values are not allowed in this context

The error appears to be in '/home/deploy/site/roles/web/tasks/main.yml': line 12, column 20, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  - name: Set greeting
    msg: Welcome to: prod
                    ^ here

Ansible prints the file, line, column, and a caret under where the parser gave up. Note the standard hedge — “but may be elsewhere in the file” — because a mistake several lines up (a missing quote, a stray -) often surfaces only when the parser reaches a later line.

Symptoms

  • The playbook fails instantly with Syntax Error while loading YAML, before the play header prints.
  • A caret (^ here) points at a colon, quote, or brace.
  • The message names a line/column but the real error is a few lines above.
  • ansible-lint or an editor’s YAML plugin flags the same file.
  • The file “looks fine” but was edited on a system that inserted tab characters.
ansible-playbook -i inventory.ini site.yml
ERROR! Syntax Error while loading YAML.
  mapping values are not allowed in this context

Common Root Causes

1. Unquoted colon inside a value

A value containing : (colon-space) is parsed as a nested mapping. This is the single most common trigger.

- name: Print message
  ansible.builtin.debug:
    msg: Deploying build: 42 to prod   # the "build: 42" reads as a mapping
mapping values are not allowed in this context

Fix — quote the whole scalar:

    msg: "Deploying build: 42 to prod"

2. Bad or inconsistent indentation

Mixing indentation levels, or under-indenting a list item, breaks the block structure.

tasks:
- name: Install nginx
   ansible.builtin.apt:      # 3 spaces here, 2 elsewhere
     name: nginx
Syntax Error while loading YAML.

3. Tab characters

YAML forbids tabs for indentation. A tab pasted from another editor produces a parser error even though it looks like spaces.

found character '\t' that cannot start any token

4. Unbalanced or unquoted Jinja braces at the start of a value

A value that begins with {{ must be quoted, or YAML tries to parse it as a flow mapping.

    dest: {{ app_home }}/config   # starts with { -> YAML flow mapping
Syntax Error while loading YAML.

Fix:

    dest: "{{ app_home }}/config"

5. Unclosed quote or bracket earlier in the file

A missing closing " or ] makes the parser consume following lines until it fails — which is why the reported line is often below the real mistake.

Diagnostic Workflow

Step 1: Do a syntax-only parse

Ask Ansible to parse without running anything. This is the fastest confirmation and prints the same locator:

ansible-playbook -i inventory.ini site.yml --syntax-check
ERROR! Syntax Error while loading YAML.
  mapping values are not allowed in this context
The error appears to be in '.../roles/web/tasks/main.yml': line 12, column 20

Step 2: Read the caret, then look above it

The caret marks where parsing failed, but per Ansible’s own hint the cause is often earlier. Open the file at the reported line and scan upward for an unquoted colon, an unclosed quote, or a bad indent.

Step 3: Validate the raw YAML independently

Take Ansible out of the loop and let a pure YAML parser point at the structural fault:

python3 -c "import yaml,sys; yaml.safe_load(open('roles/web/tasks/main.yml'))"
yaml.scanner.ScannerError: mapping values are not allowed here
  in "roles/web/tasks/main.yml", line 12, column 20

Step 4: Hunt for tabs and trailing structure

Make invisible characters visible — tabs are a frequent, hard-to-see cause:

grep -nP '\t' roles/web/tasks/main.yml
9:	    name: nginx

Step 5: Lint the whole tree before re-running

ansible-lint catches YAML faults plus risky patterns across every file, not just the first one that fails:

# .ansible-lint (minimal)
# profile: basic
# exclude_paths:
#   - .cache/
ansible-lint roles/web/

Example Root Cause Analysis

A role that had worked for months started failing after an “unrelated” edit:

ERROR! Syntax Error while loading YAML.
  mapping values are not allowed in this context
The error appears to be in '.../roles/app/tasks/deploy.yml': line 24, column 15

Line 24 looked correct — a plain become: true. Following Step 2 and reading upward, line 21 was the real culprit:

- name: Record deploy note
  ansible.builtin.set_fact:
    note: Rollback plan: revert to previous tag   # unquoted "plan: revert"
- name: Escalate
  become: true

The value Rollback plan: revert to previous tag contains a colon-space, so YAML parsed plan: revert... as a nested mapping. The parser only reported an error when it reached the next - list item on line 24, which now appeared in an illegal context.

Root cause: an unquoted colon in a free-text value, three lines above the reported line. Fix — quote the scalar:

    note: "Rollback plan: revert to previous tag"

--syntax-check then passed, and the play ran normally.

Prevention Best Practices

  • Quote any scalar that contains a colon-space, a leading {/[, #, @, or a value beginning with {{ — these are the characters that turn a string into structure.
  • Configure your editor to show whitespace and to convert tabs to spaces for .yml/.yaml files.
  • Run ansible-playbook --syntax-check (or ansible-lint) in a pre-commit hook so malformed YAML never reaches a run.
  • Keep indentation consistent (two spaces is conventional) and never mix tabs and spaces in the same file.
  • When the reported line looks fine, always scan upward for an unclosed quote or bracket — the parser fails late, not at the mistake.
  • For a file that resists eyeballing, the YAML validators on this site parse it client-side and point at the fault. See more in the Ansible guides.

Quick Command Reference

# Parse only, no execution
ansible-playbook -i inventory.ini site.yml --syntax-check

# Validate raw YAML with a pure parser
python3 -c "import yaml; yaml.safe_load(open('roles/web/tasks/main.yml'))"

# Find tab characters used for indentation
grep -nP '\t' roles/web/tasks/main.yml

# Lint the whole role/tree
ansible-lint roles/web/

# Common fixes:
#   msg: "Deploying build: 42"      # quote colon-space values
#   dest: "{{ app_home }}/config"   # quote leading {{ }}

Conclusion

Syntax Error while loading YAML means the file is not valid YAML — Ansible stops at parse time, before any task runs. The reported line and caret show where the parser failed, but the cause is frequently a few lines above. The usual root causes:

  1. An unquoted colon-space inside a value (read as a nested mapping).
  2. Inconsistent indentation or an under-indented list item.
  3. Tab characters used for indentation.
  4. A value starting with {{ or { that was not quoted.
  5. An unclosed quote or bracket earlier in the file.

Run --syntax-check, read the caret and then look upward, validate with a pure YAML parser, and quote scalars that contain structural characters — that turns a cryptic parse failure into a one-line fix.

Free download · 368-page PDF

Get 500 Battle-Tested DevOps AI Prompts — Free

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.