Automating File Extension Validation with Ansible
How to use the regular expression within an Ansible Playbook.
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 for file: {{ item }}"
loop: "{{ my_dicts | map(attribute='file_name') | list }}"
loop_control:
label: "{{ item }}"
Key Components Explained
Hosts: The playbook targets
localhost
, meaning it runs on the local machine. This setup is ideal for scripts that manage local files or perform tests without affecting remote systems.Variables: The
my_dicts
variable is a list of dictionaries, each representing a file with afile_name
key. This structure is flexible and can be expanded to include more files or metadata as needed.Tasks:
Check file extensions: This task utilizes the
assert
module, which checks conditions and fails if the conditions are not met. The condition here ensures each file name matches the regex pattern'.*\.(csv|txt)$'
, confirming it ends with.csv
or.txt
.Loop: The playbook iterates over each filename extracted from
my_dicts
, applying the validation condition to each.Loop Control: The
label
control improves the readability of loop outputs by customizing how looped items are displayed.
The Best Resources For Ansible
Certifications
- Coursera Pro - Unlimited access to 7,000+ world-class courses, hands-on projects, and job-ready certificate programs—all included in your subscription
Video Course
Printed Book
-
Ansible For VMware by Examples
-
Ansible for Kubernetes by Example
-
Hands-on Ansible Automation
-
Red Hat Ansible Automation Platform
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
- Terraform By Example: A Practical Approach for Beginners to Learn Cloud Infrastructure with Terraform
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
- Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
Execution Flow and Output
When executed, the playbook performs the following steps:
- Gathering Facts: Collects the system facts from
localhost
. - Check File Extensions: Validates each file’s extension. If a file does not meet the criteria, the playbook fails for that file, indicating which file has an invalid extension.
The output snippet below illustrates the playbook’s execution results:
PLAY [Validate file extensions] ******************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************
ok: [localhost]
TASK [Check file extensions] *********************************************************************************************************************
ok: [localhost] => (item=data1.csv) => {
"ansible_loop_var": "item",
"changed": false,
"item": "data1.csv",
"msg": "Valid file extension for file: data1.csv"
}
ok: [localhost] => (item=report.txt) => {
"ansible_loop_var": "item",
"changed": false,
"item": "report.txt",
"msg": "Valid file extension for file: report.txt"
}
failed: [localhost] (item=summary.json) => {
"ansible_loop_var": "item",
"assertion": "item is match('.*\\.(csv|txt)$')",
"changed": false,
"evaluated_to": false,
"item": "summary.json",
"msg": "File with invalid extension detected: summary.json"
}
PLAY RECAP ***************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
In this output:
- The playbook successfully validates
data1.csv
andreport.txt
. - It fails for
summary.json
because its extension does not meet the specified conditions.
Conclusion
Automating file validation with Ansible not only streamlines the process of maintaining file standards across systems but also enhances security by ensuring that only files with correct formats are processed or moved forward in workflows. By adapting the playbook provided, organizations can enforce data quality and integrity standards efficiently, reducing manual oversight and the potential for errors.
Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and AWX System Administrator and DevOps
Donate
Want to keep this project going? Please donate