YAML "mapping values are not allowed in this context": a colon, or an indent, in the wrong place
Fix YAML "mapping values are not allowed in this context" in Docker Compose, Kubernetes or Ansible: quote values containing a colon and space, and fix the indentation.
by OhSudo ·
Checked on real software: Docker Compose, kubectl, Ansible, PyYAML · November 2026How we check
yaml: line 4, column 27: mapping values are not allowed in this contextThat’s Docker Compose. kubectl says yaml: line 4: mapping values are not allowed in this context, and Python programs using PyYAML say it like this:
mapping values are not allowed here in "config.yml", line 4, column 27Recent versions of Ansible explain the same problem in their own words:
[ERROR]: YAML parsing failed: Colons in unquoted values must be followed by a non-space character.In YAML, a colon followed by a space means “key: value”. This error means the parser found one where a key can’t start: in the middle of a value, or on a line indented as if it belonged to the line above. There are only two common causes, and the line number takes you to them.
TL;DR
- A value contains
:: put it in quotes,command: "echo a: b". - A line is indented too far: line it up with its neighbours, using spaces only.
Paste the file into the YAML ⇄ JSON converter, which shows the error and its line, and the structure it reads when it works.
1. A colon inside a value
The most common case. Here the value has : in it:
services: web: image: nginx:1.27 command: echo Starting: webnginx:1.27 is fine, as there’s no space after its colon. echo Starting: web isn’t, because Starting: web looks like a new key and value. Quote the whole value:
command: "echo Starting: web"Single quotes work too, and keep backslashes as they are. Messages, commands and passwords that contain : all trigger this.
Try it in the terminal, which has this very file: Compose points at the colon, and after you quote the value, docker compose config shows the command it reads.
Two keys on one line is the same problem: image: nginx command: start. Each key gets its own line.
2. A line indented too far
environment: DB_HOST: db DB_PORT: 5432DB_PORT is indented more than DB_HOST, so YAML reads it as a continuation of the value db, and then finds a : there. Line up keys at the same level:
environment: DB_HOST: db DB_PORT: 5432The line in the message is where the parser noticed, which is usually the line with the extra indent, but can be one later. Look at the line before it, too.
3. A list item indented too far
steps: - name: Build run: makerun is part of the step, so it needs the same indent as name, two spaces after the - . Indented more, it becomes part of the value Build:
steps: - name: Build run: makeThis one often shows up in GitHub Actions and GitLab CI files, and Ansible tasks.
4. Tabs
YAML forbids tabs for indentation. They give a different message, and editors can hide them:
found character that cannot start any tokenMake your editor show whitespace, or find tabs with grep -nP '\t' file.yml. Set it to insert spaces for .yml files.
5. Check before you deploy
Each tool can parse a file without running anything:
docker compose config --quietkubectl apply --dry-run=client -f deploy.yamlansible-playbook --syntax-check playbook.ymlpython3 -c 'import sys, yaml; yaml.safe_load(open(sys.argv[1]))' file.ymlyamllint checks style too, catching indentation that’s valid and still wrong. Run one of these in CI, so a broken file fails before deployment.
The Docker Compose Generator and the Kubernetes Manifest Generator write files that parse, which you can then edit.
Warning
A file that parses isn’t necessarily right. A value shifted by two spaces can quietly become part of another key, and the tool then ignores it, or uses a default. After fixing the error, check the result: docker compose config and kubectl apply --dry-run=client -o yaml print what the tool really understood.
Shell, we have a problem!
Similar, but different: did not find expected key usually means a line is indented too little, or a quote isn’t closed. could not find expected ':' means a key has no colon, often a missing space or a stray line. found character that cannot start any token means a tab, or a value starting with @ or a backtick, which needs quotes. In Ansible, a file that parses and still fails may be couldn’t resolve module/action. YAML that parses but has a key in the wrong place gets unknown field from kubectl. In Docker Compose, valid YAML with an unknown key gets Additional property … is not allowed.
$ ./practice.sh
Practice this in the terminal
Open the Terminal Playground with this article’s scenario loaded. It is safe and runs in your browser.
Found this useful? Share it.
Found a mistake? Let us know at contact@ohsudo.com.