Skip to content

roles: do not reference the loop variable in task names #736

Open

Description

@NavidSassan

The strategy templates a task name once, before the loop starts, with undefined variables allowed. At that point item is not defined yet. On ansible-core 2.16 the whole name stays raw (systemctl mask {{ item["service"] }}.{{ unit_type }}, even the parts that are defined). On 2.19 and newer it renders as << error 1 - 'item' is undefined >> and every affected task prints [WARNING]: Encountered 1 template error., so switching the execution environment to 2.19+ adds this warning to every run of setup_basic and the role playbooks.

On main, 55 looped tasks and includes in 20 roles reference the loop variable in name, most of them in matomo_import_logs, systemd_unit, lvm, wordpress, podman_containers, monitoring_plugins and login, plus roles/shared/tasks/validate-role-arguments.yml.

Suggested fix:

  • Rewrite the names with <placeholder> for the variable parts, as nmcli connection modify <profile> already does, and / for alternatives, as in pvcreate / pvresize. Examples: systemctl disable --now <service>, mkfs --type <fstype> /dev/<vg>/<lv>. The item is still shown per iteration through loop_control.label (see roles: give every loop a loop_control label #683).
  • Add a CONTRIBUTING rule after "Name tasks after their respective shell commands".
  • Add a local pre-commit hook (script in tools/, files ^roles/.*/(tasks|handlers)/.*\.yml$) that rejects a looped task whose name references its loop variable. A unit test would not catch new cases, because the unit-test workflow does not run for changes below roles/.

Activity

  1. markuslf commented on Nov 18, 2026

    @markuslf
    Member

    Hosts are affected the same way: the strategy renders the name once with the variables of the first host and hands that name to the callbacks for the results of all hosts (verified on ansible-core 2.16.19 and 2.18.14). A task like Remove the loopback entries of {{ hostname__full_hostname }} from /etc/hosts reports the FQDN of the first host for every host, which the dashboard callback shows per host in its LAST WORST column. The <placeholder> rule and the pre-commit hook should cover every host-specific variable in name, not only the loop variable. roles/hostname is fixed in #777; about 14 more task names in other roles reference inventory_hostname, ansible_fqdn and similar variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions