Skip to content

Instantly share code, notes, and snippets.

@logan2211
Created July 31, 2017 15:39
Show Gist options
  • Save logan2211/32a98a8bad71ed393badd621668e6a58 to your computer and use it in GitHub Desktop.
Save logan2211/32a98a8bad71ed393badd621668e6a58 to your computer and use it in GitHub Desktop.
Broken jinja2 include inside loop
ubuntu@2378491cf3ce:/template-bug$ ansible-playbook test.yml -e test_template=test-context.j2
[WARNING]: Host file not found: /etc/ansible/hosts
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [localhost] ******************************************************************************************************************************************************************
TASK [Test template] **************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "KeyError: 'undefined variable: 0'"}
to retry, use: --limit @/template-bug/test.retry
PLAY RECAP ************************************************************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1
ubuntu@2378491cf3ce:/template-bug$ ansible-playbook test.yml -e test_template=test-nocontext.j2
[WARNING]: Host file not found: /etc/ansible/hosts
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [localhost] ******************************************************************************************************************************************************************
TASK [Test template] **************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "AttributeError: 'NoneType' object has no attribute 'add_locals'"}
to retry, use: --limit @/template-bug/test.retry
PLAY RECAP ************************************************************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1
{% set b = file_base %}
{% for p in file_part %}
{% include file_base ~ p ~ '.j2' %}
{% endfor %}
{% set b = file_base %}
{% for p in file_part %}
{% include file_base ~ p ~ '.j2' without context %}
{% endfor %}
# Test playbook
# .
# ├── templates
# │   ├── base_specific1.j2
# │   ├── test-context.j2
# │   └── test-nocontext.j2
# └── test.yml
- hosts: localhost
gather_facts: no
vars:
test_template: test-context.j2
file_base: 'base_'
file_part:
- specific1
tasks:
- name: Test template
template:
src: "{{ test_template }}"
dest: "{{ playbook_dir }}/output-rendered.txt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment