TECHIES WORLD

For Techs.... Techniques.... Technologies....

CpanelLinux

How to list files/folders in a directory using Ansible.

Ansible find module can be used to list files/folders in a directory.

Sample code to list the files in the directory "/var/log" is given below. Here we have specified one condition that the filename should be ending with ".log". This is required only if needed.

- name: Find /var/log files ending with .log.
find:
    paths: /var/log
    file_type: file
    patterns: '*.log'

Sample code to list the folders in the directory "/var/log" is given below. Here we have specified one condition to exclude the folders with name test1 and test2. This is required only if needed.

- name: Find /var/log all directories, exclude test1 and test2
find:
    paths: /var/log
    file_type: directory
    excludes: 'test1,test2'

Please note that the functions of find module is not limited to this and we are juts exploring few commonly used options. More detailed explanations can be found at Ansible documentations.

Leave a Reply