问题描述
我正在尝试通过添加到实例元数据的脚本设置环境变量.我使用以下命令从文件中添加了元数据:
I am trying to set an environment variable from a script added to an instance metadata. I added the metadata from file using the command:
gcloud计算实例add-metadata server-1 --metadata-from-file file =〜/meta.sh
gcloud compute instances add-metadata server-1 --metadata-from-file file=~/meta.sh
并且脚本是
#!/bin/sh
export SERVER="ide"
重启服务器后似乎什么也没做
it seems is doing nothing when I reboot the server
推荐答案
6岁的问题,但对我自己和其他人有借鉴意义:
6 years old question, but for future reference for myself and others:
在启动脚本中设置环境变量似乎无效,但是您可以做的是将它们写入 .bashrc
-在我的示例中,我将其设置为:/p>
Setting environment-variables in the startup-script doesn't seem to work, but what you can do is write them to your .bashrc
- in my example, I set them like this:
gcloud compute instances add-metadata etl-pipelines --metadata startup-script='#! /bin/bash
echo "
export USER='${USER}'
export PASSWORD='${PASSWORD}'
" >> /home/USERNAME/.bashrc
更好的方法当然是检查该字符串是否已插入到VM中,但这与我无关,因为无论如何我都很快杀死了VM.
better would of course be to check if that string is already inserted into the VM, but that wasn't relevant for me as I kill the VMs quite quickly anyway.
或者,在此SO答案中,描述了如何使用 curl
获取env-vars直接来自元数据,但是我还没有进一步研究.
Alternatively, in this SO answer, it is described how to user curl
to get the env-vars directly from the metadata, but I haven't looked further into it yet.
这篇关于gcloud计算实例添加元数据设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!