weijie lin
weijie lin

Reputation: 153

YAML Syntax error (Ansible 2.2.1.0)

I'm self-learning Ansible with YAML. I wrote a small test to test the hostname in my environment.

   ---
     - hosts: all
       tasks:
         - name: get server hostname
           command: hostname

but when I ran ansible-playbook playbooks/hostname.yml it gave me the error below:

ERROR! Syntax Error while loading YAML.


The error appears to have been in '/home/ansible/work/ansible/playbooks/hostname.yml': line 5, column 1, but may be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

      - name: get server hostname
        command: hostname 
^ here

I've double checked there is no extra new line. Also, when I remove

name: get server hostname

It works perfectly fine for me. Could anyone point it out what might cause this issue?

Upvotes: 0

Views: 322

Answers (1)

techraf
techraf

Reputation: 68439

Parsing YAML is sensitive to indentation, so what you posted can be both: correct and incorrect depending on the number of spaces in front. Unless you were very precise it is hard to answer.

Most likely cause for an error in column 1 is a character other than space, however if there was a tab, the error message would indicate that directly.

Use the following code to avoid problems. Copy it to a new file and re-run.

---
- hosts: all
  tasks:
    - name: get server hostname
      command: hostname

Besides, your task is not the correct way to acquire hostname in Ansible, although it might work.

Upvotes: 1

Related Questions