# 'when: item not in' conditional issues?

**URL:** <https://forum.ansible.com/t/when-item-not-in-conditional-issues/33163>\
**Category:** Ansible Project\
**Created:** [June 12, 2020, 1:34pm UTC](https://forum.ansible.com/t/when-item-not-in-conditional-issues/33163 "2020-06-12T13:34:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Doug\_OLeary](https://avatars.discourse-cdn.com/v4/letter/d/c0e974/32.png) [@Doug\_OLeary](https://forum.ansible.com/u/Doug_OLeary)\
**Post date:** [June 12, 2020, 1:34pm UTC](https://forum.ansible.com/t/when-item-not-in-conditional-issues/33163/1 "2020-06-12T13:34:30Z")

</div>

Hey;

I’m trying to execute pvcreate on a disk but would like to ensure I don’t run it on a disk that’s already defined. I have a short playbook that gathers facts, displays ansible\_lvm.pvs.keys()  
and then:

`

- name: run pvcreate  
command: pvcreate /dev/{{item.key}}  
when: item.key is not in ansible\_lvm.pvs.keys()  
with\_dict: “{{disks}}”  
`

That particular one resulted in:

The error was: template error while templating string: expected token ‘end of statement block’, got ‘.’.

Even if it didn’t error out, I suspect it wouldn’t work because the keys are:

`"msg": "dict_keys(['/dev/sdd', '/dev/sdb1', '/dev/sdc', '/dev/sda2'])"`

I’ve also tried “when: /dev/item.key is not in…” and 'when: “/dev/item.key” is not in…" Those two result in:

The error was: template error while templating string: unexpected ‘/’.

Any hints on how to verify “/dev/{{item.key}” is not in the pvs dictionary?

Thanks

Doug O’Leary

---

<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:** [June 12, 2020, 1:42pm UTC](https://forum.ansible.com/t/when-item-not-in-conditional-issues/33163/2 "2020-06-12T13:42:26Z")

</div>

> Hey;
> 
> I'm trying to execute pvcreate on a disk but would like to ensure I don't run it on a disk that's already defined. I  
> have a short playbook that gathers facts, displays ansible\_lvm.pvs.keys()  
> and then:
> 
> \>  
> -name:run pvcreate  
> command:pvcreate /dev/{{item.key}}  
> when:item.key isnotinansible\_lvm.pvs.keys()  
> with\_dict:"{{disks}}"  
> \>
> 
> That particular one resulted in:
> 
> The error was: template error while templating string: expected token 'end of statement block', got '.'.
> 
> Even if it didn't error out, I suspect it wouldn't work because the keys are:
> 
> \>  
> "msg":"dict\_keys(['/dev/sdd', '/dev/sdb1', '/dev/sdc', '/dev/sda2'])"  
> \>
> 
> I've also tried "when: /dev/item.key is not in..." and 'when: "/dev/item.key" is not in..." Those two result in:
> 
> The error was: template error while templating string: unexpected '/'.
> 
> Any hints on how to verify "/dev/{{item.key}" is not in the pvs dictionary?

Please try:

&nbsp;&nbsp;when: "( '/dev/' + item.key ) not in ansible\_lvm.pvs"

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

---

<div class="post-metadata">

**Author:** ![Doug\_OLeary](https://avatars.discourse-cdn.com/v4/letter/d/c0e974/32.png) [@Doug\_OLeary](https://forum.ansible.com/u/Doug_OLeary)\
**Post date:** [June 12, 2020, 1:56pm UTC](https://forum.ansible.com/t/when-item-not-in-conditional-issues/33163/3 "2020-06-12T13:56:27Z")

</div>

That did the trick, sir, thank you!

I didn’t see that syntax anywhere in my searching. I would’ve spent much longer on that. Thank you again. I appreciate it.

Doug O’Leary
