Bug 1697237

Summary: Jinja2 templating breaks with "<lambda>() takes exactly 0 arguments (1 given)" in Ansible
Product: Red Hat Enterprise Linux 7 Reporter: Till Maas <till>
Component: python-jinja2Assignee: Python Maintainers <python-maint>
Status: CLOSED ERRATA QA Contact: Lukas Zachar <lzachar>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 7.7CC: cstratak, hhorak, jkejda, mhroncok, pviktori
Target Milestone: rc   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Fixed In Version: python-jinja2-2.7.2-3.el7 Doc Type: No Doc Update
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2019-08-06 13:17:46 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Description Till Maas 2019-04-08 07:28:12 UTC
Description of problem:
This ansible playbook breaks on RHEL 7.6:

- hosts: localhost
    pci_address: "virtio0"
    device_by_pci_address: "{{
        dict(ansible_facts | json_query('@.*| [?pciid].[pciid, device ]'))
    - name: Show devices by PCI address
        var: device_by_pci_address
    - name: "Show device with PCI address {{ pci_address }}"
        msg: "The device {{ device_by_pci_address[pci_address] }} is at the
           PCI address {{ pci_address }}"

Version-Release number of selected component (if applicable):

How reproducible:

Steps to Reproduce:
python2 -c 'import jinja2; jinja2.Environment().from_string("{{ dict([(0, 1)]) }}").render()'

or ansible-playbook on the playbook mentioned above

Actual results:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "<template>", line 1, in top-level template code
TypeError: <lambda>() takes exactly 0 arguments (1 given)

Expected results:
should not raise the exceptions

Additional info:
I am not 100% sure that the minimum example hightlights the problem. At least it triggers the same error and it does not raise on Fedora.

Comment 2 Till Maas 2019-04-09 06:38:18 UTC
Minimal example using Ansible:
cat > 'dict_test.yml' <<EOF
- hosts: localhost
    dict_test: "{{ dict([(0, 1)]) }}"
    - name: Show dict
        var: dict_test

Test run on RHEL 7.6:
ansible-playbook ~/dict_test.yml 
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] **********************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************
ok: [localhost]

TASK [Show dict] **********************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "An unhandled exception occurred while templating '{{ dict([(0, 1)]) }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: Unexpected templating type error occurred on ({{ dict([(0, 1)]) }}): <lambda>() takes exactly 0 arguments (1 given)"}
	to retry, use: --limit @/root/dict_test.retry

PLAY RECAP ****************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1   


It works on Fedora 28:
ansible-playbook dict_test.yml 
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] *****************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************************
ok: [localhost]

TASK [Show dict] *****************************************************************************************************************************************************************************************
ok: [localhost] => {
    "dict_test": {
        "0": 1

PLAY RECAP ***********************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0

Comment 3 Miro Hrončok 2019-04-09 09:13:38 UTC
FWIW this started working in Jinja2 2.8 upstream.

Nothing interesting in the chnagelog: http://jinja.pocoo.org/docs/2.10/changelog/#version-2-8

Comment 5 Till Maas 2019-04-10 08:23:50 UTC
(In reply to Miro Hrončok from comment #4)
> Upstream fix:
> https://github.com/pallets/jinja/commit/
> 6179c02c91800d220de03006117afa5e6d60f0f6

Awesome, this simple fix makes it work! I patched it on my test VM and now the playbooks work again. Would be great to get this shipped!

Comment 6 Miro Hrončok 2019-04-10 11:07:15 UTC
I'll send a Pagure PR, but somebody else from the team needs to take it from there.

Comment 12 errata-xmlrpc 2019-08-06 13:17:46 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.