# Jinja2 to filter paths

**URL:** <https://forum.ansible.com/t/jinja2-to-filter-paths/29886>\
**Category:** Ansible Project\
**Created:** [March 14, 2019, 1:12pm UTC](https://forum.ansible.com/t/jinja2-to-filter-paths/29886 "2019-03-14T13:12:30Z")\
**Posts on this page:** 3\
**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 14, 2019, 1:12pm UTC](https://forum.ansible.com/t/jinja2-to-filter-paths/29886/1 "2019-03-14T13:12:30Z")

</div>

Hi,

I’ve random paths like:

/logs/other/dyn.log  
/logs/mx/mail.log  
/logs/apache-servers/other.log  
/logs/network/nas01.log  
/logs/mailouts/highvolume/mailout.log

and many more. They are part of a `list` that is iterated over a loop. /logs/ will be common to all paths. I need to filter each item in the list so that, for eg, the last path looks like:

mailouts\_highvolume\_mailout

i.e. remove all forward slashes, logs and ‘.log’ and concatenate rest with an underscore.

Thank you.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 14, 2019, 1:39pm UTC](https://forum.ansible.com/t/jinja2-to-filter-paths/29886/2 "2019-03-14T13:39:36Z")

</div>

There are many way to do this, one of them is

&nbsp;&nbsp;&nbsp;- debug: msg="{{ item | regex\_replace('^/logs/(.\*)\.log$', '\\1') | regex\_replace('/', '\_') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- /logs/other/dyn.log  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- /logs/mx/mail.log  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- /logs/apache-servers/other.log  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- /logs/network/nas01.log  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- /logs/mailouts/highvolume/mailout.log

---

<div class="post-metadata">

**Author:** ![Sabiul\_Islam](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@Sabiul\_Islam](https://forum.ansible.com/u/Sabiul_Islam)\
**Post date:** [March 19, 2019, 9:47am UTC](https://forum.ansible.com/t/jinja2-to-filter-paths/29886/3 "2019-03-19T09:47:47Z")

</div>


