Ansible Module stat: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(7 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=
Retrieve file or file system status.
Retrieves facts for a file similar to the Linux/Unix "stat" command.


<syntaxhighlight lang='yaml'>
<syntaxhighlight lang='yaml'>
- name: "Check if a directory exists"
- name: Gather stats on the given path
   stat:
   stat:
     path: "{{ some_path }}"
     path: "{{ some_path }}"
   register: some_dir
   register: some_path_info
- name: debug
  debug:
    var: some_path_info
</syntaxhighlight>
 
The module result variable has the following structure:
<syntaxhighlight lang='json'>
{
  "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
  }
}
</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 }}"