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 Articles • Ansible lookup('file'): Read Local File Contents into Variable • ansible.builtin.set_fact Module: Set Variables Dynamically • Ansible lineinfile Module: Add, Replace & Delete Lines in Files • Ansible Python Interpreter: Fix ansible_python_interpreter Errors
Category: installation