Newbie: Can't get to the bottom of this error

I have a playbook which is doing my head in. I’ve abstracted the relevant parts that fail into:

---
- name: dummy playbook
  block:
    - name: do a check
      block:
        - name: check if copy required
          lineinfile:
            path: /tmp/myfile
            regexp: '^copy=yes'
            state: present
            check_mode: yes
          register: copy_needed
          failed_when: (copy_needed is changed)

        -name: copy the file
          copy:
            src: ex.cmd
            dest: /tmp/ex.cmd
            mode: 0500
  rescue:
    - name: no copy required
      debug:
        msg: 'copy not needed'

(this isn’t the functionality but it’s failing with the same error)

The requirement is:

  • check if a matching line exists in a file
  • if it does, stop processing this block but don’t fail the entire playbook
  • if it doesn’t, copy a file

The error I’m getting is:

ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
  did not find expected '-' indicator

The error appears to be in '/home/ansible/ansible/roles/progress_odbc/tasks/main.yml': line 15, column 9, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

        -name: copy the file
        ^ here

Could someone point me to the what I’m missing here? Thanks.

Never mind. Just spotted the “-name” rather than “- name”!

Glad you got it sorted.
Depending what text editor you are using, you might be able to enable YAML/Ansible syntax highlighting which can help.

If you use VScode, then the official Red Hat Ansible VScode plugin is really useful

1 Like

Yeah. It was the context-sensitive editor on the forum which highlighted the issue :-/

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.