Problem scenario
You run an Ansible playbook but you get an error about "this one looks like it might be an issue with missing quotes." Your Ansible playbook compares two variables with each other.
After running ansible-playbook on the .yaml file, you get this error:
"ERROR! Syntax Error while loading YAML.
expected <block end>, but found '<scalar>'
The error appears to have been in '/home/cooluser/good.yaml': line 12, column 36, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- shell: echo "THIS STATEMENT IS EXECUTED" > /tmp/printer.txt
when: "{{ ansible_hostname }}" == "{{ ansible_nodename }}"
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}""
How do you get the comparison to work?
Solution
Put single quotes around the entire expression including the == (or !=) symbol.
Example of an incorrect when (if) statement:
- shell: echo "THIS STATEMENT IS EXECUTED" > /tmp/printer.txt
when: "{{ ansible_hostname }}" == "{{ ansible_nodename }}"
The above has two variables on each side of an equivalence condition. With Ansible 2.1, it will throw an error.
Here is an example of a correct when: statement (with single quotes around everything except the "when:"):
- shell: echo "THIS STATEMENT IS EXECUTED" > /tmp/printer.txt
when: '"{{ ansible_hostname }}" == "{{ ansible_nodename }}"'