如何为gcloud计算实例创建的启动脚本中注入环境变量? [英] How can I inject an environment variable into the startup script for gcloud compute instances create?

查看:58
本文介绍了如何为gcloud计算实例创建的启动脚本中注入环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 gcloud计算实例创建命令创建GCE VM,并且正在使用-metadata-from-files 参数传入启动Shell脚本进行一些配置.

但是,我需要使用指定的值预先填充各种环境变量,以使启动脚本能够正常工作.不能将它们硬编码到启动脚本中,因为它们对于我正在编写的整个脚本的不同用户而言是不同的(例如,传递到新实例中的env var可能基于$ USER调用脚本)./p>

我也尝试添加带有-metadata 参数的键值对,但这是行不通的,并且在启动脚本的执行过程中仍未绑定引用的shell变量.

我如何发出gcloud计算实例创建命令,该命令转发一些将填充到环境中的环境变量集,以供启动脚本使用它们?

-元数据参数似乎应该正确,因为解决方案

您应该能够使用此文档来生成适当的 curl 命令来提取元数据: https://cloud.google.com/compute/docs/storing-retrieving-元数据

然后您可以使用提取的值来设置环境变量.

(已编辑以修复URL.)

I am creating a GCE VM with the gcloud compute instances create command and I am using the --metadata-from-files argument to pass in a startup shell script for some provisioning.

However, I need for a variety of environment variables to be pre-populated with specified values in order for the startup script to work. They can't be hard-coded into the startup script because they will be different for different users of the overall script I am writing (eg the env vars passed into the new instance might be based on the $USER invoking the script).

I tried adding key value pairs with the --metadata argument as well, but this does not work, and the referenced shell variables are still unbound in the execution of the startup script.

How can I issue a gcloud compute instances create command that forwards some set of environment variables that will be populated into the environment for the startup script to consume them?

The --metadata parameter seems like it should be correct since the docs say,

Metadata to be made available to the guest operating system running on the instances.

解决方案

You should be able to use this documentation to generate the appropriate curl commands to extract your metadata: https://cloud.google.com/compute/docs/storing-retrieving-metadata

You can then use the extracted values to set your environment variables.

(Edited to fix the URL.)

这篇关于如何为gcloud计算实例创建的启动脚本中注入环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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