Software >> Automation >> Ansible >> Modules >> stat >> Examples

 

stat


## DOCUMENTATION

online help: https://docs.ansible.com/ansible-core/2.11/collections/ansible/builtin/stat_module.html
offline help: ansible-doc stat


## EXAMPLE 1
## PLAYBOOK

---
- name: stat module example 1
  hosts: all
  become: True
  gather_facts: no
  tasks:
  - name: Get stats of a file
    stat:
       path: /home/ansible/hello.txt
    register: filestat
  - debug:
      msg:
        - "path={{filestat.stat.path}}"
        - "mtime={{filestat.stat.mtime}}"
        - "atime={{filestat.stat.atime}}"
  - name: Calculate checksum with sha256 algorithm
    stat:
      path: /home/ansible/hello.txt
      checksum_algorithm: sha256
    register: filestat
  - debug: var=filestat.stat.checksum
  - name: Display all values in file stat
    debug: var=filestat.stat



## OUTPUT

PLAY [stat module example 1] ***********************************************************************

TASK [Get stats of a file] *************************************************************************
ok: [ubuntu20_int]
ok: [debian11_int]

TASK [debug] ***************************************************************************************
ok: [debian11_int] => {
    "msg": [
        "path=/home/ansible/hello.txt",
        "mtime=1635299447.3124452",
        "atime=1635299490.8528533"
    ]
}
ok: [ubuntu20_int] => {
    "msg": [
        "path=/home/ansible/hello.txt",
        "mtime=1635299449.8489878",
        "atime=1635299492.513668"
    ]
}

TASK [Calculate checksum with sha256 algorithm] ****************************************************
ok: [ubuntu20_int]
ok: [debian11_int]

TASK [debug] ***************************************************************************************
ok: [debian11_int] => {
    "filestat.stat.checksum": "66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18"
}
ok: [ubuntu20_int] => {
    "filestat.stat.checksum": "66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18"
}

TASK [Display all values in file stat] *************************************************************
ok: [debian11_int] => {
    "filestat.stat": {
        "atime": 1635299490.8528533,
        "attr_flags": "e",
        "attributes": [
            "extents"
        ],
        "block_size": 4096,
        "blocks": 8,
        "charset": "us-ascii",
        "checksum": "66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18",
        "ctime": 1635299450.4324746,
        "dev": 65024,
        "device_type": 0,
        "executable": true,
        "exists": true,
        "gid": 1001,
        "gr_name": "ansible",
        "inode": 1189297,
        "isblk": false,
        "ischr": false,
        "isdir": false,
        "isfifo": false,
        "isgid": false,
        "islnk": false,
        "isreg": true,
        "issock": false,
        "isuid": false,
        "mimetype": "text/plain",
        "mode": "0755",
        "mtime": 1635299447.3124452,
        "nlink": 1,
        "path": "/home/ansible/hello.txt",
        "pw_name": "ansible",
        "readable": true,
        "rgrp": true,
        "roth": true,
        "rusr": true,
        "size": 6,
        "uid": 1001,
        "version": "2092334248",
        "wgrp": false,
        "woth": false,
        "writeable": true,
        "wusr": true,
        "xgrp": true,
        "xoth": true,
        "xusr": true
    }
}
ok: [ubuntu20_int] => {
    "filestat.stat": {
        "atime": 1635299492.513668,
        "attr_flags": "e",
        "attributes": [
            "extents"
        ],
        "block_size": 4096,
        "blocks": 8,
        "charset": "us-ascii",
        "checksum": "66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18",
        "ctime": 1635299450.2569947,
        "dev": 2053,
        "device_type": 0,
        "executable": true,
        "exists": true,
        "gid": 1002,
        "gr_name": "ansible",
        "inode": 816371,
        "isblk": false,
        "ischr": false,
        "isdir": false,
        "isfifo": false,
        "isgid": false,
        "islnk": false,
        "isreg": true,
        "issock": false,
        "isuid": false,
        "mimetype": "text/plain",
        "mode": "0755",
        "mtime": 1635299449.8489878,
        "nlink": 1,
        "path": "/home/ansible/hello.txt",
        "pw_name": "ansible",
        "readable": true,
        "rgrp": true,
        "roth": true,
        "rusr": true,
        "size": 6,
        "uid": 1002,
        "version": "4147267447",
        "wgrp": false,
        "woth": false,
        "writeable": true,
        "wusr": true,
        "xgrp": true,
        "xoth": true,
        "xusr": true
    }
}

PLAY RECAP *****************************************************************************************
debian11_int               : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ubuntu20_int               : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0