AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 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 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", 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 slurp Module: Read File Content from Remote Hosts (ansible.builtin.slurp) — Video Tutorial
Complete guide to ansible.builtin.slurp module. Read and decode file content from remote hosts as base64. Practical examples with register and b64decode.
What You'll Learn
- Read a file from remote hosts - Ansible module slurp
- Ansible Read file from remote hosts
- Parameters
- Links
- code
- execution
- idempotency
- verification
- Conclusion
- Basic slurp
Full Tutorial Content
Read a file from remote hosts - Ansible module slurp
How to automate the read of `/proc/cpuinfo` file from Linux remote host with Ansible. The file is copied as base 64 encoding and decoded with an Ansible Filter.
Ansible Read file from remote hosts
- ansible.builtin.slurp
- Slurps a file from remote nodes
- Fetching a base64-encoded blob of the data in a remote file.
Today we're talking about the Ansible module `slurp`.
The full name is `ansible.builtin.slurp` which means is part of the collection of modules "builtin" with ansible and shipped with it.
This module is pretty stable and out for years and supports Linux and Windows targets.
The purpose is to slurp a file from a remote location. Please note that the read operation is going to fetch a base64-encoded blob containing the data in a remote file.
Parameters
- src string - Remote file path
This module has only one parameter "src", which is also mandatory.
The parameter "src" specifies the source files in the remote hosts. It must be a file, not a directory.
Links
- [ansible.builtin.slurp](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/slurp_module.html)
## Playbook
Read a file from remote hosts with Ansible Playbook.
code
```yaml
---
- name: slurp module Playbook
hosts: all
become: false
vars:
remotefile: "/proc/cpuinfo"
tasks:
- name: slurp remote file
ansible.builtin.slurp:
src: "{{ remotefile }}"
register: slurpfile
- name: print remote file
ansible.builtin.debug:
msg: "{{ slurpfile['content'] | b64decode }}"
```
execution
```bash
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory read\ file\ from\ remote\ hosts/slurp.yml
PLAY [slurp module Playbook] **************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [slurp remote file] **************************************************************************
ok: [demo.example.com]
TASK [print remote file] **************************************************************************
ok: [demo.example.com] => {
"msg": "processor\t: 0\nvendor_id\t: GenuineIntel\ncpu family\t: 6\nmodel\t\t: 158\nmodel name\t: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz\nstepping\t: 10\ncpu MHz\t\t: 2591.998\ncache size\t: 12288 KB\nphysical id\t: 0\nsiblings\t: 1\ncore id\t\t: 0\ncpu cores\t: 1\napicid\t\t: 0\ninitial apicid\t: 0\nfpu\t\t: yes\nfpu_exception\t: yes\ncpuid level\t: 22\nwp\t\t: yes\nflags\t\t: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq monitor ssse3 cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single pti fsgsbase avx2 invpcid rdseed clflushopt md_clear flush_l1d\nbugs\t\t: cpu
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 7 min
- Category: troubleshooting
Read the full written article: Ansible slurp Module: Read File Content from Remote Hosts (ansible.builtin.slurp)
Related Video Tutorials
- Ansible Copy Multiple Files: fileglob Lookup & with_fileglob Examples — Discover how to use Ansible fileglob lookup plugin and copy module to efficiently transfer multiple files to remote hosts. Explore practical examples and steps.
- Ansible fetch Module: Copy Files from Remote Hosts to Control Node — How to copy files from remote hosts with Ansible fetch module (ansible.builtin.fetch). Download logs, configs, backups from Linux and Windows.
- Ansible fetch Module: Download Files from Remote Hosts (Complete Guide) — Complete guide to Ansible fetch module (ansible.builtin.fetch). Download and retrieve files from remote hosts to local machine with flat, dest.
- Ansible win_copy Module: Copy Files to Windows Hosts (ansible.windows.win_copy) — How to copy files to Windows remote hosts with Ansible win_copy module (ansible.windows.win_copy). Transfer files, directories, set permissions.
- Ansible copy Module: Copy Files to Remote Hosts (ansible.builtin.copy Guide) — How to copy files to remote hosts with Ansible copy module (ansible.builtin.copy). Transfer files, set permissions, use content parameter, backup.
- Ansible Set File Permissions 755: chmod with file Module Guide — How to set file permissions with Ansible file module. Add execute permission (755, 644, 600), manage ownership, and apply permissions recursively.