# variable names of variables holding environment variables :)

**URL:** <https://forum.ansible.com/t/variable-names-of-variables-holding-environment-variables/15207>\
**Category:** Ansible Project\
**Created:** [May 10, 2014, 11:10pm UTC](https://forum.ansible.com/t/variable-names-of-variables-holding-environment-variables/15207 "2014-05-10T23:10:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [May 10, 2014, 11:10pm UTC](https://forum.ansible.com/t/variable-names-of-variables-holding-environment-variables/15207/1 "2014-05-10T23:10:24Z")

</div>

I know subject sounds kind of crazy, but here’s the code to explain it a bit:

- hosts: all  
vars:
- djbdns\_var:  
FOO: “DJ DNS”
- vnstat\_var:  
FOO: “VN, stat!”  
tasks:
- name: Test flexible vars  
command: echo “$FOO”  
environment: “{{ item }}\_var”  
with\_items:
- djbdns
- vnstat

which should be equivalent to something like:

tasks:

- name: Test flexible vars (step 1)  
command: echo “$FOO”  
environment: djbdns\_var"

- name: Test flexible vars (step 2)  
command: echo “$FOO”  
environment: vnstat\_var"

so what I’m trying to do is define different set of environment variables for each step of iteration, and the best way I figured was something like above. However it doesn’t work. Ansible (1.5.3) recognizes passed argument as a string and throws an error rather than interpret resulting variable name.

Question is: can something be done to accomplish what I’m trying to do?

---

<div class="post-metadata">

**Author:** ![mhael](https://avatars.discourse-cdn.com/v4/letter/m/919ad9/32.png) [@mhael](https://forum.ansible.com/u/mhael)\
**Post date:** [May 11, 2014, 9:56pm UTC](https://forum.ansible.com/t/variable-names-of-variables-holding-environment-variables/15207/2 "2014-05-11T21:56:54Z")

</div>

It depends on context of your actual task. Adding top-level hash will do if you can put definitions for all \*\_var data in a single unit.  
For multiple units (f.e. per-role definitions) you may try to set “hash\_behaviour=merge” in ansible.cfg, though this is generally not recommended.

`

- hosts: localhost  
connection: local  
vars:
- cmd\_envs:  
djbdns: { FOO: “DJ DNS” }  
vnstat: { FOO: “VN, stat!” }  
tasks:
- name: Test flexible vars  
command: echo “$FOO”  
environment: cmd\_envs[item]  
with\_items:
- djbdns
- vnstat

`

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [May 12, 2014, 7:01pm UTC](https://forum.ansible.com/t/variable-names-of-variables-holding-environment-variables/15207/3 "2014-05-12T19:01:23Z")

</div>

that sounds like something I need. Thanks!

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [May 13, 2014, 4:18am UTC](https://forum.ansible.com/t/variable-names-of-variables-holding-environment-variables/15207/4 "2014-05-13T04:18:40Z")

</div>

after tinkering with above idea I’ve got things working exactly how I wanted. Thanks for the tip!
