mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-04 20:19:46 +00:00
Merge pull request #14 from open-dynaMIX/env_vars_in_templates
Access environment variables inside templates
This commit is contained in:
34
README.md
34
README.md
@@ -386,6 +386,40 @@ Note that dotdrop uses different delimiters than
|
||||
* comment start = `{#@@`
|
||||
* comment end = `@@#}`
|
||||
|
||||
## Available variables
|
||||
### Profile
|
||||
|
||||
`{{@@ profile @@}}` contains the profile provided to dotdrop. Below example shows how it is used.
|
||||
|
||||
### Environment variables
|
||||
|
||||
It's possible to access environment variables inside the templates. This feature can be used like this:
|
||||
|
||||
```
|
||||
{{@@ env['MY_VAR'] @@}}
|
||||
```
|
||||
|
||||
This allows for storing host-specific properties and/or secrets in environment.
|
||||
|
||||
You can have an `.env` file in the directory where your `config.yaml` lies:
|
||||
|
||||
```
|
||||
# My variables for this host
|
||||
var1="some value"
|
||||
var2="some other value"
|
||||
|
||||
# Some secrets
|
||||
pass="verysecurepassword"
|
||||
```
|
||||
Of course, this file should not be tracked by git (put it in your `.gitignore`).
|
||||
|
||||
|
||||
Then you can invoke dotdrop with the help of an alias like that:
|
||||
```
|
||||
alias dotdrop='eval $(grep -v "^#" ~/dotfiles/.env) ~/dotfiles/dotdrop.sh'
|
||||
```
|
||||
This loads all the variables from `.env` (while omitting lines starting with `#`) before calling `~/dotfiles/dotdrop.sh`.
|
||||
|
||||
# Example
|
||||
|
||||
Let's consider two hosts:
|
||||
|
||||
@@ -6,7 +6,7 @@ jinja2 template generator
|
||||
|
||||
import os
|
||||
import utils
|
||||
from jinja2 import Environment, Template, FileSystemLoader
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
|
||||
BLOCK_START = '{%@@'
|
||||
BLOCK_END = '@@%}'
|
||||
@@ -48,12 +48,12 @@ class Templategen:
|
||||
length = len(self.base) + 1
|
||||
try:
|
||||
template = self.env.get_template(src[length:])
|
||||
content = template.render(profile=profile)
|
||||
content = template.render(profile=profile, env=os.environ)
|
||||
except UnicodeDecodeError:
|
||||
data = self._read_bad_encoded_text(src)
|
||||
template = self.env.from_string(data)
|
||||
content = template.render(profile=profile, env=os.environ)
|
||||
|
||||
content = template.render(profile=profile)
|
||||
content = content.encode('UTF-8')
|
||||
return content
|
||||
|
||||
|
||||
Reference in New Issue
Block a user