ansible.builtin.regex_search filter – extract regex match from string

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

Synopsis

  • Search in a string to extract the part that matches the regular expression.

Input

This describes the input of the filter, the value before | ansible.builtin.regex_search.

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_search(positional1, positional2, ...)

Parameter

Comments

_regex

string

Regular expression string that defines the match.

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_search(key1=value1, key2=value2, ...)

Parameter

Comments

ignorecase

boolean

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

Choices:

  • false ← (default)

  • true

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_search(positional1, positional2, key1=value1, key2=value2)

  • Maps to Python’s re.search.

Examples

# db => 'database42'
db: "{{ 'server1/database42' | regex_search('database[0-9]+') }}"

# 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
# server => 'sErver1'
db: "{{ 'sErver1/database42' | regex_search('(?i)server([0-9]+)') }}"

# drinkat => 'BAR'
drinkat: "{{ 'foo\nBAR' | regex_search('^bar', multiline=True, ignorecase=True) }}"

Return Value

Key

Description

Return value

string

Matched string or empty string 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.