Using when module with variables/template

Hello Everyone,

I have a playbook

  • name: Test
    uri:
    url: 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.

Hello Everyone,

I have a playbook
- name: Test
uri:
url: 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:

   when: item.device_type.model in ciscorouters

Regards
        Racke

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