I cannot find a way to much a literal (a dot) in Ansible's regex_replace filter. Here is the task:
- name: Display database name
debug:
msg: "{{ vhost | regex_replace('(.+\.)(.+)$', \\1) }}"
tags: [debug]
My intention is to match and replace the whole URL like test.staging.domain.com with its first part (test in the example).
Ansible would report the following error:
debug:
msg: "{{ vhost | regex_replace('(.+\.)(.+)$', \\1) }}"
^ here
We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when they start a value.
How can I match literals in Ansible regexp_replace
filter?
It seems that "match" requires a pattern that matches the whole string, hence the *. before and after that string to be matched. Two separate "set_fact" stanzas are needed so that "keytype" is set before it is used. The code works in Ansible 2.4.3.0 running on Debian 9 (Raspbian "Stretch").
regex_replace or the replace filter can be used to replace data in a string or variable. The replace module is used to replace data in a file. In this example, Hello is replaced with Goodbye. - name: "replace 'Hello' with 'Goodbye'" debug: msg: " { { 'Hello World' | regex_replace ('Hello', 'Goodbye') }}"
In Ansible, when we need data manipulation, processing, formatting and conversion. We have a set of filters, jinja2 template filters and custom filters created by users.
By default Ansible requires values for all variables in a templated expression. However, you can make specific variables optional. For example, you might want to use a system default for some items and control the value for others. To make a variable optional, set the default value to the special variable omit:
it's actually possible to escape literals with double backlashes:
- name: Create database name and username
set_fact:
db_name: "{{ vhost | regex_replace('([^\\.]*)\\.(.+)$', '\\1') }}_stg"
The regexp above works correctly. The first capturing group extracts the first part of the URL until the first dot, the whole regex captures the whole URL. Passing test.staging.domain.com through it would produce just test.
There could be something whacky about escaping characters, but there's an escape-less way to code a literal dot:
[.]
So your regex could be written
(.+[.])(.+)$
Most characters lose their special meaning when in a character class, and the dot is one of them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With