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
- hosts: localhost
connection: local
gather_facts: no
vars:
computer:
architect:
dmz_info:
- app: nginx
ip_addr: 192.168.1.100
app_info:
- app: tomcat
ip_addr: 10.10.10.50
- app: mysql
ip_addr: 10.10.10.60
password: P@55w0rD
tasks:
- name: Test 02
debug: msg="{{ 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."}
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
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
I can verify the latest version is installed/upgraded successfully using pip show jinja2 again.
Now once again I tested ansible yml playbook. This time it gave me filtered information in json format as expected.
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