Ansible Module find: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 8: Line 8:
=Overview=
=Overview=
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.
=Examples=
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'
    use_regex: no
    recurse: no
    file_type: file
  register: java_module_files
</syntaxhighlight>
<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>


=Module Arguments=
=Module Arguments=

Revision as of 04:49, 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

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.