Ansible: register variable with loop
Q: &quot;Check if all items are own by root.&quot;
A: Put the list of the tools into the variable audit_tools. Compare the length of the lists. For example
- hosts: localhost
  vars:
    audit_tools:
      - auditctl
      - aureport
      - ausearch
      - autrace
      - auditd
      - audispd
      - augenrules
  tasks:
    - block:
        - stat:
            path: &quot;/sbin/{{ item }}&quot;
          loop: &quot;{{ audit_tools }}&quot;
          register: result
        - assert:
            that: no_audit_tools == no_owner_root
            fail_msg: &quot;One or more tools are not own by root.&quot;
          vars:
            no_audit_tools: &quot;{{ audit_tools|length }}&quot;
            no_owner_root: &quot;{{ result.results|
                               json_query('[?stat.pw_name==`root`]')|
                               length }}&quot;
      rescue:
        - debug:
            msg: &quot;Rescue: audit tools not owned by root.&quot;

If not all items are owned by root assert will fail and the block will proceed to the rescue section
TASK [assert] ****
fatal: [localhost]: FAILED! =&gt; changed=false 
  assertion: no_audit_tools == no_owner_root
  evaluated_to: false
  msg: One or more tools are not own by root.

TASK [debug] ****
ok: [localhost] =&gt; 
  msg: 'Rescue: audit tools not owned by root.'


Q: &quot;This solution requires JMESPath to be installed. Is there an alternative solution?&quot;
A: Yes. It is. Use Jinja filter selectattr
            no_owner_root: &quot;{{ result.results|
                               selectattr('stat.pw_name', 'eq', 'root')|
                               list|length }}&quot;