Skip to main content

将环境变量渲染到YAML文件中的工具

Project description

env2yaml 工具使用说明

env2yaml 是一个将环境变量文件渲染到 YAML 文件中的工具。

  • 支持 jinja2 语法,可以根据环境变量的值动态渲染 YAML 文件。
  • 支持变量扩展,可以根据环境变量的值动态扩展变量的值。

安装

pip install env2yaml

基本用法

env2yaml -h
usage: env2yaml [-h] --input_ini INPUT_INI --input_yaml INPUT_YAML --output_yaml OUTPUT_YAML [--dry-run]

环境变量渲染到YAML文件

optional arguments:
  -h, --help            show this help message and exit
  --input_ini INPUT_INI
                        环境变量文件路径
  --input_yaml INPUT_YAML
                        模板YAML文件路径
  --output_yaml OUTPUT_YAML
                        输出YAML文件路径
  --dry-run             仅输出渲染结果,不写入文件


env2yaml --input_ini=config.env --input_yaml=test.yml --output_yaml=test_output.yaml
env2yaml --input_ini=config.env --input_yaml=test.yml --output_yaml=test_output.yaml --dry-run
  • --input_ini: 环境变量文件路径
  • --input_yaml: YAML模板文件路径
  • --output_yaml: 输出文件路径
  • --dry-run: 仅输出渲染结果,不写入文件(可选)

示例

环境变量文件 (config.env)

DB_HOST=127.0.0.1
MYSQL_DB=test
MYSQL_PORT=${MYSQL_PORT:3307}
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_ENABLED=true
REDIS_CONNECTION=redis://${REDIS_HOST}:${REDIS_PORT}
BASE_DIR=/opt/app
LOG_DIR=${BASE_DIR}/logs
LOG_FILE=${LOG_DIR}/app.log
HOSTS=host1,host2,host3

YAML模板文件 (test.yml)

---
hosts: web
remote_user: root
gather_facts: no
vars:
  DB_HOST: "{{ DB_HOST }}"
  MYSQL_DB: "{{ MYSQL_DB }}"
  MYSQL_PORT: "{{ MYSQL_PORT }}"
  REDIS_HOST: "{{ REDIS_HOST }}"
  BASE_DIR: "{{ BASE_DIR }}"
  LOG_DIR: "{{ LOG_DIR }}"
  LOG_FILE: "{{ LOG_FILE }}"
  {%- if REDIS_ENABLED %}
  REDIS_CONNECTION: "{{ REDIS_CONNECTION }}"
  {%- endif %}
  {%- for host in HOSTS %}
  HOST_{{ loop.index0 }}: "{{ host }}"
  {%- endfor %}

渲染后的输出 (test_output.yaml)

---
hosts: web
remote_user: root
gather_facts: no
vars:
  DB_HOST: "127.0.0.1"
  MYSQL_DB: "test"
  MYSQL_PORT: "3307"
  REDIS_HOST: "127.0.0.1"
  BASE_DIR: "/opt/app"
  LOG_DIR: "/opt/app/logs"
  LOG_FILE: "/opt/app/logs/app.log"
  REDIS_CONNECTION: "redis://127.0.0.1:6379"
  HOST_0: "host1"
  HOST_1: "host2"
  HOST_2: "host3"

开发注意事项

  • 环境变量文件支持 key=valuekey="value" 格式
  • 支持注释行(以 # 开头的行)
  • 空行将被忽略
  • 工具使用Jinja2模板引擎,支持变量引用、条件判断、循环等

高级功能

变量扩展

工具支持在环境变量值中使用变量扩展,格式为 ${VAR_NAME}。例如:

BASE_DIR=/opt/app
LOG_DIR=${BASE_DIR}/logs
LOG_FILE=${LOG_DIR}/app.log

在这个例子中,LOG_DIR 的值会被扩展为 /opt/app/logs

变量默认值

工具支持为变量引用指定默认值,格式为 ${VAR_NAME:DEFAULT_VALUE}。如果 VAR_NAME 不存在,则使用 DEFAULT_VALUE

环境变量文件 (config.env)

DB_HOST=127.0.0.1
MYSQL_DB=test
# 支持默认值
MYSQL_PORT=${MYSQL_PORT:3307}

YAML模板文件 (test.yml)

---
hosts: web
remote_user: root
gather_facts: no
vars:
  DB_HOST: "{{ DB_HOST }}"
  MYSQL_DB: "{{ MYSQL_DB }}"
  MYSQL_PORT: "{{ MYSQL_PORT }}"   #使用默认值

渲染后的输出

---
hosts: web
remote_user: root
gather_facts: no
vars:
  DB_HOST: "127.0.0.1"
  MYSQL_DB: "test"
  MYSQL_PORT: "3307"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

env2yaml-0.2.0.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

env2yaml-0.2.0-py3-none-any.whl (2.8 kB view details)

Uploaded Python 3

File details

Details for the file env2yaml-0.2.0.tar.gz.

File metadata

  • Download URL: env2yaml-0.2.0.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.6

File hashes

Hashes for env2yaml-0.2.0.tar.gz
Algorithm Hash digest
SHA256 08d14a4435ad7b5276a163d38d93f7f87ef798d8753043442f85ef8bbeab28e8
MD5 908853306052fdb483982abee7baf051
BLAKE2b-256 866f64e5f8951bf2927952d85e514936b88d62257ebbe2ebf8260a21357b8937

See more details on using hashes here.

File details

Details for the file env2yaml-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: env2yaml-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 2.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.6

File hashes

Hashes for env2yaml-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5db279ad9cf52169d347e71090d426e99148a9294e01b2df7b3c2e0d3145429f
MD5 b6061e9c99071429367b6813753268e0
BLAKE2b-256 620513a303a90bc0acc6939a90422320ce023f2e756efb2df9ac4977a520be44

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page