loop vs with_items and union(query('vars', item))

Trying to switch from with_items to loops and having problems. Similar to the problem I posted about at https://groups.google.com/d/msg/ansible-project/UTXDN4pztvM/EGppDy1CAwAJ

I do not understand why item is undefined in the in the loop task.

Simple task list

  • debug:
    msg: “XXXXX hostvars {{ hostvars[inventory_hostname]|select(‘match’,‘.*apache2_additional_packages$’)|flatten(levels=1) }}”
    tags: apache2-servers

  • debug:
    msg: “XXXXX loop {{ item }}”
    loop: “{{ hostvars[inventory_hostname]|select(‘match’,‘.*apache2_additional_packages$’)|flatten(levels=1) }}”
    tags: apache2-servers

Here is the ouput

TASK [apache2-servers : debug] *************************************************
ok: [www.aws.comap.com] => {
“msg”: “XXXXX hostvars [‘apache2_additional_packages’, ‘apache2_apache2_additional_packages’, ‘php_apache2_additional_packages’]”
}

TASK [apache2-servers : debug] *************************************************
fatal: [www.aws.comap.com]: FAILED! => {“msg”: “The task includes an option with an undefined variable. The error was: ‘item’ is undefined\n\nThe error appears to be in ‘/Users/tanner/projects/py3-ansible/src/playbooks.git/roles/apache2-servers/tasks/main.yml’: line 20, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- debug:\n ^ here\n”}

The hostvars tasks looks like it output a list?

The loop tasks fails with “… undefined variable. The error was: ‘item’ is undefined…”

Not sure what I am doing wrong.

Any help?

loop is indented to much, remove 2 spaces so it align on the same level as debug.

Wow, how embarrassing!

Thank you!