python - 在 iot edge 模块中传递环境变量

标签 python azure-iot-hub azure-iot-edge

我正在为我的 Linux arm 设备构建一个 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']

关于python - 在 iot edge 模块中传递环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57521312/

相关文章:

python - 有没有办法在Python中重新加载 "current"文件?

python - 当宽度 <1.0 时,Matplotlib 周线太细;当宽度>=1.0 时,周线太粗

azure - 使用 Azure 设备预配服务的 REST API 删除设备?

azure - 如何从移动应用程序配置 Azure IoT 设备

azure - Azure IoT Edge 模块的安装路径

python - 为什么 QDoubleSpinBox 的精度有时高于其小数属性允许的精度?

python - 如何在 DBSCAN 中使用 knn 距离图估计 eps

Azure 服务总线 : No messages on queue, 仅请求

c# - 如何在不使用 IoT 中心的情况下从 IoT Edge 中心检索消息

Azure 流分析作业 - 如何在边缘远程部署/发送引用数据文件