在物联网边缘模块中传递环境变量
pass environment variables in iot edge module
我正在为我的 Linux 手臂设备构建 Microsoft IoT edge python 模块。我必须从我的 deployment.template.json 文件访问 python 模块中的环境变量。该文件显示环境变量如何从环境传递到模块。问题是如何在代码中访问它们。
deployment.template.json 文件:
"$schema-template": "2.0.0",
"modulesContent": {
"$edgeAgent": {
"properties.desired": {
"schemaVersion": "1.0",
"runtime": {
"type": "docker",
"settings": {
"minDockerVersion": "v1.25",
"loggingOptions": "",
"registryCredentials": {
"myRegistryName": {
"username": "$CONTAINER_REGISTRY_USERNAME",
"password": "$CONTAINER_REGISTRY_PASSWORD",
"address": "myRegistryAddress.azurecr.io"
}
}
}
},
"systemModules": {
"edgeAgent": {
"type": "docker",
"settings": {
"image": "mcr.microsoft.com/azureiotedge-agent:1.0",
"createOptions": {}
}
},
"edgeHub": {
"type": "docker",
"status": "running",
"restartPolicy": "always",
"settings": {
"image": "mcr.microsoft.com/azureiotedge-hub:1.0",
"createOptions": {
"HostConfig": {
"PortBindings": {
"5671/tcp": [
{
"HostPort": "5671"
}
],
"8883/tcp": [
{
"HostPort": "8883"
}
],
"443/tcp": [
{
"HostPort": "443"
}
]
}
}
}
}
}
},
"modules": {
"Module_Name": {
"version": "1.0",
"type": "docker",
"status": "running",
"restartPolicy": "always",
"settings": {
"image": "${MODULES.Module_Name}",
"createOptions": {}
},
"env": {
"test_var": {
"value": "secret"
}
}
}
}
}
},
"$edgeHub": {
"properties.desired": {
"schemaVersion": "1.0",
"routes": {
"route": "FROM /messages/* INTO $upstream"
},
"storeAndForwardConfiguration": {
"timeToLiveSecs": 7200
}
}
}
}
}
如何访问 python 代码模块中的环境 test_var
变量?
os.environ
应该有。
类似
import os
os.environ['test_var']
首先需要构建物联网边缘解决方案,以便在 VS Code 中使用 Generate IoT Edge Deployment Manifest
访问变量,然后在模拟器中 运行 解决方案。我的代码显示了如何访问变量。
def main(protocol):
try:
print ( "\nPython %s\n" % sys.version )
print ( "Module client" )
hub_manager = HubManager(protocol)
os.environ['test_var'] // prints secret i.e. value of the variable itself
except IoTHubError as iothub_error:
print ( "Unexpected error %s from IoTHub" % iothub_error )
return
except KeyboardInterrupt:
print ( "client stopped" )
if __name__ == '__main__':
main(PROTOCOL)```
对于 .Net C#/.Net 核心
string environmentValue = Environment.GetEnvironmentVariable("environment-name");
我正在为我的 Linux 手臂设备构建 Microsoft IoT edge python 模块。我必须从我的 deployment.template.json 文件访问 python 模块中的环境变量。该文件显示环境变量如何从环境传递到模块。问题是如何在代码中访问它们。
deployment.template.json 文件:
"$schema-template": "2.0.0",
"modulesContent": {
"$edgeAgent": {
"properties.desired": {
"schemaVersion": "1.0",
"runtime": {
"type": "docker",
"settings": {
"minDockerVersion": "v1.25",
"loggingOptions": "",
"registryCredentials": {
"myRegistryName": {
"username": "$CONTAINER_REGISTRY_USERNAME",
"password": "$CONTAINER_REGISTRY_PASSWORD",
"address": "myRegistryAddress.azurecr.io"
}
}
}
},
"systemModules": {
"edgeAgent": {
"type": "docker",
"settings": {
"image": "mcr.microsoft.com/azureiotedge-agent:1.0",
"createOptions": {}
}
},
"edgeHub": {
"type": "docker",
"status": "running",
"restartPolicy": "always",
"settings": {
"image": "mcr.microsoft.com/azureiotedge-hub:1.0",
"createOptions": {
"HostConfig": {
"PortBindings": {
"5671/tcp": [
{
"HostPort": "5671"
}
],
"8883/tcp": [
{
"HostPort": "8883"
}
],
"443/tcp": [
{
"HostPort": "443"
}
]
}
}
}
}
}
},
"modules": {
"Module_Name": {
"version": "1.0",
"type": "docker",
"status": "running",
"restartPolicy": "always",
"settings": {
"image": "${MODULES.Module_Name}",
"createOptions": {}
},
"env": {
"test_var": {
"value": "secret"
}
}
}
}
}
},
"$edgeHub": {
"properties.desired": {
"schemaVersion": "1.0",
"routes": {
"route": "FROM /messages/* INTO $upstream"
},
"storeAndForwardConfiguration": {
"timeToLiveSecs": 7200
}
}
}
}
}
如何访问 python 代码模块中的环境 test_var
变量?
os.environ
应该有。
类似
import os
os.environ['test_var']
首先需要构建物联网边缘解决方案,以便在 VS Code 中使用 Generate IoT Edge Deployment Manifest
访问变量,然后在模拟器中 运行 解决方案。我的代码显示了如何访问变量。
def main(protocol):
try:
print ( "\nPython %s\n" % sys.version )
print ( "Module client" )
hub_manager = HubManager(protocol)
os.environ['test_var'] // prints secret i.e. value of the variable itself
except IoTHubError as iothub_error:
print ( "Unexpected error %s from IoTHub" % iothub_error )
return
except KeyboardInterrupt:
print ( "client stopped" )
if __name__ == '__main__':
main(PROTOCOL)```
对于 .Net C#/.Net 核心
string environmentValue = Environment.GetEnvironmentVariable("environment-name");