Bootstrap FreeKB - Ansible - List files and directories using the find module
Ansible - List files and directories using the find module

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

With the basic usage, the find module will find files (but not directories) below a target directory. In this example, every file in the /tmp/foo directory will be stored in the "out" variable. The /tmp/foo directory will not be included in the find results. The register parameter and debug module are then used to display the contents of the "out" variable. By default, only non-hidden files in the target directory will be returned, and the search is not recursive, meaning only the files and directories in the /tmp/foo directory will be include in the "out" variable.

---
- hosts: all
  tasks:
  - name: store the files in the /tmp/foo directory in the 'out' variable
    ansible.builtin.find:
      paths: /tmp/foo
    register: out

  - debug:
      var: out
...

 

If the directory does not exist, something like this should be returned.

TASK [debug] 
ok: [server1.example.com] => {
    "msg": {
        "changed": false, 
        "examined": 0, 
        "failed": false, 
        "files": [], 
        "matched": 0, 
        "msg": "/tmp/foo was skipped as it does not seem to be a valid directory or it cannot be accessed\n"
    }
}

 

If the directory exists but contains no files (/tmp/foo in this example), the following should be returned.

ok: [server1.example.com] => {
    "msg": {
        "changed": false,
        "examined": 0,
        "failed": false,
        "files": [],
        "matched": 0,
        "msg": ""
    }
}

 

If the directory exists and contains one or more files, something like this should be returned. Notice the accessed datetime (atime), changed datetime (ctime), and modified datetime (mtime) of the files are epoch formated, such as 1583384414.8106842. The strftime (string format time) filter can be used to convert epoch to human readable.

ok: [server1.example.com] => {
    "msg": {
        "changed": false,
        "examined": 1,
        "failed": false,
        "files": [
            {
              "atime": 1583384414.8106842,
              "mtime": 1583388631.5600421,
              "dev": 64768,
              "gid": 0,
              "inode": 131881,
              "isblk": false,
              "ischr": false,
              "isdir": false,
              "isfifo": false,
              "isgid": false,
              "islnk": false,
              "isreg": true,
              "issock": false,
              "isuid": false,
              "mode": "0664",
              "mtime": 158338361.5600421,
              "nlink": 1,
              "path": "/tmp/foo.txt",
              "rgrp": true,
              "roth": true,
              "rusr": true,
              "size": 8,
              "uid": 0,
              "wgrp": false,
              "woth": false,
              "wusr": true,
              "xgrp": false,
              "xoth": false,
              "xusr": false
           }
        ],
        "matched": 1,
        "msg": ""
    }
}

 

Since the JSON that gets returned will contain the results list, you will almost always do something like this to loop through the JSON list .

---
- hosts: all
  tasks:
  - ansible.builtin.find:
      paths: /tmp
      recurse: true
      use_regex: true
      patterns: (?i).*foo
    register: out

  - ansible.builtin.debug:
      msg: "{{ item.path }}"
    with_items: "{{ out.files }}"
    when: out.matched > 0
    loop_control:
      label: "{{ item.path }}"
...

 

file_type: directory can be used if you want to only search for directories.

---
- hosts: all
  tasks:
  - ansible.builtin.find:
      paths: /tmp
      file_type: directory
...

 

file_type: any can be used if you want to search for both files directories.

---
- hosts: all
  tasks:
  - ansible.builtin.find:
      paths: /tmp
      file_type: any
...

 

hidden:true can be used if you want to include hidden files/directories.

---
- hosts: all
  tasks:
  - ansible.builtin.find:
      paths: /tmp
      hidden: true
...

 

recurse:true can be used to search for files or directories at and below a specified directory.

---
- hosts: all
  tasks:
  - ansible.builtin.find:
      paths: /tmp
      recurse: true
...

 

use_regex: true and patterns can be used to match files/directories that match a regular expression. I almost always include (?i) for a case insensitive search.

---
- hosts: all
  tasks:
  - ansible.builtin.find:
      paths: /tmp
      patterns: (?i).*txt
      use_regex: true
...

 

Multiple paths and patterns can be searched.

---
- hosts: all
  tasks:
  - ansible.builtin.find:
      paths: /tmp,/home/john.doe
      patterns: *.txt,*.docx
      use_regex: true
...

 

And here is an example of how to find files that contain "foo" or "bar" below the /tmp directory.

---
- hosts: all
  tasks:
  - ansible.builtin.find:
      paths: /tmp
      patterns: .*(foo|bar).*
      use_regex: true
...

 

And here is an example of searching for directories that contains foo or bar.

---
- hosts: all
  tasks:
  - ansible.builtin.find:
      paths: /tmp
      file_type: directory
      patterns: '.*(foo|bar).*'
      use_regex: true
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter ee61b1 in the box below so that we can be sure you are a human.