'How to source a file from Ansible playbook?

I have added few env variables in the /etc/profile and I am able to do so succesfuly. Now I want to source /etc/profile from ansible script so that in the current shell the env variables are set. I can do this manually by :

source /etc/profile
. /etc/profile

I have tried below ways. The script is runnning withput giving any error but the env variables aren't set in the current shell . My intention is to avoid manual steps as much as possible .

# - name: Execute the command in remote shell; stdout goes to the specified file on the remote
#   #sudo: no
#   command: . /etc/profile 
  #ansible.builtin.shell: configuration/files/source.sh
# - name: source bashrc
#   ansible.builtin.shell: "source /etc/profile"

- name: Get the value of the environment variable we just added
  #sudo: no 
  shell: '/bin/bash -i -c "source /etc/profile"'
  # become-user: {your_remote_user}
  # args:
  #   executable: /bin/sh

# - name: Add another bin dir to system-wide $PATH.
#   copy:
#     src: env_vars.sh
#     dest: /etc/profile
#     #content: 'HADOOP_HOME=/opt/spark/hadoop-3.1.1'

I am using ansible-core 2.12 . Is there a way we can source /etc/profile from ansible itself ?



Sources

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

Source: Stack Overflow

Solution Source