Accessing OS environment variables from Jinja2 template

前端 未结 3 685
予麋鹿
予麋鹿 2021-02-02 13:37

Is it possible to access a OS environment variable directly from a Jinja2 template?

3条回答
  •  长情又很酷
    2021-02-02 14:26

    The answer in https://stackoverflow.com/a/27984610/1070890 works beautifully but you can still get rid of the useless use of cat and compress it to a single statement:

    python -c 'import os
    import sys
    import jinja2
    sys.stdout.write(
        jinja2.Template(sys.stdin.read()
    ).render(env=os.environ))' <$CONFIGTEMPLATE >$CONFIGFILE
    

    P.S.: Stack Overflow does not allow formatted code in comments. Therefore I had to post this as a separate answer instead of commenting on https://stackoverflow.com/a/27984610/1070890.

提交回复
热议问题