Looping through found files in Ansible I am trying to set the sendmail_path to use Mailhog in all php.ini files on a server with multiple versions of PHP installed for PHP FPM.

Why is the following snippet is not working?

- hosts: localhost
  tasks:
    - name: Find ini files
      find:
        paths: /home/myuser/projects/infra/php
        file_type: file
        recurse: Yes
        patterns: "*php.ini"
      register: files_matched

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

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

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


The error I am getting is essentially No such file or directory: '' on the item.path variable.

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: OSError: [Errno 2] No such file or directory: ''
failed: [localhost] (item=/home/myuser/projects/infra/php/7.0/fpm/php.ini) =&gt; {"changed": false, "item": {"atime": 1551116700.7761922, "ctime": 1551115509.596934, "dev": 66309, "gid": 1000, "gr_name": "myuser", "inode": 5640238, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mode": "0644", "mtime": 1544171307.0, "nlink": 1, "path": "/home/myuser/projects/infra/php/7.0/fpm/php.ini", "pw_name": "myuser", "rgrp": true, "roth": true, "rusr": true, "size": 70999, "uid": 1000, "wgrp": false, "woth": false, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}, "module_stderr": "Traceback (most recent call last):\n  File \"/home/myuser/.ansible/tmp/ansible-tmp-1551192384.86-125024514722793/AnsiballZ_ini_file.py\", line 113, in &lt;module&gt;\n    _ansiballz_main()\n  File \"/home/myuser/.ansible/tmp/ansible-tmp-1551192384.86-125024514722793/AnsiballZ_ini_file.py\", line 105, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/myuser/.ansible/tmp/ansible-tmp-1551192384.86-125024514722793/AnsiballZ_ini_file.py\", line 48, in invoke_module\n    imp.load_module('__main__', mod, module, MOD_DESC)\n  File \"/tmp/ansible_ini_file_payload_yVPEa4/__main__.py\", line 342, in &lt;module&gt;\n  File \"/tmp/ansible_ini_file_payload_yVPEa4/__main__.py\", line 322, in main\n  File \"/tmp/ansible_ini_file_payload_yVPEa4/__main__.py\", line 151, in do_ini\n  File \"/usr/lib/python2.7/os.py\", line 157, in makedirs\n    mkdir(name, mode)\nOSError: [Errno 2] No such file or directory: ''\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}


As you can see from the error and debug output the file path should not be empty.
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;