处理 EC2 userdata 中的 JSON 字符串

huangapple go评论60阅读模式
英文:

Handling JSON string in EC2 userdata

问题

以下是您要翻译的部分:

"I am trying to provision an EC2 instance using terraform, passing a cloudinit configuration file as userdata to the instance.

As part of the cloudinit config I am setting environment variables and some of the variables are JSON strings.

The user data that is generated by terraform and passed to the instance looks correct, surrounded by single quotes, but when the environment variables are written to the file on the server the quotes are stripped which is causing issues

Here is an example snippet of the user data passed to the EC2 instance


environment:
  var: '{"key": "value"}'```

Then on the server the environment file (`/etc/environment`) looks like this
```var={"key": "value"}```

But I need to to still be surrounded by single quotes when in `/etc/environment`
```var='{"key": "value"}'```

Any ideas on how I do this? I tried escaping the quotes using `\'` but that is not valid YAML and so the user data fails with an error, any thoughts on this would be appreciated."

<details>
<summary>英文:</summary>

I am trying to provision an EC2 instance using terraform, passing a cloudinit configuration file as userdata to the instance.

As part of the cloudinit config I am setting environment variables and some of the variables are JSON strings.

The user data that is generated by terraform and passed to the instance looks correct, surrounded by single quotes, but when the environment variables are written to the file on the server the quotes are stripped which is causing issues

Here is an example snippet of the user data passed to the EC2 instance

#cloud-config

environment:
var: '{"key": "value"}'


Then on the server the environment file (`/etc/environment`) looks like this

var={"key": "value"}


But I need to to still be surrounded by single quotes when in `/etc/environment`

var='{"key": "value"}'


Any ideas on how I do this? I tried escaping the quotes using `\&#39;` but that is not valid YAML and so the user data fails with an error, any thoughts on this would be appreciated.

</details>


# 答案1
**得分**: 3

看起来你可能想要一个YAML值的块。你可以使用`&gt;`来实现这一点。

```yaml
#cloud-config

environment: 
  var: &gt;
    &#39;{&quot;key&quot;: &quot;value&quot;}&#39;
英文:

It seems you may want a block for your YAML value. You can do this using &gt;.

#cloud-config

environment: 
  var: &gt;
    &#39;{&quot;key&quot;: &quot;value&quot;}&#39;

huangapple
  • 本文由 发表于 2023年4月7日 05:07:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953797.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定