Ansible Pilot

Permanently Set Remote System Wide Environment Variables on Linux - /etc/environment - Ansible module lineinfile

How to automate the customization of System-Wide Environment Variables on Linux editing /etc/environment file using Ansible module lineinfile.

How to permanently set system-wide environment variables on remote Linux with Ansible?

I’m going to show you a live demo 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

There are principally two ways to configure System-Wide Environment Variables on Linux:

demo

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

code

---
- name: set environment demo
  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 demo] ***********************************************************************
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 demo] ***********************************************************************
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 [email protected]
Last login: Tue Feb 22 18:45:12 2022 from 192.168.131.111
[[email protected] ~]$ sudo su
[[email protected] devops]# printenv | grep EDITOR
[[email protected] devops]# printenv | grep MY_ENV_VARIABLE
[[email protected] devops]# cat /etc/environment 
[[email protected] devops]#

after execution

ansible-pilot $ ssh [email protected]
Last login: Tue Feb 22 18:48:46 2022 from 192.168.131.111
[[email protected] ~]$ sudo su
[[email protected] devops]# printenv | grep EDITOR
EDITOR=vi
[[email protected] devops]# printenv | grep MY_ENV_VARIABLE
MY_ENV_VARIABLE=ansiblepilot
[[email protected] devops]# cat /etc/environment 
EDITOR=vi
MY_ENV_VARIABLE=ansiblepilot
[[email protected] devops]#

code with ❤️ in GitHub

Recap

Now you know how to Permanently Set System-Wide Environment Variables on Remote Linux with Ansible. Subscribe to the YouTube channel, Medium, Website and Twitter to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 100+ Automation Examples For Linux and Windows System Administrator and DevOps

Want to keep this project going? Please donate

Trustpilot
Follow me

Subscribe not to miss any new releases

February 22, 2022

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress