ansible 我如何搜索像“home/ansible”这样的目录/子目录

问题描述 投票:0回答:1

在 Ansible v2.8.2 中,如何搜索目录树的子集,例如“home/ansible”?

我尝试使用查找模块:

    - name: Find ansible directory
      become: true
      find:
          paths: /
          recurse: yes
          file_type: directory
          use_regex: yes
          patterns: 'home/ansible'
      register: ansible_dirs

    - name: post ansible dirs
      debug:
          msg:
              - "{{ ansible_dirs }}"

我收到错误:

/proc/20828/task/20828/fd/6 was skipped as it does not seem to be a valid file or it cannot be accessed

如果我将模式更改为“home”或“ansible”,它会起作用,但会提供比我需要的更多结果。

我想我可以采用这个更大的结果集并将其缩减为我需要的内容,但我希望有一种开箱即用的方法可以使用。

非常感谢任何帮助,谢谢!

regex linux ansible find
1个回答
0
投票

认为查找模块执行原始搜索。因此,它无法通过权限限制访问所有内容,或者在非常大的安装网络存储中将永远花费时间。

因此限制您的搜索可能会有所帮助。

如果您只是想测试特定路径是否存在(尚未测试目录是否像文件一样工作):

- name: Wait until the file /tmp/foo is present before continuing
  wait_for:
    path: /tmp/foo

或者您可能会排除您不希望得到结果的路径:

排除您在查找模块中查找的选项。

- name: Find /var/log all directories, exclude nginx and mysql
  find:
    paths: /var/log
    recurse: no
    file_type: directory
    excludes: 'nginx,mysql'

代码提取取自 Ansible 文档示例。

© www.soinside.com 2019 - 2024. All rights reserved.