ansible.builtin.regex_findall filter – extract all regex matches 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_findall. However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.regex_findall 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 or extract all the parts of a string matching a regular expression.

Input

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

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

Examples

# all_pirates => ['CAR', 'tar', 'bar']
all_pirates: "{{ 'CAR\ntar\nfoo\nbar\n' | regex_findall('^.ar$', 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
# all_pirates => ['CAR', 'tar', 'bar']
all_pirates: "{{ 'CAR\ntar\nfoo\nbar\n' | regex_findall('(?im)^.ar$') }}"

# get_ips => ['8.8.8.8', '8.8.4.4']
get_ips: "{{ 'Some DNS servers are 8.8.8.8 and 8.8.4.4' | regex_findall('\\b(?:[0-9]{1,3}\\.){3}[0-9]{1,3}\\b') }}"

Return Value

Key

Description

Return value

list / elements=string

List of matched strings.

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.