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

Jinja2 3.1 Breaks filter #77413

Closed
1 task done
rgajason opened this issue Mar 30, 2022 · 9 comments
Closed
1 task done

Jinja2 3.1 Breaks filter #77413

rgajason opened this issue Mar 30, 2022 · 9 comments
Labels
affects_2.12 bug This issue/PR relates to a bug. support:core This issue/PR relates to code supported by the Ansible Engineering Team. test This PR relates to tests.

Comments

@rgajason
Copy link

rgajason commented Mar 30, 2022

Summary

Version 3.1 of Jinja2 removed deprecated code, including environmentfilter: pallets/jinja#1544

Attempting to execute Ansible operations that use filter with Jinja2 >= 3.1 results in:

[WARNING]: Skipping plugin (/opt/python3/lib/python3.10/site-packages/ansible/plugins/filter/core.py) as it seems to be invalid: cannot import name 'environmentfilter' from 'jinja2.filters'  (/opt/python3/lib/python3.10/site-packages/jinja2/filters.py)

This issue appears to have been originally addressed here: #74667

Issue Type

Bug Report

Component Name

filter

Ansible Version

$ ansible --version
ansible [core 2.12.3]
  config file = None
  configured module search path = ['/home/jenkins/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/python3/lib/python3.10/site-packages/ansible
  ansible collection location = /home/jenkins/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.10.4 (main, Mar 24 2022, 16:14:08) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
  jinja version = 3.1.1
  libyaml = False

Configuration

$ ansible-config dump --only-changed
<no output>

OS / Environment

Linux (EL7)
Python 3.10.4 compiled from source
Jinja2 3.1.1
Ansible 5.5.0 (according to pip3 freeze)
Ansible Core 2.12.3 (according to pip3 freeze)

Same results with Ansible 2.10.7 and Ansible Base 2.10.9 using Python 3.9.10.

Steps to Reproduce

Install Jinja2 version 3.1.x
Execute playbook

Expected Results

No Python import error

Actual Results

[WARNING]: Skipping plugin (/opt/python3/lib/python3.10/site-packages/ansible/plugins/filter/core.py) as it seems to be invalid: cannot import name 'environmentfilter' from 'jinja2.filters'  (/opt/python3/lib/python3.10/site-packages/jinja2/filters.py)

Code of Conduct

  • I agree to follow the Ansible Code of Conduct
@ansibot
Copy link
Contributor

ansibot commented Mar 30, 2022

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibot ansibot added affects_2.12 bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. test This PR relates to tests. labels Mar 30, 2022
@bcoca
Copy link
Member

bcoca commented Mar 30, 2022

fixed in #75881, which applied it to 2.13

@mkrizek
Copy link
Contributor

mkrizek commented Mar 30, 2022

This actually looks more like a duplicate of #77406, #77383 and #77356.

Same results with Ansible 2.10.7 and Ansible Base 2.10.9 using Python 3.9.10.

This is expected, the versions of Ansible that this was fixed (#74666) in are 2.9.22, 2.10.10, 2.11.1 and 2.12.0.

Ansible Core 2.12.3 (according to pip3 freeze)

This should not be the case. Can you double check that you are indeed running 2.12.3 version and that /opt/python3/lib/python3.10/site-packages/ansible/__init__.py (inferred from your output) contains the fix from f99d024#diff-8dc7a6a3bbcf9fbd56c4f0a97c98cf20c58ad49c588a1e6937c4197c24deed0f? Otherwise it might indicate that something is wrong with the installation.

needs_info

@ansibot ansibot added the needs_info This issue requires further information. Please answer any outstanding questions. label Mar 30, 2022
@xfyre
Copy link

xfyre commented Mar 31, 2022

@mkrizek Could you confirm it was fixed in 2.9.22?

I've upgraded to this version and I'm still getting the same issue:

2022-03-31 01:39:05
dcnet-build
TASK [Retrieve project names] **************************************************
2022-03-31 01:39:05
dcnet-build
[WARNING]: Skipping plugin (/usr/local/lib/python3.9/dist-
2022-03-31 01:39:05
dcnet-build
packages/ansible/plugins/filter/core.py) as it seems to be invalid: cannot
2022-03-31 01:39:05
dcnet-build
import name 'environmentfilter' from 'jinja2.filters'
2022-03-31 01:39:05
dcnet-build
(/usr/local/lib/python3.9/dist-packages/jinja2/filters.py)
2022-03-31 01:39:05
dcnet-build
[WARNING]: Skipping plugin (/usr/local/lib/python3.9/dist-
2022-03-31 01:39:05
dcnet-build
packages/ansible/plugins/filter/mathstuff.py) as it seems to be invalid: cannot
2022-03-31 01:39:05
dcnet-build
import name 'environmentfilter' from 'jinja2.filters'
2022-03-31 01:39:05
dcnet-build
(/usr/local/lib/python3.9/dist-packages/jinja2/filters.py)
2022-03-31 01:39:05
dcnet-build
fatal: [localhost]: FAILED! => {"msg": "template error while templating string: No filter named 'flatten'.. String: {{ commit_message | regex_findall('(dcnet-server|dcnet-nuxt|dcnet-redir)@([a-f0-9]+)') | flatten }}"}
2022-03-31 01:39:05
dcnet-build

2022-03-31 01:39:05
dcnet-build
PLAY RECAP *********************************************************************
2022-03-31 01:39:05
dcnet-build
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
2022-03-31 01:39:05
dcnet-build

@Akasurde
Copy link
Member

@xfyre For me it works -

---
- hosts: localhost
  gather_facts: no
  tasks:
    - debug:
        msg: "{{ 1 | pow(2) }}"
PLAY [localhost] ***************************************************************

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "1.0"
}

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible --version
ansible 2.9.22
  config file = None
  configured module search path = ['/Users/akasurde/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Volumes/data/src/ansible/lib/ansible
  executable location = /Volumes/data/src/ansible/bin/ansible
  python version = 3.9.10 (main, Jan 15 2022, 11:48:00) [Clang 13.0.0 (clang-1300.0.29.3)]

@mkrizek
Copy link
Contributor

mkrizek commented Mar 31, 2022

@mkrizek Could you confirm it was fixed in 2.9.22?

Yes.

I've upgraded to this version and I'm still getting the same issue:

2022-03-31 01:39:05
dcnet-build
TASK [Retrieve project names] **************************************************
2022-03-31 01:39:05
dcnet-build
[WARNING]: Skipping plugin (/usr/local/lib/python3.9/dist-
2022-03-31 01:39:05
dcnet-build
packages/ansible/plugins/filter/core.py) as it seems to be invalid: cannot
2022-03-31 01:39:05
dcnet-build
import name 'environmentfilter' from 'jinja2.filters'
2022-03-31 01:39:05
dcnet-build
(/usr/local/lib/python3.9/dist-packages/jinja2/filters.py)
2022-03-31 01:39:05
dcnet-build
[WARNING]: Skipping plugin (/usr/local/lib/python3.9/dist-
2022-03-31 01:39:05
dcnet-build
packages/ansible/plugins/filter/mathstuff.py) as it seems to be invalid: cannot
2022-03-31 01:39:05
dcnet-build
import name 'environmentfilter' from 'jinja2.filters'
2022-03-31 01:39:05
dcnet-build
(/usr/local/lib/python3.9/dist-packages/jinja2/filters.py)
2022-03-31 01:39:05
dcnet-build
fatal: [localhost]: FAILED! => {"msg": "template error while templating string: No filter named 'flatten'.. String: {{ commit_message | regex_findall('(dcnet-server|dcnet-nuxt|dcnet-redir)@([a-f0-9]+)') | flatten }}"}
2022-03-31 01:39:05
dcnet-build

2022-03-31 01:39:05
dcnet-build
PLAY RECAP *********************************************************************
2022-03-31 01:39:05
dcnet-build
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
2022-03-31 01:39:05
dcnet-build

Assuming the location from the provided output, what is the output of the following command?

grep "patch Jinja2 >= 3.0 for backwards compatibility" /usr/local/lib/python3.9/dist-packages/ansible/__init__.py

@jborean93 jborean93 removed the needs_triage Needs a first human triage before being processed. label Mar 31, 2022
@xfyre
Copy link

xfyre commented Mar 31, 2022

@mkrizek Thanks for looking into this. I appears that, indeed, everything is working is expected, and my problem was related to the CodeShip "cache reset" feature not doing what it should (cached docker image with an old version somehow stuck and kept giving me the same error).

My builds today are working with 2.9.22.

@rgajason
Copy link
Author

rgajason commented Apr 1, 2022

Things are also working for me. The Docker image I was testing that originally led to this bug report was destroyed and I don't have a list of Python modules/versions that were installed outside of ansible/ansible-base/Jinja2. Several modules that I install have changed their versions and dependency requirements since I reported this and I'm unable to find the right combination to recreate.

Thanks to all who spent time on this.

@ansibot ansibot removed the needs_info This issue requires further information. Please answer any outstanding questions. label Apr 1, 2022
@mkrizek
Copy link
Contributor

mkrizek commented Apr 1, 2022

Closing as per above.

If you have further questions please stop by IRC or the mailing list:

@mkrizek mkrizek closed this as completed Apr 1, 2022
@ansible ansible locked and limited conversation to collaborators Apr 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.12 bug This issue/PR relates to a bug. support:core This issue/PR relates to code supported by the Ansible Engineering Team. test This PR relates to tests.
Projects
None yet
Development

No branches or pull requests

7 participants