# Ansible - Environment variable

**URL:** <https://forum.ansible.com/t/ansible-environment-variable/32283>\
**Category:** Ansible Project\
**Created:** [February 20, 2020, 9:55am UTC](https://forum.ansible.com/t/ansible-environment-variable/32283 "2020-02-20T09:55:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sivaraman\_P](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sivaraman\_P](https://forum.ansible.com/u/Sivaraman_P)\
**Post date:** [February 20, 2020, 9:55am UTC](https://forum.ansible.com/t/ansible-environment-variable/32283/1 "2020-02-20T09:55:32Z")

</div>

0I have the env file which is present in target machine and it contain certain number of variables with export command(the export command itself was present inside the file).

```auto
export AB_HOME=/et/dev/abinitio/abinitio-V3
export PATH=${AB_HOME}/bin:${PATH}

```

I have executed the env file using the below playbook and I tried to read the variables which are exported using the output1 which is a register variable in my playbook. But I am able to see my register variable is empty. Is there any way to get the variables which are all exported. I don’t know the variables name which are present inside the file, So I am not able to use the ECHO command as well.

```auto
- hosts: dev
  gather_facts: false
  tasks:   
    - name: get the environment variables
      shell: "su <id> & . ./.env"
      args:
        chdir: /path to the file
      register: output1

    - debug: var=output1.stdout_lines 

```

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 20, 2020, 11:39am UTC](https://forum.ansible.com/t/ansible-environment-variable/32283/2 "2020-02-20T11:39:50Z")

</div>

Hi

Could you please tell clearly:

- What you are trying to achieve.  
- How you are doing this.  
- What problems you encounter.  
- Which command did you run, and what output did you get from that  
(copied as text, not as images/attachments/screenshots)  
- What the relevant tasks/playbooks/code/variables look like

---

<div class="post-metadata">

**Author:** ![Sivaraman\_P](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sivaraman\_P](https://forum.ansible.com/u/Sivaraman_P)\
**Post date:** [February 20, 2020, 12:18pm UTC](https://forum.ansible.com/t/ansible-environment-variable/32283/3 "2020-02-20T12:18:04Z")

</div>

Hi, Thank you for your response,

I have a command to check the status of abinitio service in target machines. But, the status command will only work if the env file is executed.

The env file contain some environment variables like below. The variables numbers and value will change for one target machine to another.

```auto
export AB_HOME=/et/dev/abinitio/abinitio-V3
export PATH=${AB_HOME}/bin:${PATH}

```

I tried to execute the env file present in the target machine by sourcing the env file and I tried to register the output to get what are all the variables present in the file in-order to use them in my next task, which is to check the status. But, I am able to see my register variable is empty. I just want to execute the environment file and to run the status command with that executed environment. Below is the playbook I have used,

```auto
- hosts: dev
  gather_facts: false
  tasks:   
    - name: get the environment variables
      shell: "su <id> & . ./.env" #this command is to execute the env file and store the result
      args:
        chdir: /path to the file
      register: output1 #for storing exported variables.

    - debug: var=output1.stdout_lines #print exported variables (Here I am not getting anything the stdout is empty)

```

> Hi
> 
> Could you please tell clearly:
> 
> - What you are trying to achieve.
> - How you are doing this.
> - What problems you encounter.
> - Which command did you run, and what output did you get from that  
> (copied as text, not as images/attachments/screenshots)
> - What the relevant tasks/playbooks/code/variables look like
> 
> > 0I have the env file which is present in target machine and it contain certain number of variables with export command(the export command itself was present inside the file).
> > 
> > export AB\_HOME=/et/dev/abinitio/abinitio-V3  
> > export PATH=${AB\_HOME}/bin:${PATH}
> > 
> > I have executed the env file using the below playbook and I tried to read the variables which are exported using the output1 which is a register variable in my playbook. But I am able to see my register variable is empty. Is there any way to get the variables which are all exported. I don’t know the variables name which are present inside the file, So I am not able to use the ECHO command as well.
> > 
> > - hosts: dev  
> > gather\_facts: false  
> > tasks:
> > 
> > - name: get the environment variables  
> > shell: “su & . ./.env”  
> > args:  
> > chdir: /path to the file  
> > register: output1
> > 
> > - debug: var=output1.stdout\_lines

- name: check status  
shell: “su & ” #command to check status  
environment: “{{output1.stdout\_lines}}” #setting the environment in which th above status command need to run  
register: output2

- debug: var=output2.stdout\_lines

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 20, 2020, 1:53pm UTC](https://forum.ansible.com/t/ansible-environment-variable/32283/4 "2020-02-20T13:53:29Z")

</div>

Hi

You could fetch the .env file, parse it, and then use that as  
environment vars for a subsequent shell/command.  
It's not the cleanest method and probably fragile.

There is another problem, the shell and command module will execute a  
single command.  
What you have is two command, the first one being "su".  
Instead you should either connect as the user you want to run the  
command as, or you should use proper privilege escalation.  
Read [https://docs.ansible.com/ansible/latest/user\_guide/become.html#understanding-privilege-escalation-become](https://docs.ansible.com/ansible/latest/user_guide/become.html#understanding-privilege-escalation-become)  
for that.  
If "\<id\>" is different that the user you connect as, but it's not a  
privileged user, you will run into issues, so for that read:  
[https://docs.ansible.com/ansible/latest/user\_guide/become.html#risks-of-becoming-an-unprivileged-user](https://docs.ansible.com/ansible/latest/user_guide/become.html#risks-of-becoming-an-unprivileged-user)

Having said all of that, this should do the trick:

---

<div class="post-metadata">

**Author:** ![Sivaraman\_P](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sivaraman\_P](https://forum.ansible.com/u/Sivaraman_P)\
**Post date:** [February 20, 2020, 2:13pm UTC](https://forum.ansible.com/t/ansible-environment-variable/32283/5 "2020-02-20T14:13:01Z")

</div>

Thanks much for your reply, I think this is what the solution I need. But there are two things which I am not able get. I am new to ansible, kindly answer those question even though it was silly. whether the variables which are being used in setfact need to be set before using that in setfact, if so how to do that. And the 2nd question is

- set\_fact:

my\_env: “{{ envs|default({}) | combine( { item.split(‘=’)[0]:  
item.split(‘=’)[1] } ) }}”  
loop: “{{ dotenv\_pairs.split() }}”

in the above task what is envs and what is the roll of envs|default({})

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 20, 2020, 3:30pm UTC](https://forum.ansible.com/t/ansible-environment-variable/32283/6 "2020-02-20T15:30:49Z")

</div>

> Thanks much for your reply, I think this is what the solution I need. But there are two things which I am not able get. I am new to ansible, kindly answer those question even though it was silly. whether the variables which are being used in setfact need to be set before using that in setfact, if so how to do that.

I have no idea what you mean. In any case, the playbook just works, so  
do some trial and error yourself if you want to see if things are  
needed or not.

> And the 2nd question is
> 
> - set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_env: "{{ envs|default({}) | combine( { item.split('=')[0]:  
> item.split('=')[1] } ) }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ dotenv\_pairs.split() }}"
> 
> in the above task what is envs and what is the roll of envs|default({})

That was a typo, the set\_fact tasks should read:

&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dotenv\_pairs: "{{ dotenv.content | b64decode |  
regex\_replace('export\\s+', '') }}"

&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_env: "{{ my\_env|default({}) | combine( { item.split('=')[0]:  
item.split('=')[1] } ) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ dotenv\_pairs.split() }}"

---

<div class="post-metadata">

**Author:** ![Sivaraman\_P](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sivaraman\_P](https://forum.ansible.com/u/Sivaraman_P)\
**Post date:** [February 21, 2020, 10:02am UTC](https://forum.ansible.com/t/ansible-environment-variable/32283/7 "2020-02-21T10:02:37Z")

</div>

Now I am able to set the environment using the playbook you have sent. But I Have one more problem in this. The file that I have contains variables like below. As we are reading the file and setting the environment, the values are not passing correctly, Is there any workaround for that?

Variables present in the file,

export AB\_AIR\_ROOT=//devetl1/et/devctl/ee/rep/dev

export DEV\_AIR\_ROOT=${AB\_AIR\_ROOT}

after running the playbook, I tried to echo $DEV\_AIR\_ROOT, But I am getting output as ${AB\_AIR\_ROOT} instead of getting //devetl1/et/devctl/ee/rep/dev

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 22, 2020, 6:16am UTC](https://forum.ansible.com/t/ansible-environment-variable/32283/8 "2020-02-22T06:16:09Z")

</div>

Not really, this is what I meant with that the solution is fragile.

---

<div class="post-metadata">

**Author:** ![Sivaraman\_P](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sivaraman\_P](https://forum.ansible.com/u/Sivaraman_P)\
**Post date:** [February 22, 2020, 6:18am UTC](https://forum.ansible.com/t/ansible-environment-variable/32283/9 "2020-02-22T06:18:14Z")

</div>

Is there any other way to do this?
