From 68f5d69365d646a46f6f5cc0317badb965529d14 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 14 Nov 2012 02:20:39 -0500 Subject: [PATCH 1/4] added ability to override jinja enviornment from first line of template Signed-off-by: Brian Coca Signed-off-by: Brian Coca --- lib/ansible/utils/template.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/ansible/utils/template.py b/lib/ansible/utils/template.py index 6e8893e2c84..42f5612a5b9 100644 --- a/lib/ansible/utils/template.py +++ b/lib/ansible/utils/template.py @@ -275,6 +275,16 @@ def template_from_file(basedir, path, vars): raise errors.AnsibleError("unable to process as utf-8: %s" % realpath) except: raise errors.AnsibleError("unable to read %s" % realpath) + + # Get jinja env overrides from template + if data.startswith('#env:'): + eol = data.find('\n') + line = data[5:eol] + data = data[eol+1:] + for pair in line.split(','): + (key,val) = pair.split(':') + setattr(environment,key.strip(),val.strip()) + t = environment.from_string(data) vars = vars.copy() try: From d751d88b48f6f6886175bab1d1bae1653da3c900 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 19 Nov 2012 09:44:54 -0500 Subject: [PATCH 2/4] added test for template overrides Signed-off-by: Brian Coca Signed-off-by: Brian Coca --- test/jinja2_overrides.tpl | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 test/jinja2_overrides.tpl diff --git a/test/jinja2_overrides.tpl b/test/jinja2_overrides.tpl new file mode 100644 index 00000000000..4d7a55c04a0 --- /dev/null +++ b/test/jinja2_overrides.tpl @@ -0,0 +1,10 @@ +#env: variable_end_string: @@, variable_start_string: @@ + +{% raw %} + if this succeeds you should see '{{ ansible_hostname }}' with the hostname on the line above + if this fails you should see '@@ ansible_hostname @@' with the hostname on the line beneath +{% endraw %} + +@@ ansible_hostname @@ +{{ ansible_hostname }} + From 6a1e2aaff5da6a12c4abc501ba146cc12c116b28 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 19 Nov 2012 09:48:16 -0500 Subject: [PATCH 3/4] moved override matching string to variable changed test template to match Signed-off-by: Brian Coca Signed-off-by: Brian Coca --- lib/ansible/utils/template.py | 4 ++-- test/jinja2_overrides.tpl | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ansible/utils/template.py b/lib/ansible/utils/template.py index 42f5612a5b9..311be63f5dc 100644 --- a/lib/ansible/utils/template.py +++ b/lib/ansible/utils/template.py @@ -31,7 +31,7 @@ import pwd # TODO: refactor this file _LISTRE = re.compile(r"(\w+)\[(\d+)\]") - +JINJA2_OVERRIDE='#jinja2:' def _varFindLimitSpace(basedir, vars, space, part, depth): ''' limits the search space of space to part @@ -277,7 +277,7 @@ def template_from_file(basedir, path, vars): raise errors.AnsibleError("unable to read %s" % realpath) # Get jinja env overrides from template - if data.startswith('#env:'): + if data.startswith(JINJA2_OVERRIDE): eol = data.find('\n') line = data[5:eol] data = data[eol+1:] diff --git a/test/jinja2_overrides.tpl b/test/jinja2_overrides.tpl index 4d7a55c04a0..b0e18238faa 100644 --- a/test/jinja2_overrides.tpl +++ b/test/jinja2_overrides.tpl @@ -1,4 +1,4 @@ -#env: variable_end_string: @@, variable_start_string: @@ +#jinja2: variable_end_string: @@, variable_start_string: @@ {% raw %} if this succeeds you should see '{{ ansible_hostname }}' with the hostname on the line above From d7f38d07b3f3deed374214c1748f2a7caca8ac32 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 19 Nov 2012 09:54:19 -0500 Subject: [PATCH 4/4] fixed bug for string size mismatch, now substring depends on size of match string Signed-off-by: Brian Coca Signed-off-by: Brian Coca --- lib/ansible/utils/template.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ansible/utils/template.py b/lib/ansible/utils/template.py index 311be63f5dc..53ff0275fb9 100644 --- a/lib/ansible/utils/template.py +++ b/lib/ansible/utils/template.py @@ -279,7 +279,7 @@ def template_from_file(basedir, path, vars): # Get jinja env overrides from template if data.startswith(JINJA2_OVERRIDE): eol = data.find('\n') - line = data[5:eol] + line = data[len(JINJA2_OVERRIDE):eol] data = data[eol+1:] for pair in line.split(','): (key,val) = pair.split(':')