--- - name: Check if backup directory exists stat: path: '{{ backup_path }}' register: backup_dir_stat - name: Check if backup directory is not empty find: paths: '{{ backup_path }}' file_type: any recurse: false when: backup_dir_stat.stat.exists and backup_dir_stat.stat.isdir register: backup_dir_contents - name: Check if each required file exists stat: path: '{{ dir_path }}/{{ item }}' loop: '{{ backup_required_files }}' register: required_file_stats - name: Determine missing files set_fact: missing_files: >- {{ required_file_stats.results | selectattr('stat.exists', 'equalto', false) | map(attribute='item') | list }} - name: Set fact if all required files are present set_fact: all_required_files_present: true when: missing_files | length == 0 - name: Debug - Show status debug: msg: >- {% if all_required_files_present | default(false) %} All required files are present. {% else %} Missing required files: {{ missing_files }} {% endif %} - name: Set fact if backup directory exists and is not empty set_fact: backup_dir_valid: true when: - backup_dir_stat.stat.exists - backup_dir_stat.stat.isdir - backup_dir_contents.matched | int > 0 - all_required_files_present - name: Debug - Show final result debug: msg: 'Backup directory exists and is not empty.' when: backup_dir_valid | default(false)