<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>

CC BY-SA 4.0
2019-06-11T04:21:02.227Z
2024-12-01T13:06:30.463Z
2024-12-01T13:06:30.463Z
Vladimir Botka
<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>

CC BY-SA 4.0
2019-06-11T04:23:56.593Z
Wouldn't be better to use `'/AAA/d;s/BBB/CCC/;/DDD/d'` ?
Vladimir Botka
CC BY-SA 4.0
2019-06-11T04:59:49.473Z
@VladimirBotka : These lines are part of a config file.I was looking for a similar implementation in ansible using lineinfile/replace module.
AVS
<ansible>
Ansible equivalent of below sed command
AVS
# 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**: @VladimirBotka : 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 module is needed. This is the equivalent (backup differs)
  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
  tasks:

    - lineinfile:
        path: myconffile
        backup: true
        state: absent
        regexp: "{{ item }}"
      loop:
        - AAA
        - DDD

    - replace:
        path: myconffile
        backup: true
        regexp: BBB
        replace: CCC


---

