英文:
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 `\'` 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值的块。你可以使用`>`来实现这一点。
```yaml
#cloud-config
environment:
var: >
'{"key": "value"}'
英文:
It seems you may want a block for your YAML value. You can do this using >
.
#cloud-config
environment:
var: >
'{"key": "value"}'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论