Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: variables go out of scope inside an included template #6653

Closed
machpo opened this issue Mar 24, 2014 · 5 comments · Fixed by #72830
Closed

Bug: variables go out of scope inside an included template #6653

machpo opened this issue Mar 24, 2014 · 5 comments · Fixed by #72830
Labels
bug This issue/PR relates to a bug.

Comments

@machpo
Copy link

machpo commented Mar 24, 2014

Issue Type:

Bug Report

Ansible Version:

ansible 1.5

Environment:

debian 7

Summary:

Variables go out of scope inside an included template

Steps To Reproduce:

hosts.yml:


---
- hosts: all
  vars:
    mylist:
      - alpha
      - bravo
  tasks:
  - name: template test
    template: src=test.j2 dest=/tmp/testfile

test.j2:

{% for x in mylist %}

  X: {{ x }}

  {% include 'test-inc.j2' with context %}

{% endfor %}

test-inc.j2:

{{ x }}

Expected Results:

I expect x in test-inc.j2 to be properly dereferenced

Actual Results:

fatal: [myhost] => {'msg': "One or more undefined variables: 'x' is undefined", 'failed': True}
fatal: [myhost] => {'msg': "One or more undefined variables: 'x' is undefined", 'failed': True}

@jacobweber
Copy link
Contributor

Also similar to #4391.

@bcoca bcoca closed this as completed Dec 11, 2014
@beli-sk
Copy link

beli-sk commented Mar 7, 2015

I also ran into this bug using Ansible 1.7.2. Seems it never got fixed as mentioned in #4391.
And yes, I think it is a bug and it's not the way Jinja2 works. Jinja2 docs say that from version 2.1 the context passed to included templates should include variables defined in the template. And it indeed does work when rendering the template via Jinja2 API outside Ansible.
Is there any chance of fixing this? Because right now I'm thinking of writing my own template module using Jinja2 API directly as a workaround. The way you handle Jinja2 inside Ansible looks too complicated for me to tamper with.

@bcoca
Copy link
Member

bcoca commented Mar 8, 2015

I stand corrected, I was used to the old jinja issue. It seems we specifically disallow this in ansible's templating, I'm not sure why though. https://github.com/ansible/ansible/blob/devel/lib/ansible/utils/template.py#L201

@bcoca bcoca reopened this Mar 8, 2015
@michaelkrupp
Copy link

Any workaround available?

@jimi-c jimi-c closed this as completed in 18a9eff Jun 18, 2015
@jimi-c
Copy link
Member

jimi-c commented Jun 18, 2015

Closing This Ticket

Hi!

We believe the above commit should resolve this problem for you. This will also be included in the next major release.

If you continue seeing any problems related to this issue, or if you have any further questions, please let us know by stopping by one of the two mailing lists, as appropriate:

Because this project is very active, we're unlikely to see comments made on closed tickets, but the mailing list is a great way to ask questions, or post if you don't think this particular issue is resolved.

Thank you!

donckers added a commit to donckers/ansible that referenced this issue Sep 16, 2015
jimi-c added a commit that referenced this issue Sep 16, 2015
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 6, 2018
@ansible ansible locked and limited conversation to collaborators Apr 24, 2019
mkrizek added a commit to mkrizek/ansible that referenced this issue Dec 3, 2020
Ability to add local variables into AnsibleJ2Vars was added in
18a9eff to fix ansible#6653. Local variables
are added using ``AnsibleJ2Vars.add_locals()`` method when creating a
new context - typically when including/importing a template with
context. For that use case local template variables created using
``set`` should override variables from higher contexts - either from the
play or any parent template, or both; Jinja behaves the same way.

Also removes AnsibleJ2Vars.extras instance variable which is not used.

Also adds missing test for ansible#6653.

Fixes ansible#72262
Fixes ansible#72615

ci_complete
mkrizek added a commit to mkrizek/ansible that referenced this issue Dec 3, 2020
Ability to add local variables into AnsibleJ2Vars was added in
18a9eff to fix ansible#6653. Local variables
are added using ``AnsibleJ2Vars.add_locals()`` method when creating a
new context - typically when including/importing a template with
context. For that use case local template variables created using
``set`` should override variables from higher contexts - either from the
play or any parent template, or both; Jinja behaves the same way.

Also removes AnsibleJ2Vars.extras instance variable which is not used.

Also adds missing test for ansible#6653.

Fixes ansible#72262
Fixes ansible#72615

ci_complete
mkrizek added a commit that referenced this issue Jan 26, 2021
Ability to add local variables into AnsibleJ2Vars was added in
18a9eff to fix #6653. Local variables
are added using ``AnsibleJ2Vars.add_locals()`` method when creating a
new context - typically when including/importing a template with
context. For that use case local template variables created using
``set`` should override variables from higher contexts - either from the
play or any parent template, or both; Jinja behaves the same way.

Also removes AnsibleJ2Vars.extras instance variable which is not used.

Also adds missing test for #6653.

Fixes #72262
Fixes #72615

ci_complete
mkrizek added a commit to mkrizek/ansible that referenced this issue Jan 26, 2021
…2830)

Ability to add local variables into AnsibleJ2Vars was added in
18a9eff to fix ansible#6653. Local variables
are added using ``AnsibleJ2Vars.add_locals()`` method when creating a
new context - typically when including/importing a template with
context. For that use case local template variables created using
``set`` should override variables from higher contexts - either from the
play or any parent template, or both; Jinja behaves the same way.

Also removes AnsibleJ2Vars.extras instance variable which is not used.

Also adds missing test for ansible#6653.

Fixes ansible#72262
Fixes ansible#72615

ci_complete

(cherry picked from commit a2af843)
mkrizek added a commit to mkrizek/ansible that referenced this issue Jan 26, 2021
…2830)

Ability to add local variables into AnsibleJ2Vars was added in
18a9eff to fix ansible#6653. Local variables
are added using ``AnsibleJ2Vars.add_locals()`` method when creating a
new context - typically when including/importing a template with
context. For that use case local template variables created using
``set`` should override variables from higher contexts - either from the
play or any parent template, or both; Jinja behaves the same way.

Also removes AnsibleJ2Vars.extras instance variable which is not used.

Also adds missing test for ansible#6653.

Fixes ansible#72262
Fixes ansible#72615

ci_complete

(cherry picked from commit a2af843)
relrod pushed a commit that referenced this issue Feb 5, 2021
…73369)

Ability to add local variables into AnsibleJ2Vars was added in
18a9eff to fix #6653. Local variables
are added using ``AnsibleJ2Vars.add_locals()`` method when creating a
new context - typically when including/importing a template with
context. For that use case local template variables created using
``set`` should override variables from higher contexts - either from the
play or any parent template, or both; Jinja behaves the same way.

Also removes AnsibleJ2Vars.extras instance variable which is not used.

Also adds missing test for #6653.

Fixes #72262
Fixes #72615

ci_complete

(cherry picked from commit a2af843)
relrod pushed a commit that referenced this issue Feb 7, 2021
…73370)

Ability to add local variables into AnsibleJ2Vars was added in
18a9eff to fix #6653. Local variables
are added using ``AnsibleJ2Vars.add_locals()`` method when creating a
new context - typically when including/importing a template with
context. For that use case local template variables created using
``set`` should override variables from higher contexts - either from the
play or any parent template, or both; Jinja behaves the same way.

Also removes AnsibleJ2Vars.extras instance variable which is not used.

Also adds missing test for #6653.

Fixes #72262
Fixes #72615

ci_complete

(cherry picked from commit a2af843)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue/PR relates to a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants