Jinja2 to filter paths

18 views
Skip to first unread message

Ashok

unread,
Mar 14, 2019, 9:12:30 AM3/14/19
to Ansible Project
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.

Kai Stian Olstad

unread,
Mar 14, 2019, 9:39:47 AM3/14/19
to ansible...@googlegroups.com
There are many way to do this, one of them is

- debug: msg="{{ item | regex_replace('^/logs/(.*)\.log$', '\\1') |
regex_replace('/', '_') }}"
with_items:
- /logs/other/dyn.log
- /logs/mx/mail.log
- /logs/apache-servers/other.log
- /logs/network/nas01.log
- /logs/mailouts/highvolume/mailout.log

--
Kai Stian Olstad

Sabiul Islam

unread,
Mar 19, 2019, 5:47:48 AM3/19/19
to Ansible Project
Reply all
Reply to author
Forward
0 new messages