% ls -1lR
.:
total 20
-rwxr-xr-x 1 root root 482 Feb 18 14:05 a.sh
-rw-r--r-- 1 root root 37 May 23 2022 ansible.cfg
-rw-r--r-- 1 root root 161 Feb 18 14:05 copy_latest_file.yml
drwxr-xr-x 2 root root 4096 Feb 18 14:06 db_dir
-rw-r--r-- 1 root root 184 Nov 8 21:17 hosts

./db_dir:
total 20
-rw-r--r-- 1 root root 29 Feb 18 13:48 0.txt
-rw-r--r-- 1 root root 29 Feb 18 14:06 00.txt
-rw-r--r-- 1 root root 29 Feb 18 13:48 1.txt
-rw-r--r-- 1 root root 29 Feb 18 13:48 2.txt
-rw-r--r-- 1 root root 29 Feb 18 13:48 3.txt

=============

% cat a.sh
#!/bin/bash

Находим самый свежий файл в директории db_dir

latest_file=$(ls -t db_dir/* | head -n 1)

Проверяем, найден ли файл

if [[ -z "$latest_file" ]]; then
echo "Нет файлов для копирования."
exit 1
fi

echo "Самый свежий файл: $latest_file"

Запускаем Ansible плейбук

ansible-playbook -v -i hosts --extra-vars "latest_file=$latest_file" copy_latest_file.yml

==============

% cat copy_latest_file.yml

  • hosts: all

tasks:

  • name: Копирование самого свежего файла

copy:
src: "{{ latest_file }}"
dest: /tmp/

[file-name 000024_2025-02-18_14-09-44.txt]