Ansible Module stat: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 4: Line 4:
=Internal=
=Internal=
* [[Ansible_Concepts#stat|Ansible Concepts]]
* [[Ansible_Concepts#stat|Ansible Concepts]]
* <code>[[Ansible Module ansible.builtin.assert|assert]]</code>


=Overview=
=Overview=
Line 12: Line 13:
   stat:
   stat:
     path: "{{ some_path }}"
     path: "{{ some_path }}"
   register: some_path_stat
   register: some_path_info
- name: debug
- name: debug
   debug:
   debug:
     var: some_path_stat
     var: some_path_info
</syntaxhighlight>
</syntaxhighlight>


The module result variable has the following structure:
The module result variable has the following structure:
<syntaxhighlight lang=''json'>
<syntaxhighlight lang='json'>
{
{
        "changed": false,
  "changed": false,
        "failed": false,
  "failed": false,
        "stat": {
  "stat": {
            "atime": 1625423678.392114,
    "atime": 1625423678.392114,
            "attr_flags": "",
    "attr_flags": "",
            "attributes": [],
    "attributes": [],
            "birthtime": 1625352249.6358514,
    "birthtime": 1625352249.6358514,
            "block_size": 4096,
    "block_size": 4096,
            "blocks": 0,
    "blocks": 0,
            "charset": "binary",
    "charset": "binary",
            "ctime": 1625423644.281025,
    "ctime": 1625423644.281025,
            "dev": 16777220,
    "dev": 16777220,
            "device_type": 0,
    "device_type": 0,
            "executable": true,
    "executable": true,
            "exists": true,
    "exists": true,
            "flags": 0,
    "flags": 0,
            "generation": 0,
    "generation": 0,
            "gid": 20,
    "gid": 20,
            "gr_name": "staff",
    "gr_name": "staff",
            "inode": 30508999,
    "inode": 30508999,
            "isblk": false,
    "isblk": false,
            "ischr": false,
    "ischr": false,
            "isdir": true,
    "isdir": true,
            "isfifo": false,
    "isfifo": false,
            "isgid": false,
    "isgid": false,
            "islnk": false,
    "islnk": false,
            "isreg": false,
    "isreg": false,
            "issock": false,
    "issock": false,
            "isuid": false,
    "isuid": false,
            "mimetype": "inode/directory",
    "mimetype": "inode/directory",
            "mode": "0755",
    "mode": "0755",
            "mtime": 1625423644.281025,
    "mtime": 1625423644.281025,
            "nlink": 24,
    "nlink": 24,
            "path": "/Users/ovidiu/Library/Application Support/JetBrains/IntelliJIdea2021.2",
    "path": "/Users/ovidiu/Library/Application Support/JetBrains/IntelliJIdea2021.2",
            "pw_name": "ovidiu",
    "pw_name": "ovidiu",
            "readable": true,
    "readable": true,
            "rgrp": true,
    "rgrp": true,
            "roth": true,
    "roth": true,
            "rusr": true,
    "rusr": true,
            "size": 768,
    "size": 768,
            "uid": 502,
    "uid": 502,
            "version": null,
    "version": null,
            "wgrp": false,
    "wgrp": false,
            "woth": false,
    "woth": false,
            "writeable": true,
    "writeable": true,
            "wusr": true,
    "wusr": true,
            "xgrp": true,
    "xgrp": true,
            "xoth": true,
    "xoth": true,
            "xusr": true
    "xusr": true
        }
  }
     }
}
</syntaxhighlight>
The existence of the directory can be checked with:
<syntaxhighlight lang='yaml'>
- name: Ensure that some directory exist
  assert:
     that: "{{ some_path_info.stat.exists }}"
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 06:19, 5 July 2021

External

Internal

Overview

Retrieves facts for a file similar to the Linux/Unix "stat" command.

- name: Gather stats on the given path
  stat:
    path: "{{ some_path }}"
  register: some_path_info
- name: debug
  debug:
    var: some_path_info

The module result variable has the following structure:

{
  "changed": false,
  "failed": false,
  "stat": {
    "atime": 1625423678.392114,
    "attr_flags": "",
    "attributes": [],
    "birthtime": 1625352249.6358514,
    "block_size": 4096,
    "blocks": 0,
    "charset": "binary",
    "ctime": 1625423644.281025,
    "dev": 16777220,
    "device_type": 0,
    "executable": true,
    "exists": true,
    "flags": 0,
    "generation": 0,
    "gid": 20,
    "gr_name": "staff",
    "inode": 30508999,
    "isblk": false,
    "ischr": false,
    "isdir": true,
    "isfifo": false,
    "isgid": false,
    "islnk": false,
    "isreg": false,
    "issock": false,
    "isuid": false,
    "mimetype": "inode/directory",
    "mode": "0755",
    "mtime": 1625423644.281025,
    "nlink": 24,
    "path": "/Users/ovidiu/Library/Application Support/JetBrains/IntelliJIdea2021.2",
    "pw_name": "ovidiu",
    "readable": true,
    "rgrp": true,
    "roth": true,
    "rusr": true,
    "size": 768,
    "uid": 502,
    "version": null,
    "wgrp": false,
    "woth": false,
    "writeable": true,
    "wusr": true,
    "xgrp": true,
    "xoth": true,
    "xusr": true
  }
}

The existence of the directory can be checked with:

- name: Ensure that some directory exist
  assert:
    that: "{{ some_path_info.stat.exists }}"