Remove symbols from ansible stdout_lines in file with Ansible

Viewed 75

A have the line in file acounts.list

['# name prueba7@prueba7.com', 'zimbraIsDelegatedAdminAccount: TRUE']

I would like to remove ['# name, all ' and '] What would the result be prueba7@prueba7.com, zimbraIsDelegatedAdminAccount: TRUE

I tried to create task to remove first ['# name

- lineinfile:
    dest: /home/acounts.list
    state: absent
    regexp: "^\[\'\#\sname\s"

... but i got error. Could you please help with solution? Thank you.

1 Answers

use replace instead:

- name: replace
  ansible.builtin.replace:
    path: /home/acounts.list
    regexp: "['\\[\\]]|# *name *"
    replace: ''

you could write too: regexp: '[''\[\]]|# *name *'

['\\[\\]] means replace all ' or [ or ] (or == [])

so as [ and ] are special char, you ahve to protect by \ with " you have to protect \ too by another \ (not needed when surrounded by ')

| means or and the other expression is # *name * * means zero or more space

Related