Ansible Read Files: lookup, slurp & fetch Module Guide — Video Tutorial
How to read files with Ansible. Use lookup plugin, slurp module, fetch module, and file content in conditionals with practical examples.
Watch Video
Watch "Ansible Read Files: lookup, slurp & fetch Module Guide" on YouTube
What You'll Learn
- Introduction
- Understanding the Playbook
- Listing Files in a Directory
- Reading Content from Each File
- Displaying File Contents
- Complete Playbook
- Conclusion
- Read Local File (Controller)
- Read Remote File (slurp)
- Fetch Remote File to Controller
Full Tutorial Content
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:
1. **List all text files in the specified directory and its subdirectories**.
2. **Read the content from each text file**.
3. **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.
```yaml
- 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`.
```yaml
- 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`.
```yaml
- 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.
```yaml
- name: Display file contents
ansible.builtin.debug:
var: file_contents
```
Complete Playbook
Here's the complete playbook for reference:
```yaml
---
- 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
About This Tutorial
- Author: Luca Berton
- Difficulty: Advanced
- Read time: 7 min
- Category: troubleshooting
Read the full written article: Ansible Read Files: lookup, slurp & fetch Module Guide
Topics Covered
Related Video Tutorials
- Enhancing Ansible Documentation — Join the Initiative to Enhance Module Connections through 'See Also' Suggestions
- Simplify Ansible Output with the community.general.dense Callback Plugin — Learn how to reduce verbosity in Ansible output using callback plugins, enhancing efficiency and integration in large-scale automation tasks.
- Simplifying Ansible Output with the community.general.unixy Callback Plugin — Learn how to enhance Ansible playbook readability by using the community.general.unixy callback plugin for cleaner and more concise output.
- Mastering Ansible-Creator: Simplify Your Ansible Collection Development — Discover how to install and use Ansible-Creator to simplify Ansible Collection development. Enhance your automation projects with this powerful tool and Visual Studio Code integration.
- Ansible Troubleshooting Installation Issues on macOS and Python — Learn how to resolve the ImportError for Jinja2 when installing Ansible on macOS using Homebrew, ensuring smooth automation setup.
- Ansible-Lint: Complete Guide to Linting Playbooks & Roles — Complete guide to ansible-lint. Install, configure, run linting on playbooks and roles, fix common errors, and integrate with CI/CD pipelines.