# seting environment

**URL:** <https://forum.ansible.com/t/seting-environment/26288>\
**Category:** Ansible Project\
**Created:** [December 14, 2017, 6:43am UTC](https://forum.ansible.com/t/seting-environment/26288 "2017-12-14T06:43:29Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![coach.rhca](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@coach.rhca](https://forum.ansible.com/u/coach.rhca)\
**Post date:** [December 14, 2017, 6:43am UTC](https://forum.ansible.com/t/seting-environment/26288/1 "2017-12-14T06:43:29Z")

</div>

I have few variables defined in a variable.yml file for example

cat [variable.com](http://variable.com) ( its a dynamic file and the value keeps changing )  
ptr\_image: [my.example.com/jobname/testname](http://my.example.com/jobname/testname)  
ver\_number: v99

I am using this in my playbook … build.yml

where i am using this file for the variables…

---

<div class="post-metadata">

**Author:** ![coach.rhca](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@coach.rhca](https://forum.ansible.com/u/coach.rhca)\
**Post date:** [December 14, 2017, 1:07pm UTC](https://forum.ansible.com/t/seting-environment/26288/2 "2017-12-14T13:07:22Z")

</div>

any thoughts or approach in this regard. …

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 14, 2017, 2:58pm UTC](https://forum.ansible.com/t/seting-environment/26288/3 "2017-12-14T14:58:52Z")

</div>

Check out environments, include vars doesn't export anything to a shell. It only load the variable inside Ansible.

---

<div class="post-metadata">

**Author:** ![coach.rhca](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@coach.rhca](https://forum.ansible.com/u/coach.rhca)\
**Post date:** [December 14, 2017, 3:35pm UTC](https://forum.ansible.com/t/seting-environment/26288/4 "2017-12-14T15:35:10Z")

</div>

thanks Kai, if the include\_vars will not export , i have tried the environment module too… the variable file is dynamic and every time it changes the value.  
cat [variable.yml](http://variable.com)( its a dynamic file and the value keeps changing )  
ptr\_image: [my.example.com/jobname/testname ======\>( this value will change frequently )](http://my.example.com/jobname/testname)  
ver\_number: v99 ===\> (this values will change frequently in the file say to v100 v101 …v150 )

so how do we export the change/dynamic values so that the command docker stack -c somefile.yml runs with the exported variables…when using environment module…please share your insight …thanks

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 14, 2017, 3:45pm UTC](https://forum.ansible.com/t/seting-environment/26288/5 "2017-12-14T15:45:03Z")

</div>

Run the include\_var but on the shell you need to list all the variables again, then the variables is available in the shell.

- shell: docker..........  
&nbsp;&nbsp;environment:  
&nbsp;&nbsp;&nbsp;&nbsp;ver\_number: '{{ ver\_number }}'  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;...

---

<div class="post-metadata">

**Author:** ![coach.rhca](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@coach.rhca](https://forum.ansible.com/u/coach.rhca)\
**Post date:** [December 14, 2017, 4:58pm UTC](https://forum.ansible.com/t/seting-environment/26288/6 "2017-12-14T16:58:03Z")

</div>

Thanks Kai for clarifying it…so putting variable under the environment module after shell will export the variable. …can we see the variable with echo $ver\_number on the linux host if it has been exported using environment

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 14, 2017, 5:04pm UTC](https://forum.ansible.com/t/seting-environment/26288/7 "2017-12-14T17:04:41Z")

</div>

The environment is only for that single task in Ansible, it doesn't do any changes to the system.

---

<div class="post-metadata">

**Author:** ![coach.rhca](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@coach.rhca](https://forum.ansible.com/u/coach.rhca)\
**Post date:** [December 14, 2017, 5:56pm UTC](https://forum.ansible.com/t/seting-environment/26288/8 "2017-12-14T17:56:21Z")

</div>

thanks again kai and appreciate your help…will check it…

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [December 14, 2017, 6:20pm UTC](https://forum.ansible.com/t/seting-environment/26288/9 "2017-12-14T18:20:04Z")

</div>

- name: to execute the build  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;- name: exporting the variables  
&nbsp;&nbsp;&nbsp;&nbsp;include\_vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file: variable.yml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: myenvvars

&nbsp;&nbsp;- name: running the docker stack  
&nbsp;&nbsp;&nbsp;&nbsp;shell: docket stack -c some.yml ..  
&nbsp;&nbsp;&nbsp;&nbsp;environment: '{{myenvvars}}'

---

<div class="post-metadata">

**Author:** ![coach.rhca](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@coach.rhca](https://forum.ansible.com/u/coach.rhca)\
**Post date:** [December 15, 2017, 7:26am UTC](https://forum.ansible.com/t/seting-environment/26288/10 "2017-12-15T07:26:20Z")

</div>

Thanks a ton Brian for the help…

---

<div class="post-metadata">

**Author:** ![coach.rhca](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@coach.rhca](https://forum.ansible.com/u/coach.rhca)\
**Post date:** [December 21, 2017, 3:45am UTC](https://forum.ansible.com/t/seting-environment/26288/11 "2017-12-21T03:45:01Z")

</div>

sorry for coming back to this again…if we have multiple vars file will the logic be the same…for example …

include\_vars:  
file: {{item}} (rather than one file variable.yml)

**with\_items:**

**- variable1.yml**

**- variable2.yml**

**- variable3.yml**

name: myenvvars

- name: running the docker stack  
shell: docket stack -c some.yml …  
environment: ‘{{myenvvars}}’

please can you share the insight …

---

<div class="post-metadata">

**Author:** ![coach.rhca](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@coach.rhca](https://forum.ansible.com/u/coach.rhca)\
**Post date:** [December 22, 2017, 6:53am UTC](https://forum.ansible.com/t/seting-environment/26288/12 "2017-12-22T06:53:00Z")

</div>

Kai/Brian, could you please help on it…as above the files are variable1.yml, variable2.yml,variable3.yml…having the values as ptr\_image: [my.example.com/jobname/testname](http://my.example.com/jobname/testname)  
ver\_number: v99 and diferent values in different files but the variable name ( ptr\_image and ver\_number name remains the same in all files but values change) …is there a way that we can export the environment variable from file variable1.yml and run the docker stack -c some.yml and then take the variable2 file and export the environment variable again from this file and then again run docket stack -c some.yml and so on…( one variable file values to be export and subsequent ones)…

thanks

---

<div class="post-metadata">

**Author:** ![coach.rhca](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@coach.rhca](https://forum.ansible.com/u/coach.rhca)\
**Post date:** [December 22, 2017, 4:08pm UTC](https://forum.ansible.com/t/seting-environment/26288/13 "2017-12-22T16:08:11Z")

</div>

any thoughts on this…

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 31, 2018, 3:56am UTC](https://forum.ansible.com/t/seting-environment/26288/14 "2018-05-31T03:56:06Z")

</div>

yes, just you are indenting things wrong and name is a label, not the  
same as register:

- include\_vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file: {{item}} (rather than one file variable.yml)  
&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- variable1.yml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- variable2.yml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- variable3.yml  
&nbsp;&nbsp;&nbsp;&nbsp;register: myenvvars

---

<div class="post-metadata">

**Author:** ![coach.rhca](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@coach.rhca](https://forum.ansible.com/u/coach.rhca)\
**Post date:** [May 31, 2018, 10:24am UTC](https://forum.ansible.com/t/seting-environment/26288/15 "2018-05-31T10:24:36Z")

</div>

thank you Brian
