Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape double and single quotes in YAML within the same string

I need to properly escape single and double quotes in an ansible playbook in order to set the environment variable. None of this works:

  - name: Set environment variable     command: >       export EXTRA_CONFIG=“'”{"client": {"subscriptions": ["DIND-Worker"], "cluster": "internal"}}“'”    - name: Set environment variable     command: >       export EXTRA_CONFIG=''{"client": {"subscriptions": ["DIND-Worker"], "cluster": "internal"}}''    - name: Set environment variable     command: >       export EXTRA_CONFIG=''{\"client\": {\"subscriptions\": [\"DIND-Worker\"], \"cluster\": \"internal\"}}'' 

Looked at this:

http://yaml.org/spec/current.html#id2532720

https://github.com/dotmaster/toYaml/issues/1

The error message I get is:

fatal: [ip.address]: FAILED! => {"changed": false, "cmd": "export 'EXTRA_CONFIG={\"client\":' '{\"subscriptions\":' '[\"DIND-Worker\"],' '\"cluster\":' '\"internal\"}}'", "failed": true, "msg": "[Errno 2] No such file or directory", "rc": 2} 
like image 926
user3081519 Avatar asked May 25 '16 03:05

user3081519


People also ask

Does YAML Use single or double quotes?

When quotes are used in YAML files, it is better to use double than single. Because double escape syntax matches industry conventions (backslash), while single surprisingly does not. So single quoting can lead to downstream problems when the author is not being exceptionally careful.

How do you escape the special characters in YAML?

When double quotes, "...." , go around a scalar string you use backslash ( \ ) for escaping, and you have to escape at least the backslash and the double quotes. In addition you can escape other special characters like linefeed ( \n ) and escape an end-of-line by preceding it by a backslash.

Does YAML need quotes around strings?

Strings in yaml only need quotation if (the beginning of) the value can be misinterpreted as a data type or the value contains a ":" (because it could get misinterpreted as key). needs quotes, because the value can be misinterpreted as key. and is a must, if working productively with yaml.

How do you escape a single quote inside a single quote?

' End first quotation which uses single quotes. " Start second quotation, using double-quotes. ' Quoted character. " End second quotation, using double-quotes.


2 Answers

> starts a block scalar, in which you do not need to escape anything at all (and there are no escape sequences processed). So assuming you want single quotes around your JSON-like value, just do:

  - name: Set environment variable     command: >       export EXTRA_CONFIG='{"client": {"subscriptions": ["DIND-Worker"], "cluster": "internal"}}' 

Edit: Also be aware that a folded scalar by default includes a trailing newline character. If you do not want to have this, just use >- instead of >.

like image 121
flyx Avatar answered Sep 20 '22 11:09

flyx


You are using folded style scalars (introduced by >) and according to the YAML 1.2 specification you cannot escape characters:

Folded scalars:

The folded style is denoted by the “>” indicator. It is similar to the literal style; however, folded scalars are subject to line folding.

And the relevant text wrt escaping in literal style scalars.

Inside literal scalars, all (indented) characters are considered to be content, including white space characters. Note that all line break characters are normalized. In addition, empty lines are not folded, though final line breaks and trailing empty lines are chomped.

From your example it is unclear what you really want to do. You should probably drop folding style in favour of double quoted style:

The double-quoted style is specified by surrounding “"” indicators. This is the only style capable of expressing arbitrary strings, by using “\” escape sequences. This comes at the cost of having to escape the “\” and “"” characters.

or single quoted style:

The single-quoted style is specified by surrounding “'” indicators. Therefore, within a single-quoted scalar, such characters need to be repeated. This is the only form of escaping performed in single-quoted scalars. In particular, the “\” and “"” characters may be freely used. This restricts single-quoted scalars to printable characters. In addition, it is only possible to break a long single-quoted line where a space character is surrounded by non-spaces.

So you should first decide what the output should be exactly, then if you need to escape any characters with backslash. If you don't you can just use folded style without any escaping, or single quoted style by escaping the ', or double quoted style by escaping " and any \. If you need \ escaping double quoted style is your only option.

like image 33
Anthon Avatar answered Sep 17 '22 11:09

Anthon