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 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

  • 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

  • Maps to Python’s re.search.

Examples

# db => 'database42'
db: "{{ 'server1/database42' | regex_search('database[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.