Ansible Module find: Difference between revisions
(15 intermediate revisions by the same user not shown) | |||
Line 9: | Line 9: | ||
Return a list of files based on specific criteria. Multiple criteria are ANDed together. | Return a list of files based on specific criteria. Multiple criteria are ANDed together. | ||
= | =Module Arguments= | ||
==<tt>paths</tt>== | |||
Each element listed here must be a directory. | |||
==<span id='patterns'></span><tt>patterns|pattern</tt>== | |||
One or more ([[Bash Command Line Expansion#Glob|glob]] or regex) patterns, which type is controlled by <code>[[#use_regex|use_regex]]</code> option. The patterns restrict the list of files to be returned to those whose basenames match at least one of the patterns specified. Multiple patterns can be specified using a list. The pattern is matched against the file base name, excluding the directory. When using regexen, the pattern MUST match the ENTIRE file name, not just parts of it. So if you are looking to match all files ending in .default, you'd need to use '.*\.default' as a regexp and not just '\.default'. This parameter expects a list, which can be either comma separated or YAML. If any of the patterns contain a comma, make sure to put them in a list to avoid splitting the patterns in undesirable ways. Defaults to '*' when use_regex=False, or '.*' when use_regex=True. | |||
==<tt>use_regex</tt>== | |||
A boolean value (<code>yes</code>, <code>no</code>), default <code>no</code>. If <code>no</code>, use shell [[Bash Command Line Expansion#Glob|globs]]. Otherwise use python regular expressions. | |||
==<tt>recurse</tt>== | |||
If target is a directory, recursively descend into the directory looking for files. Values: <code>yes</code>, <code>no</code>. Default is <code>no</code>. | |||
==<tt>file_type</tt>== | |||
A string. Can be: | |||
* "any" | |||
* "directory" | |||
* "file" | |||
* "link" | |||
The default value is "file". | |||
==<tt>depth</tt>== | |||
An integer that specifies the maximum number of levels to descend into. Setting [[#recurse|recurse]] to no will override this value, which is effectively depth 1. Default is unlimited depth. | |||
=Examples= | |||
====Find Files that Match a Glob and Pass them as a List==== | |||
<syntaxhighlight lang='yaml'> | |||
- name: Find files that match a glob and pass them as a List | |||
find: | |||
paths: '/some/dir' | |||
patterns: '*.txt' | |||
register: find_result | |||
- name: debug | |||
debug: | |||
msg: "{{ item }}" | |||
with_items: "{{ find_result.files | json_query('[*].path') }}" | |||
</syntaxhighlight> | |||
====Find Files with Shell Globs==== | |||
Shell [[Bash Command Line Expansion#Glob|glob]] example: | |||
<syntaxhighlight lang='yaml'> | |||
- name: Collect Java module descriptor files from Java Home | |||
find: | |||
paths: '/Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home/jmods' | |||
patterns: '*.jmod' | |||
file_type: file | |||
</syntaxhighlight> | |||
====Find a File==== | |||
<syntaxhighlight lang='yaml'> | <syntaxhighlight lang='yaml'> | ||
- name: Find JetBrains applications | - name: Find JetBrains applications | ||
Line 21: | Line 65: | ||
register: intellij_apps | register: intellij_apps | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 05:12, 5 July 2021
External
Internal
Overview
Return a list of files based on specific criteria. Multiple criteria are ANDed together.
Module Arguments
paths
Each element listed here must be a directory.
patterns|pattern
One or more (glob or regex) patterns, which type is controlled by use_regex
option. The patterns restrict the list of files to be returned to those whose basenames match at least one of the patterns specified. Multiple patterns can be specified using a list. The pattern is matched against the file base name, excluding the directory. When using regexen, the pattern MUST match the ENTIRE file name, not just parts of it. So if you are looking to match all files ending in .default, you'd need to use '.*\.default' as a regexp and not just '\.default'. This parameter expects a list, which can be either comma separated or YAML. If any of the patterns contain a comma, make sure to put them in a list to avoid splitting the patterns in undesirable ways. Defaults to '*' when use_regex=False, or '.*' when use_regex=True.
use_regex
A boolean value (yes
, no
), default no
. If no
, use shell globs. Otherwise use python regular expressions.
recurse
If target is a directory, recursively descend into the directory looking for files. Values: yes
, no
. Default is no
.
file_type
A string. Can be:
- "any"
- "directory"
- "file"
- "link"
The default value is "file".
depth
An integer that specifies the maximum number of levels to descend into. Setting recurse to no will override this value, which is effectively depth 1. Default is unlimited depth.
Examples
Find Files that Match a Glob and Pass them as a List
- name: Find files that match a glob and pass them as a List
find:
paths: '/some/dir'
patterns: '*.txt'
register: find_result
- name: debug
debug:
msg: "{{ item }}"
with_items: "{{ find_result.files | json_query('[*].path') }}"
Find Files with Shell Globs
Shell glob example:
- name: Collect Java module descriptor files from Java Home
find:
paths: '/Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home/jmods'
patterns: '*.jmod'
file_type: file
Find a File
- name: Find JetBrains applications
find:
paths: "{{ ansible_env.HOME }}/Library/Application Support/JetBrains/Toolbox/apps"
recurse: yes
patterns: '.history.json'
hidden: yes
file_type: file
depth: 5
register: intellij_apps