# dictionary key as variable

**URL:** <https://forum.ansible.com/t/dictionary-key-as-variable/21311>\
**Category:** Ansible Project\
**Created:** [April 29, 2016, 9:12am UTC](https://forum.ansible.com/t/dictionary-key-as-variable/21311 "2016-04-29T09:12:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serkan\_C](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Serkan\_C](https://forum.ansible.com/u/Serkan_C)\
**Post date:** [April 29, 2016, 9:12am UTC](https://forum.ansible.com/t/dictionary-key-as-variable/21311/1 "2016-04-29T09:12:20Z")

</div>

Hi,  
I’m developing a generic tomcat role, it gets version number as variable;  
…

roles:

- { role: tomcat, version: “8.0.33” }

I’ve mentioned download urls for some major versions at vars.yml

…

file:

“8”: “…apache-tomcat-8.0.33.tar.gz”

“7”: “…apache-tomcat-7.0.33.tar.gz”

I would like to use this dictionary at role main.yml for given version number;

Question is, how can I use external variable as dictionary key in role task.

I tried those;

- name: copy tomcat to server

copy: src=files/{{file[{{version}}]}} dest=/tmp/

- name: copy tomcat to server

copy: src=files/{{file.version}} dest=/tmp/

- name: copy tomcat to server

copy: src=files/{{file}}.{{version}} dest=/tmp/

Thank you.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [April 29, 2016, 11:28am UTC](https://forum.ansible.com/t/dictionary-key-as-variable/21311/2 "2016-04-29T11:28:31Z")

</div>

You cannot nest {{ }}, instead you want:

{{ file[version] }}

---

<div class="post-metadata">

**Author:** ![Serkan\_C](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Serkan\_C](https://forum.ansible.com/u/Serkan_C)\
**Post date:** [April 30, 2016, 6:36am UTC](https://forum.ansible.com/t/dictionary-key-as-variable/21311/3 "2016-04-30T06:36:06Z")

</div>

Thanks Matt, it works.

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [May 3, 2016, 4:44am UTC](https://forum.ansible.com/t/dictionary-key-as-variable/21311/4 "2016-05-03T04:44:48Z")

</div>

You might want to rename your variable (file) so it does not have the  
same name as a existing module. Just to avoid trouble...

Johannes
