Ansible Module find: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
Line 32: Line 32:
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.
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=
=Examples=
====Collect a List of Files and Expose them as a Variable====
====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:
Shell [[Bash Command Line Expansion#Glob|glob]] example:
<syntaxhighlight lang='yaml'>
<syntaxhighlight lang='yaml'>
Line 39: Line 51:
     paths: '/Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home/jmods'
     paths: '/Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home/jmods'
     patterns: '*.jmod'
     patterns: '*.jmod'
    use_regex: no
    recurse: no
     file_type: file
     file_type: file
  register: find_result
- name: debug
  debug:
    msg: "{{ find_result.files | json_query('[*].path') }}"
</syntaxhighlight>
</syntaxhighlight>
====Find a File====
====Find a File====
<syntaxhighlight lang='yaml'>
<syntaxhighlight lang='yaml'>

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