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 Set Environment Variables: lineinfile for /etc/environment & .bashrc

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

How to permanently set system-wide and user-level environment variables on Linux with Ansible. Use lineinfile for /etc/environment, profile.d, and .bashrc.

How to permanently set system-wide environment variables on remote Linux 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.

Permanently Set System-Wide Environment Variables on Remote Linux • /etc/environment • /etc/profile.d directory

There are principally two ways to configure System-Wide Environment Variables on Linux: • /etc/environment is a system-wide configuration file, which means it is used by all users. It is owned by root so you need admin user privilege or sudo to modify it. Specifically, this file stores the system-wide locale and path settings. • /etc/profile and /etc/profile.d/.sh are the global initialization scripts. This file gets executed whenever a bash login shell is entered via console, terminal, ssh, or graphical user interface. The global scripts get executed before the user-specific scripts though, and the main /etc/profile executes all the .sh scripts in /etc/profile.d/ just before it exits. Each user could customize their ~/.profile, the user's personal shell initialization scripts. Every user has one and can edit their file without affecting others. This is the equivalent to /etc/profile for each user.

Linksansible.builtin.lineinfile

## Playbook

How to permanently set System-Wide Environment variables on Remote Linux with Ansible Playbook.

code

execution

idempotency

before execution

after execution

code with ❤️ in GitHub

Conclusion

Now you know how to Permanently Set System-Wide Environment Variables on Remote Linux with Ansible.

System-Wide Environment Variables

/etc/environment (all users, all shells)

/etc/profile.d/ (login shells)

User-Level Environment Variables

~/.bashrc (interactive bash)

~/.profile (login shells, all sh-compatible)

Remove Environment Variables

Blockinfile for Multiple Variables

Where to Set Variables

| File | Scope | When Loaded | |------|-------|-------------| | /etc/environment | All users | PAM login | | /etc/profile.d/*.sh | All users | Login shell | | /etc/profile | All users | Login shell | | ~/.bashrc | Single user | Interactive bash | | ~/.profile | Single user | Login shell | | ~/.bash_profile | Single user | Bash login | | systemd unit Environment= | Service only | Service start |

For Systemd Services

FAQ

Why doesn't my variable take effect immediately?

Changes to /etc/environment, .bashrc, etc. only apply to new shell sessions. Use environment: in Ansible tasks for immediate effect in the same play.

/etc/environment vs /etc/profile.d/ — which? • /etc/environment: Simple KEY=VALUE, no shell syntax, loaded by PAM • /etc/profile.d/: Full shell script, supports export, PATH manipulation, conditionals

How do I verify the variable was set?

Related ArticlesAnsible Become GuideAnsible Inventory GuideAnsible Environment Variables Guide

Category: troubleshooting

Watch the video: Ansible Set Environment Variables: lineinfile for /etc/environment & .bashrc — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home