# Syntax for using variable-item-string in when statement

**URL:** <https://forum.ansible.com/t/syntax-for-using-variable-item-string-in-when-statement/30655>\
**Category:** Ansible Project\
**Created:** [June 26, 2019, 4:38am UTC](https://forum.ansible.com/t/syntax-for-using-variable-item-string-in-when-statement/30655 "2019-06-26T04:38:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sahitansar](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@sahitansar](https://forum.ansible.com/u/sahitansar)\
**Post date:** [June 26, 2019, 4:38am UTC](https://forum.ansible.com/t/syntax-for-using-variable-item-string-in-when-statement/30655/1 "2019-06-26T04:38:13Z")

</div>

What’s the right way of using jinja2 in when statement .

I basically have a condition to check if something like below exists in stdout of perious results.

When :  
\_ ’ {{ var1 }}-{{ var2}}-{{ item }}-{{ var3 }} in prev\_results.stdout’

Jinja isn’t allowed in when.

The above variables are appended with hyphen and item value to generate the actual string .  
So whats the right syntax to use that.  
Kindly assist

---

<div class="post-metadata">

**Author:** ![sahitansar](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@sahitansar](https://forum.ansible.com/u/sahitansar)\
**Post date:** [June 26, 2019, 6:49am UTC](https://forum.ansible.com/t/syntax-for-using-variable-item-string-in-when-statement/30655/2 "2019-06-26T06:49:12Z")

</div>

I got this working.

When :  
&nbsp;&nbsp;- " var + '-'+var2 +'-'+item "

I have another problem now.

Please assist.

I have inventory variables like below :

[Grp1]  
Host1 dblist='["sar","Cor",...]  
Host2 dblist='["db1","DB2","db3",...]'

#dblist for every host is huge like some 20 items.

Now for every hostgroup I have group variables file under group\_vars dir

Example of Grp1.yml under griup\_vars dir

Cat Grp1.yml

App\_attr:  
&nbsp;&nbsp;  
&nbsp;&nbsp;- attr : "varname"  
&nbsp;&nbsp;&nbsp;&nbsp;attrval : "str"  
&nbsp;&nbsp;&nbsp;&nbsp;attrtype : "string"

&nbsp;&nbsp;- attr : "secname"  
&nbsp;&nbsp;&nbsp;&nbsp;attrval : "DB"  
&nbsp;&nbsp;&nbsp;&nbsp;attrtype : "typing"

&nbsp;&nbsp;- .  
&nbsp;&nbsp;&nbsp;&nbsp;.  
&nbsp;&nbsp;&nbsp;&nbsp;.  
Etc  
Same like above keys n diff values

In one of my task I need to access both inventory host list variable dblist and group variables App\_attr

Name : access both of them.  
Command : myscript {{ item.O }}-{{ inventory\_hostname }} --appname {{ item.1.attr }} --appval {{ item.1.attrval }} --apptype {{ item.1.attrtype }}

With\_items:  
&nbsp;&nbsp;&nbsp;- "{{ dblist }}"  
&nbsp;&nbsp;&nbsp;- "{{ App\_atrr }}"

I'm getting error.  
Dict object has no element 1.

Looks like App\_attr is not parsed properly. What's the right way to pass it.  
Please suggest
