5

I'm using Ansible 2.7.9 and I'm having trouble with the regex definition in the lineinfile module. My file has the following line:

host.example.com = /path/to/a/directory

I want to remove that line. As {{ inventory_hostname }} contains dots, I'm trying to escape those with replace. The task in my playbook looks like this:

- name: Remove LE webroot definition
    lineinfile: 
      path: "/etc/path/to/config/{{ inventory_hostname }}.conf"
      regexp: "^{{ inventory_hostname | replace('.', '\.') }} = /path/to/a/directory"
      state: absent

When I'm executing the playbook, the following error occurs:

[...]
The offending line appears to be:
      path: "etc/path/to/config/{{ inventory_hostname }}.conf"
      regexp: "^{{ inventory_hostname | replace('.', '\.') }} = /path/to/a/directory"
                                                      ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:
[...]

As you can see, I'm using the double quotes, and all examples that I've found are using this syntax. What I'm doing wrong here? Thanks for any help.

1 Answer 1

4

The issue seems resolved, If I escape the backslash in the regex too:

- name: Remove LE webroot definition
    lineinfile: 
      path: "/etc/path/to/config/{{ inventory_hostname }}.conf"
      regexp: "^{{ inventory_hostname | replace('.', '\\.') }} = /path/to/a/directory"
      state: absent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.