AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 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 "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, 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 Environment Variables: Set, Read & Manage env vars (Complete Guide)

By Luca Berton · Published 2024-01-01 · Category: installation

How to set and read environment variables in Ansible. Use environment keyword, lookup('env'), ansible.builtin.lineinfile for /etc/environment. Practical YAML playbook examples.

Ansible Environment Variables: Set, Read & Manage env vars (Complete Guide)

Managing environment variables with Ansible is essential for application deployment, service configuration, and system administration. This guide covers every approach — from setting per-task environment variables to configuring persistent system-wide settings.

Reading Environment Variables

Read from the Control Node

Use the lookup('env') plugin to read environment variables from the Ansible control node:

Read from Remote Hosts

Use ansible.builtin.shell with register or access ansible_env facts:

Setting Environment Variables Per Task

Use the environment keyword at the task level:

Setting Environment Variables Per Play

Apply environment to all tasks in a play:

Setting Environment Variables Per Role

Setting Persistent Environment Variables

System-Wide (/etc/environment)

User-Specific (~/.bashrc or ~/.profile)

Using /etc/profile.d/ (Recommended for Applications)

Systemd Service Environment

Using Variables to Define Environment

Store environment settings in group_vars:

Proxy Configuration

Common pattern for corporate environments:

FAQ

How do I set environment variables in Ansible?

Use the environment keyword at the task, play, or role level. For persistent variables, use ansible.builtin.lineinfile to modify /etc/environment, ~/.bashrc, or create files in /etc/profile.d/.

How do I read environment variables from the control node in Ansible?

Use lookup('env', 'VARIABLE_NAME') to read environment variables from the machine running Ansible. This is evaluated on the control node, not the remote host.

How do I access remote host environment variables in Ansible?

Enable gather_facts: true and access ansible_env.VARIABLE_NAME. For example, ansible_env.PATH returns the remote host's PATH. Alternatively, use ansible.builtin.shell: echo $VAR with register.

What is the difference between environment and ansible_env?

The environment keyword sets environment variables for the current task or play. The ansible_env fact reads existing environment variables from the remote host. They serve opposite purposes.

How do I pass environment variables to a systemd service with Ansible?

Create an environment file (e.g., /etc/myapp/env) with KEY=VALUE pairs, then reference it in the systemd unit with EnvironmentFile=/etc/myapp/env. Use ansible.builtin.copy or ansible.builtin.template to manage the file.

Conclusion

Ansible provides multiple ways to manage environment variables — from per-task settings with the environment keyword to persistent configuration via /etc/environment and /etc/profile.d/. Choose the right approach based on whether you need temporary task-level variables or permanent system configuration.

Related ArticlesAnsible lookup('file'): Read Local File Contents into Variableansible.builtin.set_fact Module: Set Variables DynamicallyAnsible lineinfile Module: Add, Replace & Delete Lines in FilesAnsible Python Interpreter: Fix ansible_python_interpreter Errors

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home