How to insert value of variable in conditional statement in Playbook

How can i insert value of variables using with items in conditional checks

name: system_ipaddress_mac_combinations present inside fact
debug:
msg : The system_ip-MAC combinations “{{ item }}” is present in teh fact
when: ‘{{ item }} in {{ csv_ip_mac_details }}’
with_items:

  • “{{ system_ip_mac_details }}”

TASK [system_ipaddress_mac_combinations present inside fact] ***********************************************************************************************************
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ item }} in {{ csv_ip_mac_details }}

when statements are already implicitly wrapped in jinja2 templating.

Effectively you just need:

when: item in csv_ip_mac_details

As simple as removing the {{ }} jinja2 delimiters.

Hi Mat,

Thanks for the suggestion.
That worked for me as a charm :slight_smile:

Hi Matt,

I am facing one more issue here, iam providing the conditional statemt in my playbook, but both are executing even if the conditions is true or false.

  • name: System_ipaddress_mac_combinations present inside CSV_IP_MAC_DETAILS fact
    debug:
    msg : The system_ip-MAC combinations “{{ item }}” is present in the CSV_IP_MAC_DETAILS fact, If present it will be skipped and VALIDATION SUCCESS
    when: item.find(“csv_ip_mac_details”) != -1
    with_items:

  • “{{ system_ip_mac_details }}”

  • name: System_ipaddress_mac_combinations not present inside CSV_IP_MAC_DETAILS fact
    debug:
    msg : The system_ip-MAC combinations “{{ item }}” is not present in the CSV_IP_MAC_DETAILS fact So VALIDATION FAILED
    when: item.find(“csv_ip_mac_details”) == -1
    with_items:

  • “{{ system_ip_mac_details }}”

TASK [Interface-Mac-Validation : System_ipaddress_mac_combinations present inside CSV_IP_MAC_DETAILS fact] **************************
skipping: [BVI15CA2] => (item= 10.174.206.157: 02:15:0a:ae:ce:9d 10.145.19.4: 02:15:0a:91:13:04 172.16.7.52: 02:15:ac:10:07:34 )
skipping: [BVI15CA2]

TASK [Interface-Mac-Validation : System_ipaddress_mac_combinations not present inside CSV_IP_MAC_DETAILS fact] **********************
ok: [BVI15CA2] => (item= 10.174.206.157: 02:15:0a:ae:ce:9d 10.145.19.4: 02:15:0a:91:13:04 172.16.7.52: 02:15:ac:10:07:34 ) => {
“msg”: “The system_ip-MAC combinations " 10.174.206.157: 02:15:0a:ae:ce:9d 10.145.19.4: 02:15:0a:91:13:04 172.16.7.52: 02:15:ac:10:07:34 " is not present in the CSV_IP_MAC_DETAILS fact So VALIDATION FAILED”
}

No Matter if “system_ip_mac_details” value is present or not in the “csv_ip_mac_details” am getting the same output every time.
Pls suggest

Hi ,can anyone help me debug my problem.