Ash*_*har 6 directory search find ansible
test-juli.jar这是我的剧本,我尝试在目录下查找/app,但是,我希望/app/Patchbackup从搜索中排除文件夹。
以下是我的剧本:
tasks:
- name: Find test home directories under /app
find:
paths: /app
file_type: any
recurse: yes
depth: 4
patterns: 'test-juli.jar'
excludes: 'log,tmp,.installation,Patchbackup'
tags: always
register: tomjarfound
- debug:
msg: "ALL LISTED REFINED JARS: {{ item.path }}"
loop: "{{ tomjarfound.files }}"
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,我预计find不会在下面找到/app/Patchbackup,但输出显示它确实存在,尽管被排除在外。
这是输出:
TASK [debug] ***********************************************************************************************************************************************************
ok: [10.0.0.11] => (item=/app/apache-test-9.0.10/bin/test-juli.jar) => {
"msg": "ALL LISTED REFINED JARS: /app/apache-test-9.0.10/bin/test-juli.jar"
}
ok: [10.0.0.11] => (item=/app/Patchbackup/app/apache-test-9.0.10/bin/test-juli.jar) => {
"msg": "ALL LISTED REFINED JARS: /app/Patchbackup/app/apache-test-9.0.10/bin/test-juli.jar"
}
Run Code Online (Sandbox Code Playgroud)
您能否建议我如何/app/Patchbackup从 ansible 的 find 中排除该文件夹?
该find模块可能已损坏。该excludes参数仅适用于最终结果集中的项目,而不适用于用于中间目录。
也就是说,如果您有这样的目录结构:
toplevel/
foo/
testfile1.txt
bar/
testfile2.txt
Run Code Online (Sandbox Code Playgroud)
从toplevel目录的父目录运行如下任务:
- find:
paths: toplevel
recurse: true
excludes: foo
register: results
Run Code Online (Sandbox Code Playgroud)
您的结果集将如下所示:
toplevel/bar/testfile2.txttoplevel/foo/testfile1.txt将其与设置进行比较file_type: any,如下所示:
- find:
paths: toplevel
file_type: any
recurse: true
excludes: foo
register: results
Run Code Online (Sandbox Code Playgroud)
在这种情况下,结果集将如下所示:
- `toplevel/bar`
- `toplevel/bar/testfile2.txt`
- `toplevel/foo/testfile1.txt`
Run Code Online (Sandbox Code Playgroud)
请注意,toplevel/bar包含在结果中,但toplevel/foo被排除在外。那是因为:
file_type: any,这意味着我们想要查找目录和文件。toplevel/foo结果集的一部分。foo如果您只需使用以下命令,您将获得更灵活的行为find:
- name: exclude an intermediate directory with find command
command: >-
find toplevel -name foo -prune -o -type f -print
register: result
Run Code Online (Sandbox Code Playgroud)
这将返回以下项目:
toplevel/bar/testfile2.txt我已将上述的可运行版本放在github 上。
正如您所意识到的,该find模块没有为您提供从根目录开始/app但排除其目录之一的选项。您有几个选择:
/app不多,您可以使用该子句paths提供它们的列表(排除不需要的目录)Patchbackup)/app/Patchbackup您不想要的结果。以下是如何过滤掉它们的示例:代码:
- set_fact:
my_results_final: "{{ my_results_final | default([]) + [item] }}"
when: item is not regex('^/app/Patchbackup(.+)')
with_items:
- "{{ tomjarfound.files }}"
- debug:
var: my_results_final
Run Code Online (Sandbox Code Playgroud)