# item as part of a variable

**URL:** <https://forum.ansible.com/t/item-as-part-of-a-variable/27288>\
**Category:** Ansible Project\
**Created:** [April 16, 2018, 1:46pm UTC](https://forum.ansible.com/t/item-as-part-of-a-variable/27288 "2018-04-16T13:46:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Renner](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Michael\_Renner](https://forum.ansible.com/u/Michael_Renner)\
**Post date:** [April 16, 2018, 1:46pm UTC](https://forum.ansible.com/t/item-as-part-of-a-variable/27288/1 "2018-04-16T13:46:47Z")

</div>

Moin,

I’m calling a playbook (ansible 2.4.2.0) with extra-vars for 3 system components, e.g. “foo\_version=0.11.3 bar\_version=0.10.2 baz\_version=0.12.1 env=dev”.

There is a statement In the playbook that takes care that this step is only executed if the corresponding variable is defined: “{{ item }}\_version”.

- name: copy war file  
copy:  
src: “{{ tmpdir.path }}/{{ item }}.war”  
dest: /opt/tomcat/webapps/  
owner: tomcat  
group: root  
mode: 0440  
when:
- “{{ item }}\_version” is defined  
with\_items:
- foo
- bar
- baz

But this fail:

The offending line appears to be:

- “{{ item }}\_version” is defined  
^ here  
We could be wrong, but this one looks like it might be an issue with  
missing quotes.

How can I check if the variable is set?

Thanks

---

<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:** [April 16, 2018, 2:36pm UTC](https://forum.ansible.com/t/item-as-part-of-a-variable/27288/2 "2018-04-16T14:36:08Z")

</div>

That won't work, try:

&nbsp;&nbsp;- name: copy war file  
&nbsp;&nbsp;&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ tmpdir.path }}/{{ item }}.war"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /opt/tomcat/webapps/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner: tomcat  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: root  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode: 0440  
&nbsp;&nbsp;&nbsp;&nbsp;when: item + "\_version" in hostvars[inventory\_hostname]  
&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- bar  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- baz

---

<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 16, 2018, 2:37pm UTC](https://forum.ansible.com/t/item-as-part-of-a-variable/27288/3 "2018-04-16T14:37:18Z")

</div>

You will need to construct a key, and reference the value through hostvars like:

when:

- hostvars[inventory\_hostname][item ~ “\_version”] is defined
