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 Read Environment Variable: lookup('env') Plugin Guide — Video Tutorial

How to read environment variables in Ansible with the env lookup plugin. Access PATH, HOME, USER variables in playbooks.

Watch on YouTube · Read the written article

Tutorial summary

What you'll learn

  • How to read an environment variable on Ansible Controller with Ansible?
  • Ansible read an environment variable
  • Parameters and Return Value
  • Parameters
  • Return Values
  • code
  • execution
  • idempotency
  • Conclusion
  • Reading Environment Variables
How to read an environment variable on Ansible Controller 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 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. ## Playbook Read an environment variable with Ansible Playbook. code ```yaml --- - name: environment Playbook hosts: all tasks: - name: display HOME ansible.builtin.debug: msg: "{{ lookup('env', 'HOME') }}" ``` execution ```bash ansible-pilot $ printenv | grep HOME HOME=/Users/lberton ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory ansible\ statements/environment.yml PLAY [environment Playbook] *************************************************************************** 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 Playbook] *************************************************************************** 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 $ ``` Conclusion Now you know h

About this tutorial

  • Author: Luca Berton
  • Difficulty: Beginner
  • Read time: 4 min
  • Category: installation

Topics covered

Related video tutorials