YAML error: mapping values are not allowed here (in this context)

In YAML, a colon followed by a space separates a key from its value. When a second : appears where the parser is already reading a plain value, it looks like an attempt to start a new mapping in a place where none is allowed. The cause is almost always an unquoted value that contains : , or a line indented under a key that already has a value.

Seen as:

  • yaml.scanner.ScannerError: mapping values are not allowed here
  • yaml: line 2: mapping values are not allowed in this context
  • error converting YAML to JSON: yaml: line 2: mapping values are not allowed in this context
  • YAMLException: bad indentation of a mapping entry (2:21)
  • Nested mappings are not allowed in compact mappings at line 2, column 10

Input

Settings

History

Load from URL

Common causes

1. An unquoted value that contains ": "

Commands, messages and titles often contain a colon followed by a space. Quote the whole value. A colon without a following space, as in nginx:1.27 or a URL, is fine unquoted.

Before
command: echo Status: ready
After
command: "echo Status: ready"

2. A key indented under a key that already has a value

Once image: nginx has a value on the same line, it cannot also have children. The extra indentation makes ports look like a nested mapping inside a string. Align sibling keys.

Before
image: nginx
  ports: 80
After
image: nginx
ports: 80

3. A long text continued on the next line

A plain value may continue on more-indented lines, but a continuation line containing : is read as a key. Use a block scalar: > folds lines together, | keeps the line breaks.

Before
description: Deploys the web tier
  and notes: run migrations first
After
description: >
  Deploys the web tier
  and notes: run migrations first

4. Windows paths or times after a key

Values such as C: drive or Note: 10:30 contain a colon and a space. Single quotes are the safest choice here because backslashes stay literal inside them.

Before
backup_target: D: backups\nightly
After
backup_target: 'D: backups\nightly'

Frequently asked questions

Why is "image: nginx:1.27" fine but "echo Status: ready" not?

Only a colon followed by a space (or the end of the line) is a key indicator. In nginx:1.27 and https://example.com the colon is followed by a non-space character, so it is part of the value.

Should I use single or double quotes?

Single quotes keep backslashes literal, and you write a single quote as two. Double quotes support escapes such as \n and \t. For plain text containing ": " either works.

Why does the line number point after the real problem?

The parser only notices the second colon when it reaches it, which may be on a continuation line. Check the key on the line above too, especially its indentation.

Related