# Trouble comparing Jinja2 string

**URL:** <https://forum.ansible.com/t/trouble-comparing-jinja2-string/29919>\
**Category:** Ansible Project\
**Created:** [March 20, 2019, 8:23am UTC](https://forum.ansible.com/t/trouble-comparing-jinja2-string/29919 "2019-03-20T08:23:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashok1](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@Ashok1](https://forum.ansible.com/u/Ashok1)\
**Post date:** [March 20, 2019, 8:23am UTC](https://forum.ansible.com/t/trouble-comparing-jinja2-string/29919/1 "2019-03-20T08:23:10Z")

</div>

Hi all…

I have the below vars:

`  
client\_destinations:  
apache-servers:  
pre\_cmd: ‘$template custom,“/logs/apache-servers/%source%.log”’  
rules:

- var: ‘$syslogfacility-text’  
operator: ‘contains’  
search: ‘local5’  
destination: ‘-?custom’  
path: ‘/logs/apache-servers/\*.log’
- var: ‘$syslogtag’  
search: ‘apache-access:’  
operator: ‘contains’  
destination: ‘/logs/apache-servers/access.log’
- var: ‘$syslogtag’  
search: ‘apache-error:’  
operator: ‘contains’  
destination: ‘/logs/apache-servers/error.log’
- destination: ‘/logs/apache-servers/other.log’

`

I have the below Jinja2 code **embedded in a playbook** :

`

- name: Create new log dest  
set\_fact:  
log\_dest2: |  
[  
{% for group in client\_destinations.keys() %}  
{% set group\_dest = client\_destinations[group] %}  
{% for dest in group\_dest.rules %}  
{% if dest.destination matches ‘^-?’ %}  
‘{{ dest.path }}’,  
{% else %}  
‘{{ dest.destination }}’,  
{% endif %}  
{% endfor %}  
{% endfor %}  
]  
`

WIth **“if”** statement in the Jinja2 code, I’m trying to match any `destination` (in above vars) that **begins** with **-?**. I’ve tried the operator **matches** and **starts with**. Nothing works.

( What works is only this - **{% if dest.destination == ‘-?custom’ %}** )

How do I use regex here to achieve the match?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [March 20, 2019, 8:38am UTC](https://forum.ansible.com/t/trouble-comparing-jinja2-string/29919/2 "2019-03-20T08:38:26Z")

</div>

Hi,

Did you try match (not matches) ? Be careful you need brackets

[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_tests.html](https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html)

Regards,

JYL
