Looping through found files in Ansible
OK. The answer was simple. I needed to template the item.path. I guess this is a beginner mistake.
- hosts: localhost
  tasks:
    - name: Find ini files
      find:
        paths: /home/myuser/projects/infra/php
        file_type: file
        recurse: Yes
        patterns: &quot;*php.ini&quot;
      register: files_matched

    - name: Debug files_matched
      debug:
        var: files_matched.files

    - name: Debug files_matched loop
      debug:
        var: item.path
      loop: &quot;{{ files_matched.files|flatten(levels=1) }}&quot;
      loop_control:
        label: &quot;{{ item.path }}&quot;

    - name: Ensure &quot;sendmail_path=/opt/mailhog/mhsendmail is in section &quot;[mail function]&quot;
      ini_file:
        path: &quot;{{ item.path }}&quot;
        section: &quot;mail function&quot;
        option: sendmail_path
        value: /opt/mailhog/mhsendmail
        mode: 0644
        backup: no
      loop: &quot;{{ files_matched.files|flatten(levels=1) }}&quot;
      loop_control:
        label: &quot;{{ item.path }}&quot;