'How to load environment variable in the file to current shell via ansible

my environment: OS: ubuntu/focal and current user: ubuntu

I'm trying to append some environment variable content to /etc/environment file. Then want to load these setting on current shell via ansible

ubuntu@infra-node1:~$ cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
LC_ALL="en_US.UTF-8"
LANG="en_US.UTF-8"
LANGUAGE="en_US:en"
EDITOR="vi"
http_proxy=http://myproxy:8080
no_proxy=10.1.1.0/24,127.0.0.1

as owner of file /etc/environment is root, I need to take root to manipulate file then load this file to current user(ubuntu) shell space.

so, relevant ansible configuration like

$ cat ./ansible.cfg
[defaults]
inventory         = ./hosts
forks             = 3
become            = true
become_method     = sudo 
become_user       = root

and play book file like

$ cat top.yaml 
---
- name: set environment
  gather_facts: false
  hosts: infra
  roles:
    - role: environment
  tags: environment
$ cat roles/environment/tasks/main.yaml
---
- name: deploy environment file
  copy:
    src: environment
    dest: /etc/environment
    backup: yes

- name: load environment
  become: false
  shell: source /etc/environment
  args:
    executable: /bin/bash

it was able to run but failed to check environment variable

ubuntu@infra-node1:~$ ansible-playbook -i hosts  -t environment top.yaml 

PLAY [set environment] 
TASK [environment : deploy environment file] 
ok: [10.1.1.31]

TASK [environment : load environment] 
changed: [10.1.1.31]

PLAY RECAP 
10.1.1.31: ok=2  changed=1  unreachable=0  failed=0  skipped=0 rescued=0    ignored=0

ubuntu@infra-node1:~$ env | grep proxy

how to achieve that target properly via ansible??? please help.

Thanks a lot in advance.

BR//



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source