Ansible Series

🔁 The Top 1% Hands-On for Experiencing Ansible's Jinja2 Filters and the loop/when Gotchas

Introduction

This article is part of the Top 1% Series Complete Article Guide, the 8th article in the Ansible Series.

The Big Picture

This hands-on consists of four steps.

graph LR
    Step1["Step1<br/>Process a list with<br/>a Jinja2 filter"]
    Step2["Step2<br/>Capture a command<br/>result with register"]
    Step3["Step3<br/>Combine loop<br/>with when"]
    Step4["Step4<br/>Customize change detection<br/>with changed_when"]
    Step1 --> Step2 --> Step3 --> Step4

Hands-On Steps

Step 1: Process a list with a Jinja2 filter

Process a list of package names using filters.

- name: Show a filtered and sorted package list
  debug:
    msg: "{{ packages | select('match', '^nginx') | list | sort }}"
  vars:
    packages: ["nginx", "mysql-server", "nginx-extras", "redis"]

This writes a three-stage pipeline in a single line: extracting matching elements with the select filter, explicitly turning them into a list with list, and sorting with sort. Jinja2 filters chain together with |, and you can apply several in sequence.

Step 2: Capture a command result with register

Capture a command’s execution result into a variable with register, and inspect its content.

- name: Check running processes
  command: systemctl is-active nginx
  register: nginx_status
  ignore_errors: true

- name: Show the full register content
  debug:
    var: nginx_status

A registered variable’s content isn’t a simple string — it’s a structured dictionary with keys like stdout, stderr, rc (exit code), and changed. Pulling out only the key you need, like nginx_status.stdout, is the standard real-world practice.

Step 3: Combine loop with when

For multiple services, combine loop with when to restart only the ones that are currently active.

- name: Check each service status
  command: "systemctl is-active {{ item }}"
  register: service_status
  loop: ["nginx", "redis", "mysql"]
  ignore_errors: true

- name: Restart only the services that are currently active
  service:
    name: "{{ item.item }}"
    state: restarted
  loop: "{{ service_status.results }}"
  when: item.stdout == "active"

The easily-overlooked spec here is that when is evaluated individually, per loop item. service_status.results stores each iteration’s result as an array, and it’s a common real-world misconception that meeting the condition for just one of them triggers execution for the whole thing — in reality, each element’s condition is judged independently.

Step 4: Customize change detection with changed_when

The command module is always treated as changed by default, so customize actual change detection with changed_when.

- name: Check nginx config syntax
  command: nginx -t
  register: nginx_check
  changed_when: false
  failed_when: nginx_check.rc != 0

Specifying changed_when: false accurately tells Ansible “this task is only checking state — it hasn’t changed anything.” Without it, every task using the command module gets treated as “changed,” which can cause a Handler to fire unnecessarily.

What a Pro Sees Here (Top 1% Understanding)

Idempotency and the changed judgment are different concepts

“Idempotency” (running the same Playbook any number of times produces the same result), covered in Understanding What Ansible Is the Top 1% Way, and the “changed judgment” (reporting whether this task actually changed anything) look similar but are different concepts. The command and shell modules always report changed: true by default, because Ansible has no way to judge whether that particular command itself is idempotent. Without understanding that accurately reporting the execution result requires explicitly writing a condition with changed_when, you end up with the bug of a Handler firing unintentionally every single time.

Why the seemingly redundant item.item is necessary

Step 3 uses the seemingly redundant expression item.item. That’s because the item being iterated by loop: "{{ service_status.results }}" is actually “the entire result of the previous task” (a dictionary containing stdout, rc, and more), and inside it, the item key holds the original value used by the loop before that one (such as "nginx"). Without understanding this pattern — passing a registered result straight into the next loop, which comes up constantly in real-world work — you can’t make sense of what item.item even means, and the code becomes unreadable.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. You get an error around an expression like item.item: Check whether you’re feeding a registered result into loop, versus feeding the original list directly into loop, and confirm item’s actual structure with the debug module.
  2. A Handler fires unintentionally every time: Check whether changed_when is set on tasks using the command/shell module.
  3. The select filter’s result comes back empty: Check whether the regex pattern is correct, and whether you should be using match (prefix match) or search (substring match).

Summary

Takeaways to Apply Today

  1. Whenever you use the command/shell module, always consider changed_when.
  2. If you lose track of what’s inside a registered variable, first check its structure with debug: var=variable_name.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).