HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Looping through found files in Ansible

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
loopingfoundfilesthroughansible

Problem

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 (

Solution

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 }}"

Code Snippets

- 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 }}"

Context

StackExchange DevOps Q#6484, answer score: 5

Revisions (0)

No revisions yet.