Answers: <p>OK. The answer was simple. I needed to template the item.path. I guess this is a beginner mistake.</p>
  <pre><code>- 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;
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-12-23T08:28:40.753Z
Answers: Please mark the answer as accepted if it solved the issue.
Answers: 030
Answers: CC BY-SA 4.0
Answers: 2019-02-26T15:04:14.870Z
Answers: 2020-12-17T14:28:08.603Z
Answers: 2020-12-17T14:28:08.603Z
Answers: lolcode
Body: <p>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.</p>
  
  <p>Why is the following snippet is not working?</p>
  
  <pre><code>- 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 }}"
  </code></pre>
  
  <p>The error I am getting is essentially <strong>No such file or directory: ''</strong> on the <code>item.path</code> variable.</p>
  
  <pre><code>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}
  </code></pre>
  
  <p>As you can see from the error and debug output the file path should not be empty.</p>
  
Tags: <ansible>
Title: Looping through found files in Ansible
UserDisplayName: lolcode
ThreadText: # Looping through found files in Ansible
  
  (Asked by: username_0 on 2019-02-26)
  
  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) => {"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 <module>\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 <module>\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.
  
  **Tags:** ansible
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-02-26. Score: 5 | Accepted answer
  
  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: "*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 }}"
  
  ```
  
  > **Comments:**
  > **username_1**: Please mark the answer as accepted if it solved the issue.  
  
  ---
  
  