【问题标题】:ansible how do i search for a directory/subdirectory like 'home/ansible'ansible 我如何搜索像“home/ansible”这样的目录/子目录
【发布时间】:2020-07-29 18:59:48
【问题描述】:

在 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”,它会起作用,但会给我更多的结果,然后我需要。

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

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

【问题讨论】:

  • 来自文档:The pattern is matched against the file base name, excluding the directory. 所以你试图做的事情是行不通的(至少使用 find 模块)。如果通过shellmodule 可用,您可以考虑在这种特定情况下使用locate。我不知道有任何 ansible 模块能够满足您的要求(但我很乐意被证明是错误的)。
  • @Zeitounator 这也是我的想法,但我希望我也错了:-)

标签: regex linux design-patterns ansible find


【解决方案1】:

认为 find 模块进行了原始搜索。因此,它无法通过权限限制访问所有内容,或者在非常大的网络存储中需要很长时间。

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

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

- 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 文档示例。

【讨论】:

  • 我以home/ansible 为例,但我最终要做的是找到产品的安装目录,比如“product1”......当我使用 find 模块搜索时'product1' 的模式,它会返回大量结果,因为有各种目录包含该模式。因此,要获取安装目录,我实际上想搜索“webapps/product1”的子集作为模式。您在上面使用的示例不允许这样做,因为“路径”需要一个特定的目录。我感觉没有模块可以做到这一点,但我希望大声笑
  • 好的。如果产品由包管理器安装,您可以解析安装脚本。或者,如果产品二进制文件在 $PATH 中,那么您可以执行“which ”。但如果没有其他信息,我也只是看到一个脚本试图遍历所有路径并尝试对其执行模式匹配。
  • 该产品以各种不同的方式安装在数百台服务器上。所以最简单的解决方案是搜索模式webapps/product1,因为这将显示真实位置。我曾希望find 模块,使用目录的file_type,能够处理这个问题,但显然它只能搜索一个简单的模式。谢谢!
猜你喜欢
  • 2020-06-26
  • 2019-10-28
  • 1970-01-01
  • 1970-01-01
  • 2013-10-22
  • 2016-08-23
  • 2020-05-01
  • 2014-05-15
  • 1970-01-01
相关资源
最近更新 更多