AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Automating File Extension Validation with Ansible — Video Tutorial

Learn how to structure an Ansible playbook to validate file extensions, ensuring files end with specified formats like .csv or .txt through practical examples.

Watch Video

Watch "Automating File Extension Validation with Ansible" on YouTube

What You'll Learn

Full Tutorial Content

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: ```yaml --- - 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 1. **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. 2. **Variables**: The `my_dicts` variable is a list of dictionaries, each representing a file with a `file_name` key. This structure is flexible and can be expanded to include more files or metadata as needed. 3. **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. Execution Flow and Output When executed, the playbook performs the following steps: 1. **Gathering Facts**: Collects the system facts from `localhost`. 2. **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] *****************************************************************

About This Tutorial

Read the full written article: Automating File Extension Validation with Ansible

Topics Covered

Related Video Tutorials