'when: item not in' conditional issues?

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

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:

  when: "( '/dev/' + item.key ) not in ansible_lvm.pvs"

Regards
        Racke

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