Ansible Set Environment Variables: lineinfile for /etc/environment & .bashrc — Video Tutorial
How to permanently set system-wide and user-level environment variables on Linux with Ansible. Use lineinfile for /etc/environment, profile.d, and .bashrc.
Watch Video
Watch "Ansible Set Environment Variables: lineinfile for /etc/environment & .bashrc" on YouTube
What You'll Learn
- How to permanently set system-wide environment variables on remote Linux with Ansible?
- Permanently Set System-Wide Environment Variables on Remote Linux
- Links
- code
- execution
- idempotency
- before execution
- after execution
- Conclusion
- System-Wide Environment Variables
Full Tutorial Content
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.
Links
- [ansible.builtin.lineinfile](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html)
## Playbook
How to permanently set System-Wide Environment variables on Remote Linux with Ansible Playbook.
code
```yaml
---
- name: set environment Playbook
hosts: all
gather_facts: false
become: true
vars:
os_environment:
- key: EDITOR
value: vi
- key: MY_ENV_VARIABLE
value: ansiblepilot
tasks:
- name: customize /etc/environment
ansible.builtin.lineinfile:
dest: "/etc/environment"
state: present
regexp: "^{{ item.key }}="
line: "{{ item.key }}={{ item.value }}"
with_items: "{{ os_environment }}"
```
execution
```bash
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory ansible\ statements/set-environment.yml
PLAY [set environment Playbook] ***********************************************************************
TASK [customize /etc/environment] *****************************************************************
changed: [demo.example.com] => (item={'key': 'EDITOR', 'value': 'vi'})
changed: [demo.example.com] => (item={'key': 'MY_ENV_VARIABLE', 'value': 'ansiblepilot'})
PLAY RECAP ****************************************************************************************
demo.example.com : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
```
idempotency
```bash
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory ansible\ statements/set-environment.yml
PLAY [set environment Playbook] ***********************************************************************
TASK [customize /etc/environment] ****
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 5 min
- Category: troubleshooting
Read the full written article: Ansible Set Environment Variables: lineinfile for /etc/environment & .bashrc
Topics Covered
Related Video Tutorials
- Download a file using an HTTPS proxy via environment variables - Ansible get_url and environment — Learn how to download files using Ansible get_url module with proxy settings, including checksum verification and setting file permissions.
- Ansible environment Keyword: Set Environment Variables Per Task or Play — How to set environment variables per task or play in Ansible using the environment keyword. Configure proxy, PATH, and app-specific variables with examples.
- How to install Ansible in Oracle Linux 8 - Ansible install — Learn how to install the latest Ansible release on Oracle Linux 8 using the oracle-epel-release-el8 repository with a simple script.
- Ansible Write to File: Variable Content with copy & template Modules — How to write variables to files in Ansible. Compare copy content vs template module, write JSON/YAML, and generate dynamic config files with examples.
- Ansible Set File Permissions 755: chmod with file Module Guide — How to set file permissions with Ansible file module. Add execute permission (755, 644, 600), manage ownership, and apply permissions recursively.
- How to Add a Disk to a VMware VM Using Ansible Playbook — Learn how to add a disk to a VMware VM using an Ansible playbook. This guide includes the YAML configuration, variables, and execution steps for easy automation.