Introduction
Automation is key in modern IT infrastructure management. Ansible, a powerful automation tool, makes it easy to manage complex environments. One of the many tasks you can automate with Ansible is reading content from multiple files. This article will guide you through a simple playbook that reads content from text files within a specified directory and displays the content.
Understanding the Playbook
The provided playbook is designed to run on the local host and performs the following tasks:
- List all text files in the specified directory and its subdirectories.
- Read the content from each text file.
- Display the content of the files.
Let’s break down each section of the playbook to understand how it works.
Listing Files in a Directory
First, we use the ansible.builtin.find
module to list all text files in the specified directory (/path/to/your/directory
). The recurse: yes
option ensures that the search includes subdirectories.
- name: List files in directory
ansible.builtin.find:
paths: "/path/to/your/directory"
recurse: yes
patterns: "*.txt"
register: files_list
The results are stored in a variable called files_list
.
Reading Content from Each File
Next, we initialize an empty list to store the file contents using ansible.builtin.set_fact
.
- name: Read content from each file
ansible.builtin.set_fact:
file_contents: []
We then use a loop to iterate over each file in files_list.files
. The ansible.builtin.slurp
module reads the content of each file and stores it in the slurped_files
variable. The loop_control
ensures we use a custom loop variable item_info
.
- name: Read content from each file
ansible.builtin.slurp:
src: "{{ item.path }}"
loop: "{{ files_list.files }}"
register: slurped_files
loop_control:
loop_var: item
- name: Set fact for file contents
ansible.builtin.set_fact:
file_contents: "{{ slurped_files.results | map(attribute='content') | map('b64decode') | list }}"
Displaying File Contents
Finally, we display the content of the files using the ansible.builtin.debug
module.
- name: Display file contents
ansible.builtin.debug:
var: file_contents
Complete Playbook
Here’s the complete playbook for reference:
---
- name: Read content from multiple files
hosts: localhost
gather_facts: no
tasks:
- name: List files in directory
ansible.builtin.find:
paths: "/path/to/your/directory"
recurse: yes
patterns: "*.txt"
register: files_list
- name: Read content from each file
ansible.builtin.slurp:
src: "{{ item.path }}"
loop: "{{ files_list.files }}"
register: slurped_files
loop_control:
loop_var: item
- name: Set fact for file contents
ansible.builtin.set_fact:
file_contents: "{{ slurped_files.results | map(attribute='content') | map('b64decode') | list }}"
- name: Display file contents
ansible.builtin.debug:
var: file_contents
Conclusion
Automating file reading tasks with Ansible can save time and reduce errors in managing large numbers of files. By using the ansible.builtin.find
and ansible.builtin.slurp
modules, you can easily list and read files, respectively. This playbook serves as a basic template that you can expand upon to suit more complex needs, such as processing file contents or integrating with other automation workflows.
Academy
Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.
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