Daily Shaarli

All links of one day in a single page.

February 8, 2026

Return value from specific key in specific list element (WIP)

Let's say you have a list of dicts, e.g.,

unison_jobs:
  - name: sites
    roots:
      - "root = {{ ansible_env.HOME }}/sites"
      - "root = ssh://luxor/{{ ansible_env.USER }}/sites"
    opt: "-auto"
  - name: projects
    roots:
      - "root = {{ ansible_env.HOME }}/projects"
      - "root = ssh://luxor/{{ ansible_env.USER }}/projects"
    opt: "-auto"

and you want to reference the first root element of the job named projects.
Notice that I want to reference the dicts by its name value and not by its index number (so not simply unison_jobs[n]).

This is not that hard, but enough time passes between occurrences that I forget the specifics and have to look it up again. I hope by writing it down that I will strengthen my ability to recall, or at least give myself a fixed spot to look for it.

{{ unison_jobs | selectattr('name, 'equalto', 'projects') | map(attribute='roots') | first }}

How does map(attribute='somekey') work? Does it return a list even if the referenced key is a non-list item? Yes, by default it always returns a list. Does that mean we can set some option to disable that behaviour? Does not look like it, no.