Answers: <p>To replace all lines in the file <a href="https://docs.ansible.com/ansible/latest/modules/replace_module.html#replace-replace-all-instances-of-a-particular-string-in-a-file-using-a-back-referenced-regular-expression" rel="nofollow noreferrer">replace</a> module is needed. This is the equivalent (backup differs)</p>
  <pre class="lang-yaml prettyprint-override"><code>  tasks:
  
      - lineinfile:
          path: myconffile
          backup: true
          state: absent
          regexp: '\(ADDRESS = \(PROTOCOL = TCP\)\(HOST = 10.0.0.1\)\(PORT = 1501\)\)'
  
      - replace:
          path: myconffile
          backup: true
          regexp: '\(ADDRESS = \(PROTOCOL = TCP\)\(HOST = 10.0.0.2\)\(PORT = 1501\)\)'
          replace: '(ADDRESS = (PROTOCOL = TCP)(HOST = my-site.com)(PORT = 1501))'
  
      - lineinfile:
          path: myconffile
          backup: true
          state: absent
          regexp: '\(BALANCE = yes\)'
  </code></pre>
  <p>The simplified version is below</p>
  <pre class="lang-yaml prettyprint-override"><code>  tasks:
  
      - lineinfile:
          path: myconffile
          backup: true
          state: absent
          regexp: &quot;{{ item }}&quot;
        loop:
          - AAA
          - DDD
  
      - replace:
          path: myconffile
          backup: true
          regexp: BBB
          replace: CCC
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-06-11T04:21:02.227Z
Answers: 2024-12-01T13:06:30.463Z
Answers: 2024-12-01T13:06:30.463Z
Answers: Vladimir Botka
Body: <p>How can i use ansible lineinfile module to simulate working of below sed command.</p>
  
  <pre><code>sed -i _bkp '/(ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.0.1)(PORT = 1501))/d;s/(ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.0.2)(PORT = 1501))/(ADDRESS = (PROTOCOL = TCP)(HOST = my-site.com)(PORT = 1501))/;/(BALANCE = yes))/d' myconffile
  </code></pre>
  
Comments: CC BY-SA 4.0
Comments: 2019-06-11T04:23:56.593Z
Comments: Wouldn't be better to use `'/AAA/d;s/BBB/CCC/;/DDD/d'` ?
Comments: Vladimir Botka
Comments: CC BY-SA 4.0
Comments: 2019-06-11T04:59:49.473Z
Comments: @VladimirBotka : These lines are part of a config file.I was looking for a similar implementation in ansible using lineinfile/replace module.
Comments: AVS
Tags: <ansible>
Title: Ansible equivalent of below sed command
UserDisplayName: AVS
ThreadText: # Ansible equivalent of below sed command
  
  (Asked by: username_0 on 2019-06-10)
  
  How can i use ansible lineinfile module to simulate working of below sed command.
  
  ```
  sed -i _bkp '/(ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.0.1)(PORT = 1501))/d;s/(ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.0.2)(PORT = 1501))/(ADDRESS = (PROTOCOL = TCP)(HOST = my-site.com)(PORT = 1501))/;/(BALANCE = yes))/d' myconffile
  
  ```
  
  **Tags:** ansible
  
  > **Comments:**
  > **username_1**: Wouldn't be better to use `'/AAA/d;s/BBB/CCC/;/DDD/d'` ?  
  > **username_0**: @username_1 : These lines are part of a config file.I was looking for a similar implementation in ansible using lineinfile/replace module.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-06-11. Score: 2 | Accepted answer
  
  To replace all lines in the file [replace](https://docs.ansible.com/ansible/latest/modules/replace_module.html#replace-replace-all-instances-of-a-particular-string-in-a-file-using-a-back-referenced-regular-expression) module is needed. This is the equivalent (backup differs)
  
  ```yaml
    tasks:
  
      - lineinfile:
          path: myconffile
          backup: true
          state: absent
          regexp: '\(ADDRESS = \(PROTOCOL = TCP\)\(HOST = 10.0.0.1\)\(PORT = 1501\)\)'
  
      - replace:
          path: myconffile
          backup: true
          regexp: '\(ADDRESS = \(PROTOCOL = TCP\)\(HOST = 10.0.0.2\)\(PORT = 1501\)\)'
          replace: '(ADDRESS = (PROTOCOL = TCP)(HOST = my-site.com)(PORT = 1501))'
  
      - lineinfile:
          path: myconffile
          backup: true
          state: absent
          regexp: '\(BALANCE = yes\)'
  
  ```
  
  The simplified version is below
  
  ```yaml
    tasks:
  
      - lineinfile:
          path: myconffile
          backup: true
          state: absent
          regexp: "{{ item }}"
        loop:
          - AAA
          - DDD
  
      - replace:
          path: myconffile
          backup: true
          regexp: BBB
          replace: CCC
  
  ```
  
  
  ---
  
  