Skip to main content

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=value 和 key="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"

Metadata

Release files for env2yaml 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for env2yaml 0.2.0
File Size Uploaded
env2yaml-0.2.0.tar.gz 3.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for env2yaml 0.2.0
File Interpreter ABI Platform
env2yaml-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 5.9 kB

Release files / env2yaml-0.2.0.tar.gz

Download URL env2yaml-0.2.0.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
08d14a4435ad7b5276a163d38d93f7f87ef798d8753043442f85ef8bbeab28e8
BLAKE2b-256 checksum
How to use checksums
866f64e5f8951bf2927952d85e514936b88d62257ebbe2ebf8260a21357b8937
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.6

Release files / env2yaml-0.2.0-py3-none-any.whl

Download URL env2yaml-0.2.0-py3-none-any.whl
Size 2.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5db279ad9cf52169d347e71090d426e99148a9294e01b2df7b3c2e0d3145429f
BLAKE2b-256 checksum
How to use checksums
620513a303a90bc0acc6939a90422320ce023f2e756efb2df9ac4977a520be44
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.6

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page