Skip to content

Instantly share code, notes, and snippets.

@gnosek
Last active May 4, 2016 15:48
Show Gist options
  • Save gnosek/c6ee6de97757ee5d028f91ce54fefece to your computer and use it in GitHub Desktop.
Save gnosek/c6ee6de97757ee5d028f91ce54fefece to your computer and use it in GitHub Desktop.
--- a/template/template.py 2016-05-04 15:18:58.000000000 +0000
+++ b/template/template.py 2016-05-04 15:25:04.918007068 +0000
@@ -33,5 +33,7 @@
'''
def new_context(self, vars=None, shared=False, locals=None):
- return jinja2.runtime.Context(self.environment, vars.add_locals(locals), self.name, self.blocks)
+ if vars is not None:
+ vars = vars.add_locals(locals)
+ return jinja2.runtime.Context(self.environment, vars, self.name, self.blocks)
{% macro hello() %}
hello, world!
{% endmacro %}
root@677539bf52ab:~# ansible-playbook t.yml -DCvv
Using /etc/ansible/ansible.cfg as config file
[WARNING]: provided hosts list is empty, only localhost is available
PLAYBOOK: t.yml ****************************************************************
1 plays in t.yml
PLAY [localhost] ***************************************************************
TASK [template] ****************************************************************
task path: /root/t.yml:4
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "AttributeError: 'NoneType' object has no attribute 'add_locals'"}
NO MORE HOSTS LEFT *************************************************************
to retry, use: --limit @t.retry
PLAY RECAP *********************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1
root@677539bf52ab:~# ansible-playbook t.yml -DCvv
Using /etc/ansible/ansible.cfg as config file
[WARNING]: provided hosts list is empty, only localhost is available
PLAYBOOK: t.yml ****************************************************************
1 plays in t.yml
PLAY [localhost] ***************************************************************
TASK [template] ****************************************************************
task path: /root/t.yml:4
changed: [localhost] => {"changed": true, "diff": {"after": "hello, world!\n", "after_header": "dynamically generated", "before": ""}}
--- before
+++ after: dynamically generated
@@ -0,0 +1 @@
+hello, world!
PLAY RECAP *********************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0
{% import 'macros.j2' as macros %}
{{ macros.hello() }}
- hosts: localhost
gather_facts: false
tasks:
- template:
src: t.j2
dest: /tmp/foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment