Given the files for testing
shell> ls -ogla /tmp/test/
total 28
drwxrwxr-x 2 4096 Nov 25 21:23 .
drwxrwxrwt 70 20480 Nov 25 22:28 ..
-rw-rw-r-- 1 0 Jul 25 00:00 file1
-rw-rw-r-- 1 0 Aug 25 00:00 file2
-rw-rw-r-- 1 0 Sep 25 00:00 file3
-rw-rw-r-- 1 0 Oct 25 00:00 file4
-rw-rw-r-- 1 0 Nov 25 00:00 file5
- Find lists of files older than the dates and create the difference between these lists.
In addition to the declarations
begin_date: "{{ lookup('pipe', 'date -d \"2 months ago\" -I') }}"
end_date: "{{ lookup('pipe', 'date -d \"1 months ago\" -I') }}"
Declare the variables
today: "{{ '%Y-%m-%d'|strftime }}"
begin_days: "{{ ((today|to_datetime('%Y-%m-%d')) -
(begin_date|to_datetime('%Y-%m-%d'))).days }}"
end_days: "{{ ((today|to_datetime('%Y-%m-%d')) -
(end_date|to_datetime('%Y-%m-%d'))).days }}"
gives the date of today and the number of days from begin_date and end_date
begin_date: 2022-09-25
end_date: 2022-10-25
today: 2022-11-25
begin_days: 61
end_days: 31
Find and register files where the modification time is older than begin_date and end_date
- find:
path: /tmp/test
age: "{{ begin_days }}d"
register: begin
- find:
path: /tmp/test
age: "{{ end_days }}d"
register: end
Declare the variables
begin_files: "{{ begin.files|map(attribute='path')|list }}"
end_files: "{{ end.files|map(attribute='path')|list }}"
my_files: "{{ end_files|difference(begin_files) }}"
gives the lists of files where the modification time is older than begin_date and end_date. The difference between these lists my_files is what you're looking for
begin_files: ['/tmp/test/file3', '/tmp/test/file2', '/tmp/test/file1']
end_files: ['/tmp/test/file3', '/tmp/test/file4', '/tmp/test/file2', '/tmp/test/file1']
my_files: ['/tmp/test/file4']
Example of a complete playbook for testing
- hosts: localhost
vars:
today: "{{ '%Y-%m-%d'|strftime }}"
begin_date: "{{ lookup('pipe', 'date -d \"2 months ago\" -I') }}"
end_date: "{{ lookup('pipe', 'date -d \"1 months ago\" -I') }}"
begin_days: "{{ ((today|to_datetime('%Y-%m-%d')) -
(begin_date|to_datetime('%Y-%m-%d'))).days }}"
end_days: "{{ ((today|to_datetime('%Y-%m-%d')) -
(end_date|to_datetime('%Y-%m-%d'))).days }}"
begin_files: "{{ begin.files|map(attribute='path')|list }}"
end_files: "{{ end.files|map(attribute='path')|list }}"
my_files: "{{ end_files|difference(begin_files) }}"
tasks:
- debug:
msg: |
today: {{ today }}
begin_date: {{ begin_date }}
end_date: {{ end_date }}
begin_days: {{ begin_days }}
end_days: {{ end_days }}
- find:
path: /tmp/test
age: "{{ begin_days }}d"
register: begin
- find:
path: /tmp/test
age: "{{ end_days }}d"
register: end
- debug:
msg: |
begin_files: {{ begin_files }}
end_files: {{ end_files }}
my_files: {{ my_files }}
- Collect the status of the files and select files by time.
Change the format of the dates to seconds
begin_date_cmd: "date -d '2 months ago' '+%s'"
begin_date: "{{ lookup('pipe', begin_date_cmd) }}"
end_date_cmd: "date -d '1 months ago' '+%s'"
end_date: "{{ lookup('pipe', end_date_cmd) }}"
gives
begin_date: 1664143922
end_date: 1666735922
Declare the variable
files_all: "{{ all.files|map(attribute='path')|list }}"
and get the list of all files
- find:
path: /tmp/test
register: all
Collect the status of all files
- stat:
path: "{{ item }}"
register: st
loop: "{{ files_all }}"
Declare the list where the modification time is bigger than begin_date and less than end_date
my_files: "{{ st.results|selectattr('stat.mtime', 'gt', begin_date|float)|
selectattr('stat.mtime', 'lt', end_date|float)|
map(attribute='item')|list }}"
gives
my_files:
- /tmp/test/file4
Example of a complete playbook for testing
- hosts: localhost
vars:
begin_date_cmd: "date -d '2 months ago' '+%s'"
begin_date: "{{ lookup('pipe', begin_date_cmd) }}"
end_date_cmd: "date -d '1 months ago' '+%s'"
end_date: "{{ lookup('pipe', end_date_cmd) }}"
files_all: "{{ all.files|map(attribute='path')|list }}"
my_files: "{{ st.results|selectattr('stat.mtime', 'gt', begin_date|float)|
selectattr('stat.mtime', 'lt', end_date|float)|
map(attribute='item')|list }}"
tasks:
- debug:
msg: |
begin_date: {{ begin_date }}
end_date: {{ end_date }}
- find:
path: /tmp/test
register: all
- debug:
var: files_all
- stat:
path: "{{ item }}"
register: st
loop: "{{ files_all }}"
- debug:
var: st
- debug:
var: my_files