How Do You Troubleshoot the Ansible Problem “ERROR! Syntax Error while loading YAML. mapping values are not allowed here”?

Problem scenario
You are trying to run an Ansible playbook with the "become: yes" stanza.  You get this error after your ansible-playbook command:

"ERROR! Syntax Error while loading YAML.
  mapping values are not allowed here

The error appears to have been in '/home/cooluser/good.yaml': line 5, column 10, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be..."

What should you do?

Possible Solution #1
Check the indentation of the playbook (i.e., the .yaml file).  Here is an example of incorrect indentation (with hosts and tasks indented too much):

- name: This is a test.
     hosts: contintserver
     tasks:
     - shell:  "free -m > /tmp/memory.txt"

Here is how it should look (without the extra indentation beneath the "- name:" stanza):

- name: This is a test.
  hosts: contintserver
  tasks:
    - shell:  "free -m > /tmp/memory.txt"

Possible Solution #2
Sometimes you cannot have the "become: yes" stanza immediately after the module.

Here is an example of an incorrect order:

- hosts: all
  tasks:
  - copy:
    become: yes
      src: /home/cooluser/swapspace.sh
      dest: /home/cooluser/swapspace.sh
      owner: cooluser
      group: wheel
      mode: 0644

Here is an example of a correct order:

- hosts: all
  tasks:
  - copy:
      src: /home/cooluser/swapspace.sh
      dest: /home/cooluser/swapspace.sh
      owner: cooluser
      group: wheel
      mode: 0644
    become: yes

Possible solution #3

Instead of:

tasks:
- name: foobar3
shell: "free -m > /tmp/memory.txt"

use this:

tasks:
- shell: "free -m > /tmp/memory.txt"

Leave a comment

Your email address will not be published. Required fields are marked *