Member-only story

Read a file into a variable on host — Ansible lookup plugin file

How to automate the reading of example.txt file on Ansible host, assign to a variable and use in your Ansible Playbook code.

Luca Berton
4 min readFeb 16, 2022

How to read a file into a variable on the host with Ansible?
I’m going to show you a live demo with some simple Ansible code.
I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible read a file into a variable

  • `ansible.builtin.file`
  • read file contents

Let’s deep dive into the Ansible lookup plugin file.
Plugins are a way to expand the Ansible functionality. With lookup plugins specifically, you can load variables or templates with information from external sources.
The full name is `ansible.builtin.file`, it’s part of `ansible-core` and is included in all Ansible installations.
The purpose of the `file` lookup plugin is to read file contents.

Parameters and Return Values

Parameters

--

--

Luca Berton
Luca Berton

Written by Luca Berton

I help creative Automation DevOps, Cloud Engineer, System Administrator, and IT Professional to succeed with Ansible Technology to automate more things everyday

No responses yet