How to insert value of variable in conditional statement in Playbook

50 views
Skip to first unread message

Rakesh Parida

unread,
Jul 2, 2019, 10:58:06 AM7/2/19
to Ansible Project
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 }}

Matt Martz

unread,
Jul 2, 2019, 11:00:50 AM7/2/19
to ansible...@googlegroups.com
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.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/21685cee-f595-4e88-b2bd-ce6da0c14de2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Rakesh Parida

unread,
Jul 2, 2019, 11:11:14 AM7/2/19
to Ansible Project
Hi Mat,

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


On Tuesday, July 2, 2019 at 8:30:50 PM UTC+5:30, Matt Martz wrote:
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.

On Tue, Jul 2, 2019 at 9:58 AM Rakesh Parida <rakeshp...@gmail.com> wrote:
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 }}

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Rakesh Parida

unread,
Jul 3, 2019, 9:02:53 AM7/3/19
to Ansible Project
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"

Rakesh Parida

unread,
Jul 3, 2019, 9:07:00 AM7/3/19
to Ansible Project
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

Rakesh Parida

unread,
Jul 4, 2019, 11:11:31 PM7/4/19
to Ansible Project
Hi ,,can anyone help me debug my problem.

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages