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 lookup file Plugin: Read Local File Content into Variable (Guide) — Video Tutorial
How to read file content into a variable with Ansible lookup('file') plugin. Load config files, SSH keys, certificates into playbook variables.
What You'll Learn
- How to read a file into a variable on the host with Ansible?
- Ansible read a file into a variable
- Parameters and Return Values
- Parameters
- Return Values
- code
- execution
- idempotency
- Conclusion
- File Lookup Examples
Full Tutorial Content
How to read a file into a variable on the host with Ansible?
I'm going to show you a live Playbook 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.
## Playbook
Read a file into a variable on the host with Ansible Playbook.
code
```yaml
---
- 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
```bash
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
```bash
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 $
```
[code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/)
Conclusion
Now you know how to read a file into a variable on
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 4 min
- Category: installation
Read the full written article: Ansible lookup file Plugin: Read Local File Content into Variable (Guide)
Related Video Tutorials
- Ansible Read JSON File: lookup file Plugin & from_json (Guide) — How to read JSON files into variables in Ansible using lookup file plugin and from_json filter. Load config files, parse data, and access nested values.
- Creating a Custom Ansible Lookup Plugin in Python for Reading a File — Create a custom Ansible lookup plugin in Python to extend capabilities for reading file contents on the Ansible controller, enhancing file retrieval.
- Creating a Custom Ansible Lookup Plugin in Python for retrieving API token — Learn how to create an Ansible lookup plugin to fetch API tokens, with a complete example of the token.py plugin code and step-by-step explanations.
- Executing Custom Lookup Plugins in the Ansible Automation Platform — How to execute two custom Ansible Lookup Plugins to interact with API and read the contents of files in the local Ansible Controller.
- Ansible Read Environment Variable: lookup('env') Plugin Guide — How to read environment variables in Ansible with the env lookup plugin. Access PATH, HOME, USER variables in playbooks.
- Ansible Set File Permissions 755: chmod with file Module Guide — How to set file permissions with Ansible file module. Add execute permission (755, 644, 600), manage ownership, and apply permissions recursively.