Read an environment variable - Ansible lookup plugin env
How to automate the reading of HOME environmental variable and use it in your Ansible Playbook code with lookup plugin env.


How to read an environment variable on Ansible Controller 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 an environment variable
- ansible.builtin.env
- Read the value of environment variables
Let’s deep dive into the Ansible lookup plugin env.
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.env
, it’s part of ansible-core
and is included in all Ansible installations.
The purpose of the env
lookup plugin is to read the value of environment variables.
Parameters and Return Value
Parameters
- _terms string - Environment variable
Return Values
- _raw list - Values from the environment variables
The parameters of plugin env.
The only required parameter is the default “_terms”, with the name of the environment variable to read. The normal usage is to assign the lookup plugin to a variable name but you could use it in your Ansible task directly.
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
- Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
demo
Read an environment variable with Ansible Playbook.
code
---
- name: environment demo
hosts: all
tasks:
- name: display HOME
ansible.builtin.debug:
msg: "{{ lookup('env', 'HOME') }}"
execution
ansible-pilot $ printenv | grep HOME
HOME=/Users/lberton
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory ansible\ statements/environment.yml
PLAY [environment demo] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [display HOME] *******************************************************************************
ok: [demo.example.com] => {
"msg": "/Users/lberton"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
idempotency
ansible-pilot $ printenv | grep HOME
HOME=/Users/lberton
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory ansible\ statements/environment.yml
PLAY [environment demo] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [display HOME] *******************************************************************************
ok: [demo.example.com] => {
"msg": "/Users/lberton"
}
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 an environment variable with Ansible. You know how to use it based on your use case.
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