Ansible Module find: Difference between revisions
Line 10: | Line 10: | ||
=Examples= | =Examples= | ||
Shell | Shell [[Bash Command Line Expansion#Glob|glob]] example: | ||
<syntaxhighlight lang='yaml'> | |||
- 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 | |||
</syntaxhighlight> | |||
<syntaxhighlight lang='yaml'> | <syntaxhighlight lang='yaml'> | ||
- name: Find JetBrains applications | - name: Find JetBrains applications |
Revision as of 09:31, 4 July 2021
External
Internal
Overview
Return a list of files based on specific criteria. Multiple criteria are ANDed together.
Examples
Shell glob example:
- 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
- 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
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
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.