Issue 1:
Issue Type:

Bug Report

Ansible Version:

Ansible version: 1.9.2

Jinja2 version: 2.8

Obtained from pip install on OS described hereafter

Ansible Configuration:

No change.

Environment:

Ubuntu 14.04.03 LTS, no virtualenv, pip installed

Summary:

Jinja 2.1 added the possibility to use the variable yielded by a for loop into an included template.

From Jinja website (http://jinja.pocoo.org/docs/dev/templates/#include):

Note

In Jinja 2.0, the context that was passed to the included template did not include variables defined

in the template. As a matter of fact, this did not work:

{% for box in boxes %}
{% include "render_box.html" %}
{% endfor %}

The included template render_box.html is not able to access box in Jinja 2.0. As of Jinja 2.1,

render_box.html is able to do so.

Although I use Jinja 2.8 (along with Ansible 1.9), this feature is unavailable/broken by the Ansible template module. This report is not a "feature support" request but a "stop breaking a feature" request :)

Steps To Reproduce:

To reproduce the bug, use the following playbook :

---
- hosts: desktop
gather_facts: no
vars:
nom: "Toto!"
some_list:
- "elmt 1"
- "elmt 2"
tasks:
- name: "test"
template:
src: /tmp/tmpl
dest: /tmp/output

/tmp/tmpl contains:

hello {{ nom }}
{% for elmt in some_list %}
{% include 'do_smth_with_elmt' with context %}
{% endfor %}

/tmp/do_smth_with_elmt:

Print {{ elmt }}

Expected Results:

No error message from Ansible and rendering of the template

Actual Results:

Output from Ansible is :

PLAY [desktop] ****************************************************************

TASK: [test] ******************************************************************
fatal: [desktop.local] => {'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'elmt' is undefined", 'failed': True}
fatal: [desktop.local] => {'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'elmt' is undefined", 'failed': True}

FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
to retry, use: --limit @/home/user/test.retry

desktop.local : ok=0 changed=0 unreachable=1 failed=0

Comment:

If I had to do some fingerpointing, I would blame the new_context system of the J2Template class:

https://github.com/ansible/ansible/blob/stable-1.9/lib/ansible/utils/template.py#L212-L213

Since I am totally unfamiliar with Ansible source code, I might be wrong, though.

At any rate, the following python script runs flawlessly on my machine:

import jinja2
loader = jinja2.FileSystemLoader('/tmp')
e = jinja2.Environment(loader=loader)
t = e.get_template('tmpl')
print t.render(nom='Toto!', some_list=['elmt1', 'elmt2'])

Thank you.

Issue 2:
Currently async operations in playbooks and /usr/bin/ansible are duplicating a lot of code. Using async through the API is also very ugly.

I've been thinking about this and would propose a method runAsync() on Runner that returns an object with a poll() method and does all the bookkeeping involved. Timing would be external?

Async is also not fully integrated in the callback system, this can also be fixed, along with cleaning up the files in ~/.ansible_async.

Any feedback? I could probably do this by the weekend.