Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Ansible selectattr The error was TemplateRuntimeError no test named 'equalto'

I was working on small Ansible playbook project and wrote a msg to filter nested yaml dictionary as below, in the script is was using selectattr() function to list only selected filtered attribute property information.

Getting started Ansible AWX tower for IT automation run first playbook

hostslocalhost
  connectionlocal
  gather_factsno
  vars:
    computer:
      architect:
        dmz_info:
          - appnginx
            ip_addr192.168.1.100
        app_info:
          - apptomcat
            ip_addr10.10.10.50
          - appmysql
            ip_addr10.10.10.60
            passwordP@55w0rD
  tasks:
    - nameTest 02
      debugmsg="{{ computer.architect.app_info | selectattr("app", "equalto", "mysql") | list }}"

After executing this playbook I started getting below error and tasks were failing.

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TemplateRuntimeError: not test named 'equalto' 
fatal: [localhost]: FAILED => {"msg": "Unexpected failure during module execution."}

ansible-playbook jinja template jinja2 filter selectattr equalto templateruntimeerror not test named equalto module failure play recap tasks list.png

selectattr() is ansible operator, it is a jinja2 template function, this happens due to older version of jinja2 pip module, to resolve this check/upgrade version of Jinja2 (Jinja2 is one of the most used template engines for Python).

https://jinja.palletsprojects.com/en/2.11.x/templates/

pip show jinja2

pip show jinja2 ansible template classifiers python selectattr filter playbook ansible-playbook yaml yml devops automation ansible vsphere.png

I upgraded it to latest version with below command. It will uninstall and install new version of Jinja2 with dependency MarkupSafe pip module.

pip install jinja2 --upgrade

pip install jinja2 --upgrade python ansible module selectattr filter ansible-playbook devops automation module py pip install --upgrade pip.png

I can verify the latest version is installed/upgraded successfully using pip show jinja2 again.

programming langauge python cpython pypy pypi ansible module pip filter selectattr jinja tempaleting jinja2 coding devops automation.png

Now once again I tested ansible yml playbook. This time it gave me filtered information in json format as expected.

ansible play task gathering facts play recap ok changed unreachable failed skipped rescued ignored jinja2 template ansible module filter selectattr ansible-playbook yml yaml.png

Useful articles
How to install Docker on Linux
Cannot connect to the Docker daemon at unix:var run docker.sock. Is the docker daemon running
Docker Error response from daemon i\o timeout internet proxy
How to install Ansible on Linux for vSphere configuration
How to Setup Passwordless SSH Login on Windows
configure remote ssh extension on visual studio code
VS code remote ssh could not establish to host, connecting was canceled

Go Back

Comment

Blog Search

Page Views

3627149

Follow me on Blogarama