Automating File Extension Validation with Ansible

How to use the regular expression within an Ansible Playbook.

Luca Berton

--

Introduction

In the age of automation, managing file integrity across diverse environments is crucial for ensuring data consistency and security. Ansible, a powerful IT automation tool, offers an efficient way to automate tasks and ensure that files meet specified criteria, such as having correct file extensions. This article presents a detailed guide on using Ansible to validate file extensions within a system.

Understanding the Playbook Structure

The given Ansible playbook is designed to validate file extensions to ensure that each file ends with specified formats (e.g., .csv or .txt). Here’s a breakdown of the playbook's components:

---
- name: Validate file extensions
hosts: all
vars:
my_dicts: [
{ file_name: 'data1.csv' },
{ file_name: 'report.txt' },
{ file_name: 'summary.json' }
]

tasks:
- name: Check file extensions
assert:
that: item is match('.*\.(csv|txt)$')
fail_msg: "File with invalid extension detected: {{ item }}"
success_msg: "Valid file extension…

--

--

Luca Berton

I help creative Automation DevOps, Cloud Engineer, System Administrator, and IT Professional to succeed with Ansible Technology to automate more things everyday