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}
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.
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.
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.
' End first quotation which uses single quotes. " Start second quotation, using double-quotes. ' Quoted character. " End second quotation, using double-quotes.
>
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 >
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With