FreeKB - Ansible stat module (file statistics)
Ansible - stat module (file statistics)

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".

- name: store the stats of example.txt in the 'out' variable
  stat:
    path: /path/to/example.txt
  register: out

 

The debug module can be used to output the raw stats.

- name: output the contents of the 'out' variable
  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
        }
    }
}

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |