Ansible Pilot

Check if a directory exists on Windows-like systems - Ansible module win_stat

How to automate the checking of the "example" directory/folder in the Desktop of the user on the Windows system and display a message or execute a task accordingly with Ansible Playbook.

How to check if a directory/folder exists on Windows-like systems with Ansible?

I’m going to show you a live demo and some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot

Ansible check directory exists on Windows-like systems

Let’s talk about the Ansible module win_stat. The full name is ansible.windows.win_stat, which means that is part of the collection of modules specialized to interact with Windows target host. It’s a module pretty stable and out for years. It works in Windows and Windows Server operating systems. It gets information about Windows files. For Linux target use the ansible.builtin.stat module instead.

Parameters & Return Values

Mandatory Parameters

Main Return Values

The only mandatory parameter is “path” which is the filesystem full path of the object to check. The module returns a complex object, the property that is interesting for us is “isdir”. This attribute is “true” if the object is a directory

demo

How to check if the “example” directory/folder exists on the Desktop of the user on Windows-like systems with Ansible Playbook.

code

---
- name: check if is a directory
  hosts: all
  vars:
    directory: 'C:\Users\vagrant\Desktop\example'
  tasks:
    - name: Check the directory
      ansible.windows.win_stat:
        path: "{{ directory }}"
      register: dir_data
    - name: Directory found
      ansible.builtin.debug:
        msg: "Directory {{ directory }} present"
      when: dir_data.stat.isdir is defined and dir_data.stat.isdir

directory doesn’t exist execution

win_stat directory doesn’t exist

ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory check\ directory\ exists/directory_exists_windows.yml
PLAY [check if is a directory] ********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [Check the directory] ************************************************************************
ok: [WindowsServer]
TASK [Directory found] ****************************************************************************
skipping: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer              : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0
ansible-pilot $

directory exist execution

win_stat directory exist

ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory check\ directory\ exists/directory_exists_windows.yml
PLAY [check if is a directory] ********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [Check the directory] ************************************************************************
ok: [WindowsServer]
TASK [Directory found] ****************************************************************************
ok: [WindowsServer] => {
    "msg": "Directory C:\\Users\\vagrant\\Desktop\\example present"
}
PLAY RECAP ****************************************************************************************
WindowsServer              : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

code with ❤️ in GitHub

Recap

Now you know how to check if a directory exists on Windows-like systems with Ansible. Subscribe to the YouTube channel, Medium, Website and Twitter to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 100+ Automation Examples For Linux and Windows System Administrator and DevOps

Want to keep this project going? Please donate

Trustpilot
Follow me

Subscribe not to miss any new releases

March 13, 2022

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress