# Using when module with variables/template

**URL:** <https://forum.ansible.com/t/using-when-module-with-variables-template/34132>\
**Category:** Ansible Project\
**Created:** [October 30, 2020, 10:24am UTC](https://forum.ansible.com/t/using-when-module-with-variables-template/34132 "2020-10-30T10:24:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pravin\_V](https://avatars.discourse-cdn.com/v4/letter/p/c68b51/32.png) [@Pravin\_V](https://forum.ansible.com/u/Pravin_V)\
**Post date:** [October 30, 2020, 10:24am UTC](https://forum.ansible.com/t/using-when-module-with-variables-template/34132/1 "2020-10-30T10:24:33Z")

</div>

Hello Everyone,

I have a playbook

- name: Test  
uri:  
url: [https://192.168.1.1](https://192.168.1.1)  
method: POST  
body:  
“IPADDRESS={{ item.address }}”  
body\_format: “form-urlencoded”  
when: item.device\_type.model == “2921” or  
item.device\_type.model == “3931”

But this way list is going very big.  
Now I want to create some sort of dictionary and put the name of that to use with when like below:  
vars:

- ciscorouters:
- 2921
- 3931

when: item.device\_type.model == {{ ciscorouters }}

Please let me know the procedure.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 30, 2020, 10:40am UTC](https://forum.ansible.com/t/using-when-module-with-variables-template/34132/2 "2020-10-30T10:40:02Z")

</div>

> Hello Everyone,
> 
> I have a playbook  
> - name: Test  
> uri:  
> url: [https://192.168.1.1](https://192.168.1.1)  
> method: POST  
> body:  
> "IPADDRESS={{ item.address }}"  
> body\_format: "form-urlencoded"  
> when: item.device\_type.model == "2921" or  
> item.device\_type.model == "3931"
> 
> But this way list is going very big.  
> Now I want to create some sort of dictionary and put the name of that to use with when like below:  
> vars:  
> - ciscorouters:  
> - 2921  
> - 3931
> 
> when: item.device\_type.model == {{ ciscorouters }}
> 
> Please let me know the procedure.

1. You can't use {{ }} in when conditions. The condition is already virtually wrapped in {{ }}

2. Try the following:

&nbsp;&nbsp;&nbsp;when: item.device\_type.model in ciscorouters

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Pravin\_V](https://avatars.discourse-cdn.com/v4/letter/p/c68b51/32.png) [@Pravin\_V](https://forum.ansible.com/u/Pravin_V)\
**Post date:** [October 30, 2020, 11:01am UTC](https://forum.ansible.com/t/using-when-module-with-variables-template/34132/3 "2020-10-30T11:01:20Z")

</div>

It just worked like a charm.  
Thank you so much. I really appreciate.
