10 examples for Ansible regex_search

You can use the Ansible regex_search filter to find patterns in strings and extract matching content.

Quick Index:

Ansible regex_search example playbook

Here’s a basic example of how you can use it in an Ansible playbook:

- name: Example playbook with regex_search
  hosts: localhost
  gather_facts: false
    - name: Extract matching content from a string
        matched_content: "{{ my_string | regex_search(my_pattern) }}"
        my_string: "This is a codetryout example with 7777 and some other text."
        my_pattern: '(\d+)'

    - name: Display the matched content
        var: matched_content

In this example:

  1. We define an Ansible playbook with a single task.
  2. In the task, we use the set_fact module to set the variable matched_content to the result of the regex_search filter.
  3. We specify the my_string variable, which contains the string you want to search within.
  4. We specify the my_pattern variable, which contains the regular expression pattern you want to search for. In this case, we’re searching for one or more digits.
  5. Finally, we use the debug module to display the matched content.

When you run this playbook, Ansible will search for the regular expression pattern in the provided string (my_string) and store the matched content in the matched_content variable. In this example, it will extract the digits “7777” from the string and display them.

You can customize the my_string and my_pattern variables to match your specific use case and regular expression requirements.

10 regex_search examples

Here are 10 Ansible examples that demonstrate how to use the regex search filter for various tasks:

  1. Extract IP Address from a String:
- set_fact:
    ip_address: "{{ my_string | regex_search('\\d+\\.\\d+\\.\\d+\\.\\d+') }}"
    my_string: "Server IP:"
  1. Extract Email Addresses from a Text:
- set_fact:
    email_addresses: "{{ my_text | regex_search('\\S+@\\S+') }}"
    my_text: "Emails: [email protected], [email protected]"
  1. Extract Numbers from a String:
- set_fact:
    numbers: "{{ my_string | regex_search('\\d+') }}"
    my_string: "There are 123 servers on this cluster"
  1. Extract URLs from Text:
- set_fact:
    urls: "{{ my_text | regex_search('https?://\\S+') }}"
    my_text: "Visit our website at https://codetryout.com."
  1. Extract Domain Names from a List:
- set_fact:
    domains: "{{ my_list | map('regex_search', 'https?://(\\S+)') | select('defined') | map('extract', 1) | list }}"
      - "https://codetryout.com"
      - "https://example.com"
  1. Extract Words Starting with ‘A’:
- set_fact:
    words_with_a: "{{ my_text | regex_search('\\bA\\w*') }}"
    my_text: "Ansible is awesome and so are Python!"
  1. Extract File Extensions from File Names:
- set_fact:
    extensions: "{{ my_filenames | map('regex_search', '\\.([^\\.]+)$') | select('defined') | map('extract', 1) | list }}"
      - "reports.pdf"
      - "logo.jpg"
  1. Extract Hexadecimal Colors from CSS:
- set_fact:
    colors: "{{ css_code | regex_search('#[0-9A-Fa-f]{6}') }}"
    css_code: "body { background-color: #FFFFFF; color: #00FF00; }"
  1. Extract Words in Parentheses:
- set_fact:
    words_in_parentheses: "{{ my_text | regex_search('\\(([^)]+)\\)') }}"
    my_text: "The (quick) brown (fox) jumps (over) the lazy dog."
  1. Extract First and Last Name from Full Name:
- set_fact:
    first_name: "{{ full_name | regex_search('^\\w+') }}"
    last_name: "{{ full_name | regex_search('\\w+$') }}"
    full_name: "Code Tryout"

These examples cover a range of use cases for extracting specific content using regular expressions in Ansible. You can modify the regular expressions to match your specific requirements.