Ansible slurp Module: Read File Content from Remote Hosts (ansible.builtin.slurp)
By Luca Berton · Published 2024-01-01 · Category: troubleshooting
Complete guide to ansible.builtin.slurp module. Read and decode file content from remote hosts as base64. Practical examples with register and b64decode.

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.
See also: Ansible slurp Module: Read Remote Files as Base64 (Complete Guide)
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 pathThis 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.
See also: Ansible Copy Multiple Files: fileglob Lookup & with_fileglob Examples
Links
• ansible.builtin.slurp## Playbook
Read a file from remote hosts with Ansible Playbook.
code
---
- 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
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_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit srbds\nbogomips\t: 5183.99\nclflush size\t: 64\ncache_alignment\t: 64\naddress sizes\t: 39 bits physical, 48 bits virtual\npower management:\n\n"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
idempotency
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_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit srbds\nbogomips\t: 5183.99\nclflush size\t: 64\ncache_alignment\t: 64\naddress sizes\t: 39 bits physical, 48 bits virtual\npower management:\n\n"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
verification
ansible-pilot $ ssh devops@demo.example.com
Last login: Tue Jan 25 14:29:36 2022 from 192.168.0.102
[devops@demo ~]$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 158
model name : Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
stepping : 10
cpu MHz : 2591.998
cache size : 12288 KB
physical id : 0
siblings : 1
core id : 0
cpu cores : 1
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 22
wp : yes
flags : 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
bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit srbds
bogomips : 5183.99
clflush size : 64
cache_alignment : 64
address sizes : 39 bits physical, 48 bits virtual
power management:
[devops@demo ~]$
Conclusion
Now you know how to read a file from remote hosts with Ansible.See also: Ansible fetch Module: Copy Files from Remote Hosts to Control Node
Basic slurp
- name: Read remote file
ansible.builtin.slurp:
src: /etc/hostname
register: hostname_file
- debug:
msg: "Hostname: {{ hostname_file.content | b64decode | trim }}"
Read and Parse JSON
- slurp:
src: /opt/myapp/config.json
register: config_raw
- set_fact:
app_config: "{{ config_raw.content | b64decode | from_json }}"
- debug:
msg: "DB host: {{ app_config.database.host }}, Port: {{ app_config.database.port }}"
Read and Parse YAML
- slurp:
src: /etc/myapp/settings.yml
register: settings_raw
- set_fact:
settings: "{{ settings_raw.content | b64decode | from_yaml }}"
Check Config Value
- slurp:
src: /etc/ssh/sshd_config
register: sshd_config
become: true
- set_fact:
sshd_content: "{{ sshd_config.content | b64decode }}"
- fail:
msg: "Root login is enabled!"
when: sshd_content is search('PermitRootLogin\s+yes')
Compare Files Across Hosts
- slurp:
src: /etc/myapp/config.yml
register: host_config
- set_fact:
config_checksum: "{{ host_config.content | b64decode | hash('sha256') }}"
- debug:
msg: "Config differs from primary!"
when: config_checksum != hostvars[groups['primary'][0]].config_checksum
Read Binary File
- slurp:
src: /etc/ssl/certs/myapp.crt
register: cert_file
# Write to another host
- copy:
content: "{{ cert_file.content | b64decode }}"
dest: /etc/ssl/certs/myapp.crt
delegate_to: other_host
Conditional Read
- stat: { path: /etc/myapp/custom.conf }
register: custom_conf
- slurp:
src: /etc/myapp/custom.conf
register: custom_content
when: custom_conf.stat.exists
- set_fact:
config: "{{ (custom_content.content | b64decode) if custom_conf.stat.exists else 'default_config' }}"
slurp vs lookup('file') vs fetch
| Method | Runs On | Returns | Use Case |
|--------|---------|---------|----------|
| slurp | Remote | Base64 string | Read remote into variable |
| lookup('file') | Controller | Plain text | Read local file |
| fetch | Remote → Local | File on disk | Download remote file |
| command: cat | Remote | stdout | Quick read (not idempotent) |
Key Points
• Returns base64-encoded content — always decode with| b64decode
• Loads entire file into memory — avoid for files >10MB
• Works with binary files (certs, images)
• Requires read permission on the remote file
FAQ
Why base64?
To safely transfer any content including binary data and special characters without corruption.
"Permission denied" error?
Add become: true to read files owned by root or other users.
Can I slurp a directory?
No — slurp only reads single files. Use find + loop for multiple files, or fetch for directories.
How do I handle large files?
For files >10MB, use fetch to download to controller instead. slurp loads the entire file into memory.
Related Articles
• become_user and become_method in Ansible • the Ansible inventory deep-dive • managing Windows hosts with AnsibleCategory: troubleshooting
Watch the video: Ansible slurp Module: Read File Content from Remote Hosts (ansible.builtin.slurp) — Video Tutorial