Bootstrap FreeKB - Ansible - Determine if a directory is empty
Ansible - Determine if a directory is empty

Updated:   |  Ansible articles

The find module with the file_type: all and hidden: true options can be used to determine if there are any files or directories below a target directory (the /tmp/foo directory) in this example. The register parameter is used to store the results in a variable named "out" and the debug module is used to print the content of the "out" variable. 

---
- hosts: localhost
  tasks:
  - find:
      paths: /tmp/foo
      file_type: all
      hidden: true
    register: out

  - debug:
      var: out
...

 

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

ok: [localhost] => {
    "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 is empty, something like this should be returned.

ok: [localhost] => {
    "msg": {
        "changed": false,
        "examined": 0,
        "failed": false,
        "files": [],
        "matched": 0,
        "msg": ""
    }
}

 

Notice in this example that "examined" and "matched" are 1 because there is one file or directory below the /tmp/foo directory.

ok: [localhost] => {
    "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/bar.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": ""
    }
}

 

The fail module can now be used to do something if the /tmp/foo directory is empty (or is not empty).

---
- hosts: localhost
  tasks:
  - find:
      paths: /tmp/foo
      file_type: all
      hidden: true
    register: out

  - fail:
      msg: the /tmp/foo directory is not empty
    when: out.matched != 0
...

 




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 33459b in the box below so that we can be sure you are a human.