How to permanently set system-wide environment variables on remote Linux with Ansible?
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/environmentis 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/profileand/etc/profile.d/*.share 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/profileexecutes all the*.shscripts 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/profilefor each user.
Links
Playbook
How to permanently set System-Wide Environment variables on Remote Linux with Ansible Playbook.
code
---
- 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
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
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory ansible\ statements/set-environment.yml
PLAY [set environment Playbook] ***********************************************************************
TASK [customize /etc/environment] *****************************************************************
ok: [demo.example.com] => (item={'key': 'EDITOR', 'value': 'vi'})
ok: [demo.example.com] => (item={'key': 'MY_ENV_VARIABLE', 'value': 'ansiblepilot'})
PLAY RECAP ****************************************************************************************
demo.example.com : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
before execution
ansible-pilot $ ssh devops@demo.example.com
Last login: Tue Feb 22 18:45:12 2022 from 192.168.131.111
[devops@demo ~]$ sudo su
[root@demo devops]# printenv | grep EDITOR
[root@demo devops]# printenv | grep MY_ENV_VARIABLE
[root@demo devops]# cat /etc/environment
[root@demo devops]#
after execution
ansible-pilot $ ssh devops@demo.example.com
Last login: Tue Feb 22 18:48:46 2022 from 192.168.131.111
[devops@demo ~]$ sudo su
[root@demo devops]# printenv | grep EDITOR
EDITOR=vi
[root@demo devops]# printenv | grep MY_ENV_VARIABLE
MY_ENV_VARIABLE=ansiblepilot
[root@demo devops]# cat /etc/environment
EDITOR=vi
MY_ENV_VARIABLE=ansiblepilot
[root@demo devops]#
Conclusion
Now you know how to Permanently Set System-Wide Environment Variables on Remote Linux with Ansible.