Bootstrap FreeKB - Ansible - Determine if a file or directory exists using the stat module
Ansible - Determine if a file or directory exists using the stat module

Updated:   |  Ansible articles

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

The stat module will get statics for a file on a managed node (e.g. the target system). In this example, the stat module reads the example.txt file, and the register module is used to store the results in a variable named "out". The debug module can be used to output the raw stats.

---
- hosts: localhost
  tasks:
  - name: store the stats of example.txt in the 'out' variable
    ansible.builtin.stat:
      path: /path/to/example.txt
    register: out

  - name: output the contents of the 'out' variable
    ansible.builtin.debug: 
      var: out
...

 

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

TASK [output the contents of the 'out' variable]
ok: [server1.example.com] => {
    "msg": {
        "changed": false,
        "failed": false,
        "stat": {
            "exists": false
        }
    }
}

 

If the file exists, something like this should be returned. Refer to Ansible - Getting Started parsing JSON and Ansible - Looping through JSON array if you want to access specific values in the JSON.

TASK [output the contents of the 'out' variable]
ok: [server1.example.com] => {
    "msg": {
        "changed": false,
        "failed": false,
        "stat": {
            "atime": 1597271585.646168,
            "attr_flags": "",
            "attributes": [],
            "block_size": 4096,
            "blocks": 8,
            "charset": "us-ascii",
            "checksum": "648a6a6ffffdaa0badb23b8baf90b6168dd16b3a",
            "ctime": 1597271585.646168,
            "dev": 64768,
            "device_type": 0,
            "executable": false,
            "exists": true,
            "gid": 10,
            "gr_name": "wheel",
            "inode": 26964623,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "md5": "e59ff97941044f85df5297e1c302d260",
            "mimetype": "text/plain",
            "mode": "0644",
            "mtime": 1597271585.646168,
            "nlink": 1,
            "path": "/tmp/example.txt",
            "pw_name": "root",
            "readable": true,
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 12,
            "uid": 1000,
            "version": "891153635",
            "wgrp": false,
            "woth": false,
            "writeable": true,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        }
    }
}

 

Here is how you can do something base on whether or not a file exist.

---
- hosts: all
  tasks:
  - name: store the stats of example.txt in the 'out' variable
    ansible.builtin.stat:
      path: /path/to/example.txt
    register: out

  - name: output the contents of the 'out' variable
    ansible.builtin.debug: 
      var: out

  - name: fail if /path/to/example.txt does not exist
    ansible.builtin.fail:
      msg: /path/to/example.txt does not exist
    when: out.stat.exists == false
...

 




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