diff --git a/README.md b/README.md index f14de31..e09bf98 100644 --- a/README.md +++ b/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: diff --git a/dotdrop/templategen.py b/dotdrop/templategen.py index a362f00..d84b9c1 100644 --- a/dotdrop/templategen.py +++ b/dotdrop/templategen.py @@ -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