首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在物联网边缘模块中传递环境变量

在物联网边缘模块中传递环境变量
EN

Stack Overflow用户
提问于 2019-08-16 08:31:08
回答 3查看 3.7K关注 0票数 3

我正在为我的Linux设备构建一个微软IoT边缘python模块。我必须从我的deployment.template.json文件中访问python模块中的环境变量。该文件显示了环境变量如何从环境传递到模块。问题是如何在代码中访问它们。

deployment.template.json文件:

代码语言:javascript
复制
  "$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变量?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-08-16 08:54:34

os.environ应该拥有它。

有点像

代码语言:javascript
复制
import os
os.environ['test_var']
票数 6
EN

Stack Overflow用户

发布于 2019-08-16 10:46:55

首先需要构建IoT边缘解决方案,以便在VS代码中使用Generate IoT Edge Deployment Manifest访问变量,然后在模拟器中运行解决方案。我的代码显示了如何访问变量。

代码语言:javascript
复制
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)```
票数 1
EN

Stack Overflow用户

发布于 2022-05-21 13:06:08

用于.Net C# / .Net核心

代码语言:javascript
复制
string environmentValue = Environment.GetEnvironmentVariable("environment-name");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57521312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档