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.

Ansible Read Files: lookup, slurp & fetch Module Guide

By Luca Berton · Published 2024-01-01 · Category: troubleshooting

How to read files with Ansible. Use lookup plugin, slurp module, fetch module, and file content in conditionals with practical examples.

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.

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.

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.

Displaying File Contents

Finally, we display the content of the files using the ansible.builtin.debug module.

Complete Playbook

Here's the complete playbook for reference:

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.

Read Local File (Controller)

Read Remote File (slurp)

Fetch Remote File to Controller

Read and Parse JSON

Read and Parse YAML

Read File Lines

Read with File Glob

Check File Before Reading

Read CSV File

Read INI File

Comparison

| Method | Location | Encoding | Use Case | |--------|----------|----------|----------| | lookup('file') | Controller | Plain text | Read local files | | slurp | Remote | Base64 | Read remote files | | fetch | Remote → Local | Binary | Download files | | command: cat | Remote | stdout | Quick reads |

FAQ

Why is slurp output base64?

To safely transfer binary/special characters. Always decode: content | b64decode

Can I read binary files?

Use slurp + b64decode or fetch to copy to controller. lookup('file') is text-only.

How do I read a large file?

slurp loads the entire file into memory. For large files, use command: head -n 100 /path/file or fetch to download.

Read Local File (lookup)

Read Remote File (slurp)

Fetch Remote File

Read and Use in Template

Read Lines into List

Read JSON File

Read YAML File

Read with Conditionals

Read Directory Contents

FAQ

lookup vs slurp?

lookup('file') reads from the controller. slurp reads from the managed node. Use lookup for local files, slurp for remote.

Why is slurp content base64 encoded?

To safely transfer binary content over JSON. Always pipe through b64decode.

How to read large files?

slurp loads the entire file into memory. For large files, use command: cat or fetch to download first, or use lineinfile/replace to process in place.

Related ArticlesAnsible loop_control GuideAnsible Template GuideAnsible set_fact GuideAnsible Loops Guide

Category: troubleshooting

Watch the video: Ansible Read Files: lookup, slurp & fetch Module Guide — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home