Ansible exclude Folders with Find module Recursively - ansible

As per my requirement I want to exclude the few folders recursively by using the find module. I tried the below method to exclude the folders, but unfortunately its excludes only in the base path not sub folders.
- name: Find all files
find:
path: "{{ src_path }}"
patterns: "*.ksh,*.src,*.par,*.ctl"
file_type: file
recurse: yes
excludes: ["log","dat","archive","backup","history","outfiles","_hist","tmp","data","bak"]
register: list_of_files

Related

Ansible find module collecting files in hidden directories

A couple of tasks in my playbook keep finding and modifying files in hidden directories--I do not want the hidden files to be altered.
ansible-core 2.13.7
- name: Find all script files
find:
recurse: yes
paths: /var/bbb
patterns:
- "*.sh"
- "*.py"
- "*.env"
register: script_files
- name: Make all scripts executable
file:
dest: "{{ item.path }}"
mode: "a+rx"
with_items: "{{ script_files.files }}"
Sample of output from second task:
ok: [localhost] => (item={'path': '/var/bbb/.ansible/collections/ansible_collections/community/docker/tests/unit/plugins/module_utils/test_util.py', 'mode': '0755', ...
I've gone through the docs and they say hidden files are left alone by default in the current and former versions. I suspect it may be the patterns, but again the docs say the pattern is compared to the file base name and excludes the directory.
Regarding your observation
Ansible find module collecting files in hidden directories
that's right and the expected behavior.
... they say hidden files are left alone by default in the current and former versions.
I interpret the documentation about Parameter: hidden that it applies to files only and not to directories.
A minimal test setup
mdkir .hidden
touch .hidden/script.sh
touch .hidden/.script.sh
with an example playbook
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: Find all script files
find:
hidden: false
recurse: true
paths: "/home/{{ ansible_user }}/"
patterns:
- "*script.sh"
register: result
- name: Show result
debug:
msg: "{{ result.files }}"
will result into an output of
...
path: /home/user/.hidden/script.sh
...
or if hidden: true
...
path: /home/user/.hidden/script.sh
...
path: /home/user/.hidden/.script.sh
...
Similar Q&A
Using Ansible find module to get hidden folders
To summarize, your question seems to be about
How to exclude hidden directories from Ansible find module recursive search?
Since with the parameter hidden: false and file_type: any still the /.hidden/script.sh is found, the excludes parameter operates on basenames of files, I recommend to adjust the search path.
Given the useful explanation in the previous answer and the complexity of the search path solution in my use case, I looked beyond the parameters of find and developed a separate task that removes results that contain hidden directories:
- name: filter hidden directories
set_fact:
filtered_list: "{{result.files | map(attribute='path') | reject('search', '/\\.')}}"
Be aware that this will remove all paths which contain a hidden directory: it should not be used when the find task is performed on paths that contain a hidden directory.

Need assistance in ansible playbook for compressing files and excluding files based on certain condition

I have created an ansible playbook which finds files older than 7 days ,trying to exclude
files from particular directory or already zipped but not able to exclude.
tasks:
- name : Find files ending with extensions
become: true
find:
path:
- /home/test/replicate/vol/gunnsc01/prod/scoreout
recurse: yes
file_type: any
age: 7d
age_stamp: mtime
excludes: "delete"
register: output
- name: archive the files
become: yes
archive:
remove: yes
path: "{{ item.path }}"
dest: "{{ item.path }}-{{ansible_date_time.date.replace('-','')}}.gz"
format: zip
with_items: "{{ output.files }}"
I am not able to exclude specific directory ,I have tried exclude,excludes,exclude_path,exclude_paths and exclude_patterns
however I am not able to exclude the files under the directory delete

Ansible unarchive exclude syntax

I'm trying to specify part of an archive not to extract via Ansible's unarchive module using the exclude option.
I believe the syntax should be roughly as shown here...
- name: Extract files from discovered zip file
unarchive:
src: "{{ base_path }}/weblogic-deployment/environments/{{ client_environment }}/discovered_domain.zip"
dest: "{{ base_path }}/weblogic-deployment/environments/{{ client_environment }}/tmp"
exclude:
- ./wlsdeploy/applications/
remote_src: yes
I have tried a great many slight differences but the excluded directory is always output. Any suggestions?
The exclude option is expected to be a list of paths to exclude. So most likely it doesn't support directories alone.
List the directory and file entries that you would like to exclude from the unarchive action.
So try something like:
exclude:
- ./wlsdeploy/applications/*
For some syntax examples, see: unarchive/tasks/main.yml. Also check the source code.
To exclude the folder entirely:
exclude:
- "wlsdeploy/applications"
To extract the folder, but exclude the contents, you can use a glob:
exclude:
- "wlsdeploy/applications/*"
As the paths are relative to the archive, you don't need the preceding ./.
If you need to look inside the archive first (to find the paths), you can use less:
less discovered_domain.zip
From the unarchive tests for exclude:
- name: Unpack archive file excluding regular and glob files.
unarchive:
src: "{{ remote_tmp_dir }}/unarchive-00.{{item}}"
dest: "{{ remote_tmp_dir }}/exclude-{{item}}"
remote_src: yes
exclude:
- "exclude/exclude-*.txt"
- "other/exclude-1.ext"
with_items:
- zip
- tar

Ansible playbook to find out specific files in sub directories

I have directory structure as below, wants to fetch the specific sub directory files using ansible rather than all.
/mnt/server1 ->
----> yyy.deb
----> /mnt/server1/All/tttsss.deb
----> /mnt/server1/HS-CLONE/gggg.deb
----> /mnt/server1/HS-TEST/kkkk.deb
I need to find only files present under /mnt/server1/All/tttsss.deb and /mnt/server1/HS-CLONE/gggg.deb directories. I don't require all other files.
When i trying using below logic, the parent directory file yyy.deb is also coming as output.
- name: Ansible find files in subdirectory examples
find:
paths: /mnt/server1
file_type: file
recurse: yes
use_regex: yes
patterns:
- 'All'
- "HS-CLONE"
- '.*deb$'
register: files_matched_subdirectory
With the above logic output as:
Output:
yyy.deb
/mnt/server1/All/tttsss.deb
/mnt/server1/HS-CLONE/gggg.deb
Expected output should be:
/mnt/server1/All/tttsss.deb
/mnt/server1/HS-CLONE/gggg.deb
This is happening because you have below specific pattern in your find command which will match yyy.deb in the directory /mnt/server1
patterns:
- '.*deb$'
you can use excludes parameter to explicitly exclude this particular file :
- name: Ansible find files in subdirectory examples
find:
paths: /mnt/server1
file_type: file
recurse: yes
use_regex: yes
patterns:
- 'All'
- "HS-CLONE"
- '.*deb$'
excludes:
- 'yyy.deb'
register: files_matched_subdirectory
Or may be you can try something like below in your pattern parameter :
patterns:
- 'All'
- "HS-CLONE"
- '*/.*deb$'

Ansible: remove files and folders while excluding some

In my Ansible Playbook I'd like to have a task that removes old files and folders from the application's directory. The twist to this otherwise simple task is that a few files or folders need to remain. Imagine something like this:
/opt/application
- /config
- *.properties
- special.yml
- /logs
- /bin
- /var
- /data
- /templates
Let's assume I'd like to keep /logs completely, /var/data and from /config I want to keep special.yml.
(I cannot provide exact code at the moment because I left work frustrated by this and, after cooling down, I am now writing up this question at home)
My idea was to have two lists of exclusions, one holding the folders and one the file. Then I use the find module to first get the folders in the application's directory into a variable and the same for the remaining files into another variable. Afterwards I wanted to remove every folder and file that are not in the lists of exclusions using the file module.
(Pseudo-YML because I'm not yet fluent enough in Ansible that I can whip up a properly structured example; it should be close enough though)
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ found_files_list.files }}"
when: well, that is the big question
What I can't figure out is how to properly construct the when clause. Is it even possible like this?
I don't believe there is a when clause with the file module.
But you can probably achieve what you need as follows:
- name: Find /opt/application all directories, exclude logs, data, and config
find:
paths: /opt/application
excludes: 'logs,data,config'
register: files_to_delete
- name: Ansible remove file glob
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ files_to_delete.files }}"
I hope this is what you need.
First use the find module like you said to get a total list of all files and directories. Register to a variable like all_objects.
- name: Get list of all files recursively
find:
path: /opt/application/
recurse: yes
register: all_objects
Then manually make a list of things you want to keep.
vars:
keep_these:
- /logs
- /var/data
- /config/special.yml
Then this task should delete everything except things in your list:
- name: Delete all files and directories except exclusions
file:
path: "{{ item.path }}"
state: absent
recurse: true
with_items: "{{ all_objects.files }}"
when: item.path not in keep_these
I think this general strategy should work... only thing I'm not sure about is the exact nesting heiararchy of the registered variable from the find module. You might have to play around with the debug module to get it exactly right.

Resources