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.


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
- _terms string - path(s) of files to read
Return Values
- _raw list - content of file(s)
The parameters of the lookup plugin file
.
The only required parameter is the default “_terms”, with the path(s) of files to read.
The normal usage is to assign the lookup plugin to a variable name that you could use in your playbook.
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
demo
Read a file into a variable on the host with Ansible Playbook.
code
---
- name: read file on host
hosts: all
vars:
contents: "{{ lookup('file','example.txt') }}"
tasks:
- name: print file
ansible.builtin.debug:
msg: "the content of file is {{ contents }}"
execution
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory variables/read-file.yml
PLAY [read file on host] **************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [print file] *********************************************************************************
ok: [demo.example.com] => {
"msg": "the content of file is example contents"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
idempotency
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory variables/read-file.yml
PLAY [read file on host] **************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [print file] *********************************************************************************
ok: [demo.example.com] => {
"msg": "the content of file is example contents"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
Recap
Now you know how to read a file into a variable on host with Ansible. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate