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. However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.regex_replace for easy linking to the plugin documentation and to avoid conflicting with other collections that may have the same filter plugin name.

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

Input

string / required

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

_regex_match

integer / required

Regular expression string that defines the match.

_regex_replace

integer / required

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

count

integer

added in ansible-core 2.17

Maximum number of pattern occurrences to replace. If zero, replace all occurrences.

Default: 0

ignorecase

boolean

Force the search to be case insensitive if True, case sensitive otherwise.

Choices:

  • false ← (default)

  • true

mandatory_count

integer

added in ansible-core 2.17

Except a certain number of replacements. Raises an error otherwise. If zero, ignore.

Default: 0

multiline

boolean

Search across line endings if True, do not if otherwise.

Choices:

  • false ← (default)

  • true

Notes

Note

  • When keyword and positional parameters are used together, positional parameters must be listed before keyword parameters: input | ansible.builtin.regex_replace(positional1, positional2, key1=value1, key2=value2)

  • Maps to Python’s re.sub.

  • The substring matched by the group is accessible via the symbolic group name or the ``\{number}`` special sequence. See examples section.

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) }}"

# Using inline regex flags instead of passing options to filter
# See https://docs.python.org/3/library/re.html for more information
# on inline regex flags
# piratecomment => '#CAR\n#tar\nfoo\n#bar\n'
piratecomment: "{{ 'CAR\ntar\nfoo\nbar\n' | regex_replace('(?im)^(.ar)$', '#\\1') }}"

# 'foo=bar=baz' => 'foo:bar=baz'
key_value: "{{ 'foo=bar=baz' | regex_replace('=', ':', count=1) }}"

Return Value

Key

Description

Return value

string

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.