Skip to content

Exception in safe_eval() on expr with jinja2 >= 2.9 #20098

@major

Description

@major
ISSUE TYPE
  • Bug Report
COMPONENT NAME

ansible core

ANSIBLE VERSION
ansible 2.2.0.0
  config file = 
  configured module search path = Default w/o overrides
CONFIGURATION

Defaults.

OS / ENVIRONMENT

N/A (tested on CentOS 7, Ubuntu 16.04, Fedora 25)

SUMMARY

Ansible 2.2.0.0 with jinja2 < 2.9 will handle a variable with curly braces properly in self_eval(). However, jinja >= 2.9 fails with an exception.

STEPS TO REPRODUCE

Here's an example playbook:

---

- hosts: all
  vars:
    fruits:
      - name: apple
        enjoy: yes
      - name: orange
        enjoy: no
      - name: strawberry
        enjoy: yes
  tasks:

    - name: Loop through my fruits
      debug:
        msg: |
          {% set fruit_list = [] %}
          {% for fruit_dict in item[1] %}
          {{ fruit_dict.name }}
          {% endfor %}
      with_items:
        - "{{ fruits | groupby('enjoy') }}"

Start with jinja2 2.8.1. The playbook should execute successfully. Upgrade to jinja2 2.9 (or higher). The playbook will have an error (see below).

EXPECTED RESULTS

The playbook should run without errors.

ACTUAL RESULTS
TASK [Loop through my fruits] **************************************************
 [WARNING]: Exception in safe_eval() on expr: [_GroupTuple(grouper=False, list=[{u'enjoy': False, u'name': u'orange'}]), _GroupTuple(grouper=True,
list=[{u'enjoy': True, u'name': u'apple'}, {u'enjoy': True, u'name': u'strawberry'}])] (invalid expression ([_GroupTuple(grouper=False, list=[{u'enjoy':
False, u'name': u'orange'}]), _GroupTuple(grouper=True, list=[{u'enjoy': True, u'name': u'apple'}, {u'enjoy': True, u'name': u'strawberry'}])]))

fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'unicode object' has no attribute 'name'\n\nThe error appears to have been in '/tmp/ansible/playbook.yml': line 14, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Loop through my fruits\n      ^ here\n"}

I still get the same problem if I install ansible from the latest commit in git.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Priority 2 - Issue Blocks Releaseaffects_2.2This issue/PR affects Ansible v2.2bugThis issue/PR relates to a bug.c:plugins/filter

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions