【问题标题】:Using Dynamic Variables/Placeholders in Yaml File在 Yaml 文件中使用动态变量/占位符
【发布时间】:2020-06-05 05:47:00
【问题描述】:

我有一个像这样的 Google AppEngine Yaml 文件:

entrypoint: mlflow server \
    --host 0.0.0.0 \
    --workers 4 \
    --backend-store-uri postgresql+psycopg2://$DB_USER:$DB_PASS@/$DB_NAME?host=/cloudsql/<project_id>:us-east1:<cloud-sql-instance> \
    --default-artifact-root gs://$GCP_STORAGE_BUCKET \
    --port $PORT
service: mlflow-tracking-server
runtime: python37
beta_settings:
    cloud_sql_instances: <project_id>:us-east1:<cloud-sql-instance>
resources:
    cpu: 4
    memory_gb: 12
    disk_size_gb: 60

如何用它们各自的值替换这些占位符变量?

我知道 Kubernetes,我有一个 bash 脚本可以做到这一点。

例子:

#!/usr/bin/env bash

sed "s/GCLOUD_SERVICE_KEY_ENC_REPLACE/$(printf "%s" "$GCLOUD_SERVICE_KEY_ENC"|base64)/g" mlflow_gcp_secret.yaml | \
sed "s/GCP_STORAGE_BUCKET_REPLACE/$(printf "%s" "$GCP_STORAGE_BUCKET"|base64)/g" | \
sed "s/CLOUDSQL_USER_REPLACE/$(printf "%s" "$DB_USER"|base64)/g" | \
sed "s/CLOUDSQL_PASS_REPLACE/$(printf "%s" "$DB_PASS"|base64)/g" | \
sed "s/CLOUDSQL_DB_REPLACE/$(printf "%s" "$DB_NAME"|base64)/g" | \
kubectl apply -f -

现在我想为 App Engine yaml 文件做点什么。

【问题讨论】:

    标签: python bash google-app-engine google-cloud-platform yaml


    【解决方案1】:

    有一个专用的工具:

    envsubst '$DB_USER:$DB_PASS:$GCP_STORAGE_BUCKET' <cfg.yml
    

    会将列出的所有变量替换为环境中的变量

    演示:https://ideone.com/6mEQk9

    注意:envsubst 是 gnu gettext 的一部分,它很常见,但默认情况下可能不可用。

    【讨论】:

      【解决方案2】:

      我认为您的sed 示例应该足以让您在此任务上取得进展。但是,这是第二种方法。由于所有占位符看起来都像环境变量,因此您可以使用 bash 插值来为您进行替换。

      考虑这段代码:

      DBUSER=user
      DBPASS=pass
      GCP_STORAGE_BUCKET=bucket
      
      cat  > 4appengine <<EOF
      entrypoint: mlflow server \
          --host 0.0.0.0 \
          --workers 4 \
          --backend-store-uri postgresql+psycopg2://$DB_USER:$DB_PASS@/$DB_NAME?host=/cloudsql/<project_id>:us-east1:<cloud-sql-instance> \
          --default-artifact-root gs://$GCP_STORAGE_BUCKET \
          --port $PORT
      service: mlflow-tracking-server
      runtime: python37
      beta_settings:
          cloud_sql_instances: <project_id>:us-east1:<cloud-sql-instance>
      resources:
          cpu: 4
          memory_gb: 12
          disk_size_gb: 60
      EOF
      

      上面,替换了变量的脚本将被写入文件:4appengine。

      【讨论】:

      • 复制粘贴时,请确保EOF后面没有多余的空格。
      猜你喜欢
      • 2017-05-28
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-27
      相关资源
      最近更新 更多