Bootstrap FreeKB - Ansible - regular expression regex_replace
Ansible - regular expression regex_replace

Updated:   |  Ansible articles

regex_replace or the replace filter can be used to replace data in a string, variable or list. The replace module is used to replace data in a file.

In this example, Hello is replaced with Goodbye.

---
- hosts: localhost
  tasks:
  - name: replace 'Hello' with 'Goodbye'
    ansible.builtin.debug:
      msg: "{{ 'Hello World' | regex_replace('Hello', 'Goodbye') }}"
...

 

Which should return the following.

ok: [server1.example.com] => {
    "msg": "Goodbye World"
}

 

Let's say a variable named "foo" contains a value of Hello World. Notice in this example that foo is not wrapped in quotes. When wrapped in quotes, foo is interpreted as a string. When not wrapped in quotes, foo is be interpreted as a variable

---
- hosts: localhost
  vars:
    foo: Hello World
  tasks:
  - name: "replace 'Hello' with 'World'"
    ansible.builtin.debug:
      msg: "{{ foo | regex_replace('Hello', 'Goodbye') }}"
...

 

 


Let's say you have a list of fruit. Here is how you could change apple to pear in the list.

---
- hosts: localhost
  tasks:
  - ansible.builtin.set_fact:
      fruit: ['apple', 'banana', 'orange', 'grapes']

  - name: replace apple with pear
    ansible.builtin.set_fact:
      fruit: "{{ fruit | map('regex_replace', 'apple', 'pear') | list }}"

  - ansible.builtin.debug:
      var: fruit
...

 


Case Insensitive

(?i) can be included to perform a case insensitive match.

---
- hosts: all
  tasks:
  - name: "replace 'Hello' with 'World'"
    ansible.builtin.debug:
      msg: "{{ foo | regex_replace('(?i)hello', 'Goodbye') }}"
...

 


Escaping literals

Let's say the foo variable contains a literal, a single period in this example. Double back slashes are used to escape literals.

---
- hosts: all
  vars:
    foo: "He.llo World"
  tasks:
  - ansible.builtin.debug:
      msg: "{{ foo | regex_replace('He\\.llo', 'Goodbye') }}"
...

 

Or better yet, regex_escape can be used to escape literals.

- ansible.builtin.set_fact:
    foo: "{{ foo | regex_escape() }}"

 


Escaping backslash

Let's say foo.txt contains a line that contains a backslash.

He\llo World

 

The following playbook will store the contents of foo.txt in the "out" variable, and then return the output using the debug module.

---
- hosts: localhost
  gather_facts: false
  tasks:
  - name: read foo.txt
    ansible.builtin.shell: cat foo.txt
    register: out

  - ansible.builtin.debug:
      msg: "{{ out.stdout }}"
...

 

Which should return the following. Notice Hello World in the "out" variable has 2 backslashes.

ok: [localhost] => {
    "msg": "He\\llo World"
}

 

Here is how you could remove the backslashes. It's important that the Jinja2 expression is surrounded by single quotes, and double quotes are used within the Jinja2 expression.

---
- hosts: localhost
  gather_facts: false
  tasks:
  - name: read foo.txt
    ansible.builtin.shell: cat foo.txt
    register: out

  - ansible.builtin.debug:
      msg: '{{ out.stdout | regex_replace("\\", "") }}'
...

 


Replacing new lines (/n) tabs (/t) and carriage returns (/r)

The following is an example of how you can remove all new lines, tabs, and carriage returns.

---
- hosts: localhost
  gather_facts: false
  tasks:
  - name: read foo.txt
    ansible.builtin.shell: cat foo.txt
    register: out

  - ansible.builtin.debug:
      msg: "{{ out.stdout | regex_replace('[\\r\\n\\t]+', '') }}"
...

 


whitespace

And here is how one or more whitespaces can be removed.

---
- hosts: localhost
  gather_facts: false
  tasks:
  - name: read foo.txt
    ansible.builtin.shell: cat foo.txt
    register: out

  - ansible.builtin.debug:
      msg: "{{ out.stdout | regex_replace('\\s+', '') }}"
...

 


jinja variable

Here is how you can include jinja variables (var1 in this example).

---
- hosts: localhost
  gather_facts: false
  tasks:
  - name: read foo.txt
    ansible.builtin.shell: cat foo.txt
    register: out

  - ansible.builtin.debug:
      msg: "{{ out.stdout | regex_replace('/path/to/'+var1+'', '') }}"
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 2a79f7 in the box below so that we can be sure you are a human.