Answers: <p>The issue seems resolved, If I escape the backslash in the regex too:</p>
  
  <pre><code>- name: Remove LE webroot definition
      lineinfile: 
        path: "/etc/path/to/config/{{ inventory_hostname }}.conf"
        regexp: "^{{ inventory_hostname | replace('.', '\\.') }} = /path/to/a/directory"
        state: absent
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2020-05-15T13:00:13.360Z
Answers: 2020-05-18T07:58:48.747Z
Answers: 2020-05-18T07:58:48.747Z
Answers: Jonas Hüsser
Body: <p>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:</p>
  
  <pre><code>host.example.com = /path/to/a/directory
  </code></pre>
  
  <p>I want to remove that line. As {{ inventory_hostname }} contains dots, I'm trying to escape those with <code>replace</code>. The task in my playbook looks like this:</p>
  
  <pre><code>- name: Remove LE webroot definition
      lineinfile: 
        path: "/etc/path/to/config/{{ inventory_hostname }}.conf"
        regexp: "^{{ inventory_hostname | replace('.', '\.') }} = /path/to/a/directory"
        state: absent
  </code></pre>
  
  <p>When I'm executing the playbook, the following error occurs:</p>
  
  <pre><code>[...]
  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:
  [...]
  </code></pre>
  
  <p>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.</p>
  
Tags: <ansible><yaml>
Title: Ansible: Use inventory_hostname variable in lineinfile module
UserDisplayName: Jonas Hüsser
ThreadText: # Ansible: Use inventory_hostname variable in lineinfile module
  
  (Asked by: username_0 on 2020-05-15)
  
  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.
  
  **Tags:** ansible, yaml
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-05-15. Score: 4 | Accepted answer
  
  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
  
  ```
  
  
  ---
  
  