Ansible Jinja2 length Filter — Count Items and String Length

The length filter (also aliased as count) is one of the most used Jinja2 filters in Ansible. It returns the number of items in a list or the number of characters in a string.

Basic Syntax

# Count list items
{{ my_list | length }}

# Measure string length
{{ my_string | length }}

# Alias — count works the same way
{{ my_list | count }}

Count Items in a List

- name: Show number of hosts in inventory
  ansible.builtin.debug:
    msg: "Total hosts: {{ groups['all'] | length }}"

- name: Show number of installed packages
  ansible.builtin.debug:
    msg: "{{ ansible_facts.packages | length }} packages installed"

Use length in Conditionals

- name: Only run if we have servers to configure
  ansible.builtin.debug:
    msg: "Configuring {{ servers | length }} servers"
  when: servers | length > 0

- name: Fail if no database hosts defined
  ansible.builtin.assert:
    that:
      - db_hosts | length > 0
    fail_msg: "No database hosts defined in db_hosts variable"

Measure String Length

- name: Validate password length
  ansible.builtin.assert:
    that:
      - user_password | length >= 12
    fail_msg: "Password must be at least 12 characters (got {{ user_password | length }})"

- name: Check hostname length
  ansible.builtin.debug:
    msg: "Hostname '{{ inventory_hostname }}' is {{ inventory_hostname | length }} chars"

Count Dictionary Keys

- name: Count configuration options
  ansible.builtin.debug:
    msg: "{{ config | length }} configuration keys defined"
  vars:
    config:
      port: 8080
      host: 0.0.0.0
      debug: false
  # Output: "3 configuration keys defined"

Combine with Other Filters

# Count unique items
{{ my_list | unique | length }}

# Count items matching a condition
{{ users | selectattr('active', 'equalto', true) | list | length }}

# Check if list is empty
{{ my_list | length == 0 }}

# Ternary based on length
{{ "items" if my_list | length > 1 else "item" }}

Full Playbook Example

---
- name: System inventory report
  hosts: all
  gather_facts: true
  tasks:
    - name: Collect system metrics
      ansible.builtin.set_fact:
        report:
          hostname: "{{ inventory_hostname }}"
          hostname_length: "{{ inventory_hostname | length }}"
          mount_points: "{{ ansible_mounts | length }}"
          interfaces: "{{ ansible_interfaces | length }}"
          memory_mb: "{{ ansible_memtotal_mb }}"

    - name: Display report
      ansible.builtin.debug:
        msg: >
          {{ report.hostname }}: {{ report.mount_points }} mounts,
          {{ report.interfaces }} network interfaces

Common Patterns

PatternDescription
list | lengthCount list items
string | lengthString character count
dict | lengthCount dictionary keys
list | length == 0Check if empty
list | length > NMinimum items check
list | unique | lengthCount unique items

Conclusion

The length filter is simple but powerful — use it for validation, conditional logic, and reporting in your Ansible playbooks.