ansible.builtin.regex_replace filter – replace a string via regex
Note
This filter plugin is part of ansible-core
and included in all Ansible
installations. In most cases, you can use the short
plugin name
regex_replace
even without specifying the collections:
keyword.
However, we recommend you use the FQCN for easy linking to the
plugin documentation and to avoid conflicting with other collections that may have
the same filter plugin name.
New in Ansible 2.0
Synopsis
Replace a substring defined by a regular expression with another defined by another regular expression based on the first match.
Input
This describes the input of the filter, the value before | ansible.builtin.regex_replace
.
Parameter |
Comments |
---|---|
String to match against. |
Positional parameters
This describes positional parameters of the filter. These are the values positional1
, positional2
and so on in the following example: input | ansible.builtin.regex_replace(positional1, positional2, ...)
.
Parameter |
Comments |
---|---|
Regular expression string that defines the match. |
|
Regular expression string that defines the replacement. |
Keyword parameters
This describes keyword parameters of the filter. These are the values key1=value1
, key2=value2
and so on in the following example: input | ansible.builtin.regex_replace(key1=value1, key2=value2, ...)
.
Parameter |
Comments |
---|---|
Force the search to be case insensitive if Choices:
|
|
Search across line endings if Choices:
|
Notes
Note
Maps to Python’s
re.replace
.
Examples
# whatami => 'able'
whatami: "{{ 'ansible' | regex_replace('^a.*i(.*)$', 'a\\1') }}"
# commalocal => 'localhost, 80'
commalocal: "{{ 'localhost:80' | regex_replace('^(?P<host>.+):(?P<port>\\d+)$', '\\g<host>, \\g<port>') }}"
# piratecomment => '#CAR\n#tar\nfoo\n#bar\n'
piratecomment: "{{ 'CAR\ntar\nfoo\nbar\n' | regex_replace('^(.ar)$', '#\\1', multiline=True, ignorecase=True) }}"
Return Value
Key |
Description |
---|---|
String with substitution (or original if no match). Returned: success |
Hint
Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.