Introduction

Ansible is a powerful automation tool that allows you to manage multiple servers from a single machine. It can perform tasks such as provisioning, deployment, and configuration management. However, like any tool, Ansible can encounter errors and issues that can cause frustration for users. One such error is the “Invalid plugin name: regex.replace” error, which can occur when using the regex.replace plugin in an Ansible playbook or task.

What causes the error?

The “Invalid plugin name: regex.replace” error occurs when the regex.replace plugin is not installed or is not loaded correctly. The regex.replace plugin is part of the ansible.builtin collection, so if this collection is not installed on the machine running Ansible, the plugin will not be available. Another possible cause is that the Ansible version being used is outdated and does not support the regex.replace plugin.

How to troubleshoot the error?

To troubleshoot the “Invalid plugin name: regex.replace” error, you can follow these steps:

Step 1: Update Ansible to the latest version

One of the reasons may be that the version of Ansible being used does not support the regex.replace plugin. To update Ansible to the latest version, run the following command:

pip install ansible --upgrade

Step 2: Check the syntax of the playbook or task

If the error still occurs after updating Ansible, check the syntax of the playbook or task where the error occurs. Please note that till Ansible 2.9 use the regex.replace plugin, whereas the newest version of Ansible releases uses the ansible.builtin.regex_replace filter name.

Step 3: Check for conflicting plugins

If none of the above steps work, the error may be due to a conflict with another plugin. Check if any other plugins are being used that may be causing a conflict with the regex.replace plugin or the new name ansible.builtin.regex_replace. You can disable other plugins temporarily to see if the error goes away.

Join 50+ hours of courses in our exclusive community

Demo

This is an Ansible playbook that Playbooknstrates the use of the regex.replace plugin to remove HTML tags from a string. The playbook sets a variable named “example” to the string “<HTML>example”, and then uses the regex.replace plugin to remove any HTML tags from the string.

The playbook is executed on all hosts, and the resulting string is printed to the console using the debug module.

Overall, this is a simple example of how to use the regex.replace plugin in Ansible. However, if the plugin is not loaded correctly, you may encounter the “Invalid plugin name: regex.replace” error when running this playbook.

error code

  • regex_error.yml
---
- name: Regex Playbook
  hosts: all
  vars:
    example: '<HTML>example'
  tasks:
    - name: Regex
      ansible.builtin.debug:
        msg: "{{ example | regex.replace('<.*?>') }}"

error execution

ansible-playbook -i inventory regex_error.yml

PLAY [Regex Playbook] *********************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************
ok: [localhost]

TASK [Regex] **************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "template error while templating string: Could not load \"regex.replace\": 'invalid plugin name: regex.replace'. String: {{ example | regex.replace('<.*?>') }}. Could not load \"regex.replace\": 'invalid plugin name: regex.replace'"}

PLAY RECAP ****************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

fix code

---
- name: Regex Playbook
  hosts: all
  vars:
    example: '<HTML>example'
  tasks:
    - name: Regex
      ansible.builtin.debug:
        msg: "{{ example | ansible.builtin.regex_replace('<.*?>') }}"

fix execution

ansible-playbook -i inventory regex_fix.yml

PLAY [Regex Playbook] *********************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************
ok: [localhost]

TASK [Regex] **************************************************************************************************************************************
ok: [localhost] => {
    "msg": "example"
}

PLAY RECAP ****************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Conclusion

The “Invalid plugin name: regex.replace” error can be frustrating to encounter, but by following the troubleshooting steps outlined above, you can quickly identify and resolve the issue. It is important to keep Ansible and its dependencies up-to-date to ensure smooth operation and to check the syntax of your playbooks and tasks for any errors that may cause issues like this.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza