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.

Join 50+ hours of courses in our exclusive community

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 [email protected]
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 ~]$

code with ❤️ in GitHub

Conclusion

Now you know how to read a file from remote hosts with Ansible. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza