How Do You Troubleshoot the Ansible Error “Syntax Error while loading YAML expected <block end>, but found ‘-‘”?

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.
  expected <block end>, but found '-'

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

The offending line appears to be:

  become: yes
  - copy:
  ^ here"

How do you get your playbook to run?

Solution
Put the "become: yes" stanza under the module -- not under the "tasks:" statement.

Here is an example of an incorrect order:

- hosts: all
  tasks:
  become: yes
  - copy:
      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

Leave a comment

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