如何在同一字符串中的YAML中转义双引号和单引号 [英] How to escape double and single quotes in YAML within the same string

查看:2137
本文介绍了如何在同一字符串中的YAML中转义双引号和单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在ansible剧本中正确转义单引号和双引号,以设置环境变量.这些都不起作用:

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\"}}''

看过这个:

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

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

我收到的错误消息是:

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}

推荐答案

>启动一个块标量,其中您根本不需要转义任何内容(并且不处理任何转义序列).因此,假设您想在类似JSON的值周围加上单引号,只需执行以下操作:

> 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"}}'

另外请注意,默认情况下,折叠标量包含尾随换行符.如果您不想使用此功能,只需使用>-而不是>.

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 >.

这篇关于如何在同一字符串中的YAML中转义双引号和单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆