Hello Developer Community!
I would like to ask for some help about the following, I have the following YAML data:
---
# yamllint disable rule:indentation rule:empty-lines
config_nsapp_cs_policy:
nsapp_cs_policy:
- policyname: "url_app_preprd"
rule: "URL == \'/string/*\'"
When trying to run YAML lint against the YAML file, I get the following error:
7:69 error syntax error: found unknown escape character "'" (syntax)
Is it possible to define an exception rule for this specific linting rule (ignoring the problematic content in 'single quote')? I was thinking about completely disable / ignore YAML linting for the file as a whole, but it would not be the best approach. I do not know which YAML linting rule the single quote is matching. The single quote is expected in the line.
Thank You very much in advance!
This is not a linter error, this is a parser error. Your input is invalid YAML because an escape sequence \' is not defined in YAML.
If the scalar content should simply contain single quotes, do
rule: "URL == '/string/*'"
If the scalar content should also contain the backslashes, do
rule: "URL == \\'/string/*\\'"
You can use a block scalar instead to avoid escaping the backslash:
rule: >-
URL == \'/string/*\'
Related
I have a .yaml file like this:
title: 'We'll do cool stuff'
draft: true
However, I get the following error:
Error parsing YAML: YAMLException: can not read a block mapping entry;
a multiline key may not be an implicit key at line 2, column 6:
draft: true
^
How can I fix it?
Note: this setup seems different than the other questions where this same error was raised, including the following posts:
Error parsing YAML
Getting following error on serverless.yaml
yaml syntax issues?
You can use a site like YAML Formatter to format and validate your yaml:
In this case, the error message and location is a bit of red-herring.
The error is actually caused by a string that was accidentally terminated because of an unescaped quote symbol within the string. A hint for this is the syntax highlighting of 'We'll do cool stuff'.
To fix, in this case, you can just skip wrapping the string quotes and rewrite like this:
title: We'll do cool stuff
draft: true
Further Reading
Do I need quotes for strings in YAML?
How to escape double and single quotes in YAML
title: "We'll do cool stuff"
draft: true
I have got the exact same issue, The problem with it is, we are using a single quote' in between the string and also wrapping the string with a single quote.
I resolved it by wrapping the string with a double quote.
You can also trace the issue more by reading this
I'm trying to get an effect similar to this article: https://gryzli.info/2017/12/21/ansible-debug-print-variables/
Which means in my case:
- name: Gather facts
vars:
msg: |
"{{ansible_distribution}}"
"{{ansible_distribution_major_version}}"
"{{ansible_distribution_release}}"
"{{ansible_distribution_version}}"
The problem ist that without quotes it throws an error to add the quotes. With quotes it throws the same error:
We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when theystart a value....
How do I correctly escape this multiline string so that Ansible can parse it? Or does this kind of code no longer work?
Q: "How do I correctly escape this multiline string so that Ansible can parse it?"
A: The problem is the indentation of the block. Quoting from Example 8.3. Invalid Block Scalar Indentation Indicators
ERROR:
A leading all-space line must not have too many spaces.
A following text line must not be less indented.
The text is less indented than the indicated level.
The correct syntax is (with or without quotation)
vars:
msg: |
"{{ansible_distribution}}"
"{{ansible_distribution_major_version}}"
"{{ansible_distribution_release}}"
"{{ansible_distribution_version}}"
This question already has answers here:
Quotes in ansible lineinfile
(4 answers)
Closed 4 years ago.
ERROR! Syntax Error while loading YAML.
did not find expected key
The error appears to have been in '/etc/ansible/main.yml': line 73, column 50, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Ensure IP forwarding is disabled
shell: "sysctl net.ipv4.ip_forward ; grep "net\.ipv4\.ip_forward" /etc/sysctl.conf /etc/sysctl.d/*"
^ here
We could be wrong, but this one looks like it might be an issue with
unbalanced quotes. If starting a value with a quote, make sure the
line ends with the same set of quotes. For instance this arbitrary
example:
I using grep with "" into shell module in ansible playbook and got an ERROR message as follow "ERROR! Syntax Error while loading YAML."
- name: Ensure IP forwarding is disabled
shell: "sysctl net.ipv4.ip_forward ; grep "net\.ipv4\.ip_forward" /etc/sysctl.conf /etc/sysctl.d/*"
register: CIS_3.1.1
ignore_errors: True
That configuration file for ansible is in the YAML format and in YAML a scalar that represents a string can be in multiple formats:
plain: no quotes, has restrictions on the start character and internal character sequences, no escapes
single quoted: can contain double quotes, no escapes except for repeating single quotes
double quoted: backslash escapes in string, in string double quotes need to be escaped
literal: newlines are preserved, no escapes
folded: newlines are converted to spaces, no escapes
You are using double quoted style, and in that you would need to escape the internal double quotes (") and backslashes (\). That gets ugly and unreadble real soon. It is much more useful to use literal style in such cases:
- name: Ensure IP forwarding is disabled
shell: |-
sysctl net.ipv4.ip_forward ; grep "net\.ipv4\.ip_forward" /etc/sysctl.conf /etc/sysctl.d/*
I.e. you put |- (the minus is to strip the final newline of the following line), then put the line without starting or end quotes, indented, on the next line.
I am trying to store an expression language in YAML file
"name": "${foo.data:toLower().equals('hello')}"
I tried putting '\' in front of '$' and {}, but it doesn't work.
I just want to set the "name" key to the expression language above.
Failed attempts results name key to get a property with "toLower().equals('hello')"
If you put a backslash (\) in front of the $ within the double quoted string, you should get an error because \$ is an unknown escape sequence in YAML.
If you don't get that error, your parser is broken, but you might be able to get around that by using double backslashes:
"name": "\\${foo.data:toLower().equals('hello')}"
(you may also need them for the { and })
When pasting this YAML file into an online yaml parser, I got an expected block end error:
ADDATTEMPTING: 'Tentative d ajout '
ATTEMPTINGTOGIVE: 'Tenter de donner '
ATTEMPTINGTOSET1: 'Tentative de définition '
ATTEMPTINGTOSET2: ' avec '
ALREADYEXISTS: 'Erreur. Package existe déjà’
CANCEL1: 'Annulation...'
(...)
Error
ERROR:
while parsing a block mapping
in "<unicode string>", line 1, column 1:
ADDATTEMPTING: 'Tentative d ajout '
^
expected <block end>, but found '<scalar>'
in "<unicode string>", line 6, column 11:
CANCEL1: 'Annulation...'
^
The line starting ALREADYEXISTS uses ’ as the closing quote, it should be using '. The open quote on the next line (where the error is reported) is seen as the closing quote, and this mix up is causing the error.
This error also occurs if you use four-space instead of two-space indentation.
e.g., the following would throw the error:
fields:
- metadata: {}
name: colName
nullable: true
whereas changing indentation to two-spaces would fix it:
fields:
- metadata: {}
name: colName
nullable: true
I would like to make this answer for meaningful, so the same kind of
erroneous user can enjoy without feel any hassle.
Actually, i was getting the same error but for the different reason, in my case I didn't used any kind of quoted, still getting the same error like expected <block end>, but found BlockMappingStart.
I have solved it by fixing, the Alignment issue inside the same .yml file.
If we don't manage the proper 'tab-space(Keyboard key)' for
maintaining successor or ancestor then we have to phase such kind of
things.
Now i am doing well.
With YAML, remember that it is all about the spaces used to define configuration through the hierarchical structures (indents). Many problems encountered whilst parsing YAML documents simply stems from extra spaces (or not enough spaces) before a key value somewhere in the given YAML file.
YAML follows indentation structure very strictly. Even one space/tab can cause above issue. In my case it was just once space at the start.
So make sure no extra spaces/tabs are introduced while updating YAML file
I got same issue and found that there is space in next line which combine with content of yml. For solution i just remove that space. Thanks
In my case, the error occured when I tried to pass a variable which was looking like a bytes-object (b"xxxx") but was actually a string.
You can convert the string to a real bytes object like this:
foo.strip('b"').replace("\\n", "\n").encode()