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 Remote File Content into Variable (Guide)

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

How to read file content from remote hosts with Ansible slurp module (ansible.builtin.slurp). Base64 decode, binary files, config parsing.

There are several ways to read file contents in Ansible depending on whether the file is on the control node (local) or managed host (remote).

Read a Local File (lookup)

Files on the Ansible control node:

- name: Read local file into variable
  ansible.builtin.set_fact:
    my_content: "{{ lookup('file', '/path/to/local/file.txt') }}"

- name: Use file content ansible.builtin.debug: msg: "File says: {{ my_content }}"

Read Local JSON File

- name: Read and parse JSON file
  ansible.builtin.set_fact:
    config: "{{ lookup('file', 'config.json') | from_json }}"

- name: Access JSON values ansible.builtin.debug: msg: "Database host: {{ config.database.host }}"

Read Local YAML File

- name: Read and parse YAML file
  ansible.builtin.set_fact:
    settings: "{{ lookup('file', 'settings.yml') | from_yaml }}"

Read Multiple Local Files

- name: Read all config files
  ansible.builtin.set_fact:
    configs: "{{ configs | default({}) | combine({item | basename: lookup('file', item)}) }}"
  loop: "{{ query('fileglob', 'configs/*.conf') }}"

See also: Ansible slurp Module: Read Remote Files as Base64 (Complete Guide)

Read a Remote File (slurp)

Files on managed hosts — slurp reads file content as base64:

- name: Read remote file
  ansible.builtin.slurp:
    src: /etc/hostname
  register: hostname_file

- name: Decode and display ansible.builtin.debug: msg: "Hostname: {{ hostname_file.content | b64decode | trim }}"

Read Remote JSON File

- name: Slurp remote JSON config
  ansible.builtin.slurp:
    src: /etc/myapp/config.json
  register: remote_config

- name: Parse JSON ansible.builtin.set_fact: app_config: "{{ remote_config.content | b64decode | from_json }}"

- name: Use config values ansible.builtin.debug: msg: "Port: {{ app_config.port }}"

Read Remote YAML File

- name: Slurp remote YAML
  ansible.builtin.slurp:
    src: /etc/myapp/settings.yml
  register: remote_yaml

- name: Parse YAML ansible.builtin.set_fact: settings: "{{ remote_yaml.content | b64decode | from_yaml }}"

Download Remote File to Control Node (fetch)

- name: Fetch remote file to local
  ansible.builtin.fetch:
    src: /var/log/app.log
    dest: /tmp/logs/{{ inventory_hostname }}/
    flat: true

- name: Read the fetched file ansible.builtin.set_fact: log_content: "{{ lookup('file', '/tmp/logs/' + inventory_hostname + '/app.log') }}" delegate_to: localhost

See also: Ansible Read JSON File: lookup file Plugin & from_json (Guide)

Read Command Output into Variable

- name: Run command and capture output
  ansible.builtin.command: cat /proc/version
  register: proc_version
  changed_when: false

- name: Use the output ansible.builtin.debug: msg: "Kernel: {{ proc_version.stdout }}"

Read File Line by Line

# Local file
- name: Read local file as lines
  ansible.builtin.set_fact:
    file_lines: "{{ lookup('file', 'servers.txt').splitlines() }}"

- name: Process each line ansible.builtin.debug: msg: "Server: {{ item }}" loop: "{{ file_lines }}" when: item | trim | length > 0

# Remote file - name: Slurp and split by lines ansible.builtin.slurp: src: /etc/myapp/whitelist.txt register: whitelist_raw

- name: Parse lines ansible.builtin.set_fact: whitelist: "{{ (whitelist_raw.content | b64decode).splitlines() | select | list }}"

See also: Ansible lookup file Plugin: Read Local File Content into Variable (Guide)

Read INI/Properties File

- name: Read INI value
  ansible.builtin.set_fact:
    db_host: "{{ lookup('ini', 'host', section='database', file='config.ini') }}"

Read CSV File

- name: Read CSV file
  ansible.builtin.set_fact:
    users: "{{ lookup('csvfile', 'admin', file='users.csv', col=1, delimiter=',') }}"

Check If File Exists Before Reading

# Local
- name: Read if local file exists
  ansible.builtin.set_fact:
    my_data: "{{ lookup('file', 'optional.conf', errors='ignore') | default('fallback') }}"

# Remote - name: Check remote file ansible.builtin.stat: path: /etc/myapp/override.yml register: override_file

- name: Read if exists ansible.builtin.slurp: src: /etc/myapp/override.yml register: override_content when: override_file.stat.exists

- name: Parse with fallback ansible.builtin.set_fact: overrides: "{{ (override_content.content | b64decode | from_yaml) if override_file.stat.exists else {} }}"

Quick Reference

| Method | Source | Returns | Use When | |--------|--------|---------|----------| | lookup('file', path) | Control node | String | Local files, templates | | slurp module | Remote host | Base64 (decode with b64decode) | Remote file content | | fetch module | Remote → local | File on disk | Need file locally | | command: cat | Remote host | stdout | Simple cases, scripts | | lookup('ini', ...) | Control node | Value | INI/properties files |

FAQ

How do I read a remote file into a variable?

Use the ansible.builtin.slurp module. It reads the file as base64, which you decode with the b64decode filter: {{ result.content | b64decode }}.

What's the difference between lookup('file') and slurp?

lookup('file') reads files from the Ansible control node (where you run ansible-playbook). slurp reads files from the remote managed host. They serve different use cases.

Can I read binary files?

slurp reads any file as base64, including binaries. lookup('file') is for text files only. For binary files you need to process remotely, use fetch to download them first.

Conclusion

Use lookup('file') for local files and slurp + b64decode for remote files. Parse structured data with from_json or from_yaml. Always check file existence with stat before reading optional files.

Related Articles

Ansible slurp ModuleAnsible Lookup Plugins GuideAnsible from_json & to_json

Category: troubleshooting

Browse all Ansible tutorials · AnsiblePilot Home