WoJ WoJ - 6 days ago 8
YAML Question

Is a correct YAML file enough for a correct ansible playbook, syntax errors aside?

I have an ansible playbook which raises an error (with a dreadful message, as usual):

ERROR! unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>

The error appears to have been in '/root/myplaybook.yml': line 17, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

# configure rsyslog
- name: configure rsyslog to expose events on port 42000
^ here


The line in question is typical of other lines I have in this and other playbooks:

# prepare environment
# configure rsyslog
- name: configure rsyslog to expose events on port 42000
lineinfile:
- create: yes
- dest: /etc/rsyslog.d/expose-42000.conf
- line: "*.* @127.0.0.1:42000"
notify:
- restart rsyslog


The file is validated by three online checkers, so there are no YAML errors. Is this fact enough for the file to be a correct ansible playbook?

What I am trying to understand is whether a correct YAML file leaves me only with ansible syntax errors (a module which does not exist for instance) or is a playbook an extension of YAML (in the sense that a line like
- name: blah blah blah
is OK from a YAMl perspective, but will be rejected by ansible because (I am making up an example) it has more than two words.

In other words I am checking if the following can be true: the YAML syntax is OK, the ansible keywords are OK but ansible does not conform to YAML syntax fully by having some limitations.

EDIT: I had an error, spotted by Konstantin in his answer.
I will leave this question in place since it helped me to understand that ansible does not put constraints on the YAML file itself, so when there is an error and the validation goes though I am really left with specific ansible syntax errors (or logical, like in my case).

Answer

No, valid YAML is not necessary gives you valid Ansible playbook.

The error message is unexpected parameter type in action.
So Ansible can't interpret what you want. Specifically:

  lineinfile:
    - create: yes
    - dest: /etc/rsyslog.d/expose-42000.conf
    - line: "*.* @127.0.0.1:42000"

create, dest and line are parameters of lineinfile action, but you try to feed list of three dicts into lineinfile, because you prepend each parameter with a dash. This is correct from YAML syntax perspective, but Ansible can't parse it because you are not supposed to feed lists into actions.

To correct the error, remove dashes before this parameters.

Comments