From 675aab47125254e83de4d2557d5396165f44cc90 Mon Sep 17 00:00:00 2001 From: Fabio Date: Tue, 17 Oct 2017 20:50:35 +0200 Subject: [PATCH 1/4] Make environment variables available in templates --- dotdrop/templategen.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 From cfa506b5cbd0c5dd98250038a8ee1eed63db04d9 Mon Sep 17 00:00:00 2001 From: Fabio Date: Tue, 17 Oct 2017 21:31:20 +0200 Subject: [PATCH 2/4] extend documentation in regard to environment variables --- README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/README.md b/README.md index f14de31..c455b40 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 being used. Below in the example you can see how this is useful. + +### Environment variables + +It's possible to access environment variables inside the templates. You can utilize this functionality like this: + +``` +{{@@ env['MY_VAR'] @@}} +``` + +This allows for storing host-specific properties and/or secrets as environment variables. + +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 $(cat ~/dotfiles/.env | egrep -v "^#") ~/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: From d609cd87277e3b99126c90957c07135e3b897d77 Mon Sep 17 00:00:00 2001 From: open-dynaMIX Date: Wed, 18 Oct 2017 09:21:13 +0200 Subject: [PATCH 3/4] Changes based on feedback to PR --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c455b40..caf78bc 100644 --- a/README.md +++ b/README.md @@ -389,17 +389,17 @@ Note that dotdrop uses different delimiters than ## Available variables ### Profile -`{{@@ profile @@}}` contains the profile being used. Below in the example you can see how this is useful. +`{{@@ 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. You can utilize this functionality like this: +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 as environment variables. +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: From 8356f197a70ccbebfbe3b90efb74aeaef7649ff3 Mon Sep 17 00:00:00 2001 From: open-dynaMIX Date: Wed, 18 Oct 2017 09:22:03 +0200 Subject: [PATCH 4/4] Remove unnecessary use of cat --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index caf78bc..e09bf98 100644 --- a/README.md +++ b/README.md @@ -416,7 +416,7 @@ 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 $(cat ~/dotfiles/.env | egrep -v "^#") ~/dotfiles/dotdrop.sh' +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`.