File tree Expand file tree Collapse file tree 4 files changed +26
-1
lines changed Expand file tree Collapse file tree 4 files changed +26
-1
lines changed Original file line number Diff line number Diff line change 1
1
#!/usr/bin/env python
2
2
3
+ import os
3
4
import sys
4
5
import six
5
6
import argparse
@@ -143,6 +144,7 @@ def print_output(output):
143
144
args = parser .parse_args ()
144
145
config = _load (args .config )
145
146
templates = [_load (template ) for template in args .templates ]
147
+ context = dict (os .environ )
146
148
method = args .method
147
149
method_arguments = parse_method_arguments (args .args )
148
150
@@ -153,7 +155,7 @@ backends = {
153
155
154
156
backend_class = backends [args .backend ]
155
157
try :
156
- instance = backend_class (config , templates = templates )
158
+ instance = backend_class (config , templates = templates , context = context )
157
159
except TypeError as e :
158
160
print ('netjsonconfig: invalid JSON passed in config or templates' )
159
161
sys .exit (2 )
Original file line number Diff line number Diff line change @@ -271,6 +271,8 @@ it's possible to pass multiple templates that will be added one on top of the
271
271
other to build the resulting *configuration dictionary *, allowing to reduce or
272
272
even eliminate repetitions.
273
273
274
+ .. _configuration_variables :
275
+
274
276
Context: configuration variables
275
277
--------------------------------
276
278
Original file line number Diff line number Diff line change @@ -67,3 +67,17 @@ Here's the common use cases explained::
67
67
Using templates::
68
68
69
69
netjsonconfig -c config.json -t template1.json template2.json -b openwrt -m render
70
+
71
+ Environment variables
72
+ ---------------------
73
+
74
+ *Environment variables * are automatically passed to the ``context `` argument (if you don't
75
+ know what this argument does please read ":ref: `configuration_variables `"), therefore
76
+ you can reference environment variables inside *configurations * and *templates *::
77
+
78
+ export HOSTNAME=freedom
79
+ netjsonconfig -c '{"general": { "hostname": "${HOSTNAME}" }}' -b openwrt -m render
80
+
81
+ You can also avoid using ``export `` and write everything in a one line command::
82
+
83
+ PORT=2009; netjsonconfig -c config.json -t template1.json -b openwrt -m render
Original file line number Diff line number Diff line change @@ -121,3 +121,10 @@ def test_generate_redirection(self):
121
121
tar = tarfile .open ('test.tar.gz' , 'r' )
122
122
self .assertEqual (len (tar .getmembers ()), 1 )
123
123
tar .close ()
124
+
125
+ def test_context (self ):
126
+ config = json .dumps ({'general' : {'description' : '${DESC}' }})
127
+ command = "export DESC=testdesc; netjsonconfig --config '{0}' -b openwrt -m render" .format (config )
128
+ output = subprocess .check_output (command , shell = True ).decode ()
129
+ self .assertNotIn ('${DESC}' , output )
130
+ self .assertIn ('testdesc' , output )
You can’t perform that action at this time.
0 commit comments