Remove a line from a file using ansible?
ansible
Solution
The problem probably is that `+` has a meaning in a regular expression.
You should be able to fix it by escaping the `+`. If you can't do that from the source where `{{ key }}` is defined, you can escape it with the `replace` Jinja filter:
- name: Delete keys in sysadmin/.ssh/authoriezd_keys
lineinfile: dest=/home/{{name}}/.ssh/authorized_keys
state=absent
regexp='^{{ key | replace("+", "\+") }}$'
You might run into more problems if `{{ key }}` contains other characters which have a meaning in regular expressions. If that's the case I think the safe way would be to create your own filter plugin where you simply return the input passed through `re.escape`.
Problem
I have a file called `authorized_keys`. I need to delete a particular line using an Ansible script. The problem is when I try to remove a line that includes a '+' character. Ansible is not able to remove this line. e.g `authorized_keys` file is: ``` ..... abhi foo+bar saken ......(EOF) ``` I want to remove the `abhi foo+bar saken` line but Ansible is not removing this line because of the `+` character. I am able to remove lines that do not contain a `+` character . Task: ``` - name: Delete keys in sysadmin/.ssh/authoriezd_keys lineinfile: dest=/home/{{name}}/.ssh/authorized_keys state=absent regexp='^{{key}}$' ``` PS: I am using Ansible's `lineinfile` module