ansible.windows.win_stat (2.3.0) — module

Get information about Windows files

Authors: Chris Church (@cchurch)

Install collection

Install with ansible-galaxy collection install ansible.windows:==2.3.0


Add to requirements.yml

  collections:
    - name: ansible.windows
      version: 2.3.0

Description

Returns information about a Windows file.

For non-Windows targets, use the M(ansible.builtin.stat) module instead.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Obtain information about a file
  ansible.windows.win_stat:
    path: C:\foo.ini
  register: file_info
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Obtain information about a folder
  ansible.windows.win_stat:
    path: C:\bar
  register: folder_info
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Get MD5 checksum of a file
  ansible.windows.win_stat:
    path: C:\foo.ini
    get_checksum: true
    checksum_algorithm: md5
  register: md5_checksum
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- debug:
    var: md5_checksum.stat.checksum
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Get SHA1 checksum of file
  ansible.windows.win_stat:
    path: C:\foo.ini
    get_checksum: true
  register: sha1_checksum
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- debug:
    var: sha1_checksum.stat.checksum
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Get SHA256 checksum of file
  ansible.windows.win_stat:
    path: C:\foo.ini
    get_checksum: true
    checksum_algorithm: sha256
  register: sha256_checksum
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- debug:
    var: sha256_checksum.stat.checksum

Inputs

    
path:
    aliases:
    - dest
    - name
    description:
    - The full path of the file/object to get the facts of; both forward and back slashes
      are accepted.
    required: true
    type: path

follow:
    default: false
    description:
    - Whether to follow symlinks or junction points.
    - In the case of C(path) pointing to another link, then that will be followed until
      no more links are found.
    type: bool

get_size:
    default: true
    description:
    - Whether to return the size of a file or directory.
    type: bool
    version_added: 1.11.0
    version_added_collection: ansible.windows

get_checksum:
    default: true
    description:
    - Whether to return a checksum of the file (default sha1)
    type: bool

checksum_algorithm:
    choices:
    - md5
    - sha1
    - sha256
    - sha384
    - sha512
    default: sha1
    description:
    - Algorithm to determine checksum of file.
    - Will throw an error if the host is unable to use specified algorithm.
    type: str

Outputs

changed:
  description: Whether anything was changed
  returned: always
  sample: true
  type: bool
stat:
  contains:
    attributes:
      description: Attributes of the file at path in raw form.
      returned: success, path exists
      sample: Archive, Hidden
      type: str
    checksum:
      description: The checksum of a file based on checksum_algorithm specified.
      returned: success, path exist, path is a file, get_checksum == True checksum_algorithm
        specified is supported
      sample: 09cb79e8fc7453c84a07f644e441fd81623b7f98
      type: str
    creationtime:
      description: The create time of the file represented in seconds since epoch.
      returned: success, path exists
      sample: 1477984205.15
      type: float
    exists:
      description: If the path exists or not.
      returned: success
      sample: true
      type: bool
    extension:
      description: The extension of the file at path.
      returned: success, path exists, path is a file
      sample: .ps1
      type: str
    filename:
      description: The name of the file (without path).
      returned: success, path exists, path is a file
      sample: foo.ini
      type: str
    hlnk_targets:
      description: List of other files pointing to the same file (hard links), excludes
        the current file.
      returned: success, path exists
      sample:
      - C:\temp\file.txt
      - C:\Windows\update.log
      type: list
    isarchive:
      description: If the path is ready for archiving or not.
      returned: success, path exists
      sample: true
      type: bool
    isdir:
      description: If the path is a directory or not.
      returned: success, path exists
      sample: true
      type: bool
    ishidden:
      description: If the path is hidden or not.
      returned: success, path exists
      sample: true
      type: bool
    isjunction:
      description: If the path is a junction point or not.
      returned: success, path exists
      sample: true
      type: bool
    islnk:
      description: If the path is a symbolic link or not.
      returned: success, path exists
      sample: true
      type: bool
    isreadonly:
      description: If the path is read only or not.
      returned: success, path exists
      sample: true
      type: bool
    isreg:
      description: If the path is a regular file.
      returned: success, path exists
      sample: true
      type: bool
    isshared:
      description: If the path is shared or not.
      returned: success, path exists
      sample: true
      type: bool
    lastaccesstime:
      description: The last access time of the file represented in seconds since epoch.
      returned: success, path exists
      sample: 1477984205.15
      type: float
    lastwritetime:
      description: The last modification time of the file represented in seconds since
        epoch.
      returned: success, path exists
      sample: 1477984205.15
      type: float
    lnk_source:
      description: Target of the symlink normalized for the remote filesystem.
      returned: success, path exists and the path is a symbolic link or junction point
      sample: C:\temp\link
      type: str
    lnk_target:
      description: Target of the symlink. Note that relative paths remain relative.
      returned: success, path exists and the path is a symbolic link or junction point
      sample: ..\link
      type: str
    nlink:
      description: Number of links to the file (hard links).
      returned: success, path exists
      sample: 1
      type: int
    owner:
      description: The owner of the file.
      returned: success, path exists
      sample: BUILTIN\Administrators
      type: str
    path:
      description: The full absolute path to the file.
      returned: success, path exists, file exists
      sample: C:\foo.ini
      type: str
    sharename:
      description: The name of share if folder is shared.
      returned: success, path exists, file is a directory and isshared == True
      sample: file-share
      type: str
    size:
      description: The size in bytes of a file or folder.
      returned: success, path exists, file is not a link, get_size == True
      sample: 1024
      type: int
  description: dictionary containing all the stat data
  returned: success
  type: complex

See also