循环遍历 Ansible 中的文件

75次阅读
没有评论

问题描述

在 Ansible 中,用户试图在服务器上的所有 php.ini 文件中设置 sendmail_path,以便在具有多个已安装 PHP 版本的服务器上使用 Mailhog。然而,下面的代码片段为什么无法正常工作呢?

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

用户得到的错误基本上是item.path变量上的No such file or directory: ''错误。

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}

解决方案

以下操作可能涉及版本差异,注意备份数据后再进行操作。

在你的 Ansible 代码中,问题的关键是 item.path 变量无法正确传递到 ini_file 模块中。这是因为在你的循环控制中,使用了错误的语法。正确的写法应该是 path: "{{ item.path }}"

以下是修复后的 Ansible 代码示例:

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

修复后的代码中,将 path: "{{ item.path }}" 应用于 ini_file 模块中,确保正确传递文件路径。这样应该能够解决你遇到的问题。

请注意,这只是修复问题的一种方法,具体的解决方案可能会因系统和环境的不同而有所不同。在进行任何更改之前,请务必备份相关文件和数据,以防万一出现意外情况。

正文完