# Solved - Replace three strings in one line

**URL:** <https://forum.ansible.com/t/solved-replace-three-strings-in-one-line/9620>\
**Category:** Get Help\
**Created:** [October 1, 2024, 6:02pm UTC](https://forum.ansible.com/t/solved-replace-three-strings-in-one-line/9620 "2024-10-01T18:02:23Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [October 1, 2024, 6:02pm UTC](https://forum.ansible.com/t/solved-replace-three-strings-in-one-line/9620/1 "2024-10-01T18:02:23Z")

</div>

Hello,

Apologies if this was already answered. I haven’t found the answer so far…

I have this templated line in the /etc/fstab file:  
NFS\_SVM:/p\_n\_s\_REQ\_ID\_oracle\_HOSTNAME\_data

Would it be possible to replace NFS\_SVM, REQ\_ID and HOSTNAME using a single task, considering that I can’t use a template file…? How?  
As the /etc/fstab file is not standard across all servers, I’m considering that a template file would not fit as a solution.

Thanks,

Alex

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [October 1, 2024, 6:54pm UTC](https://forum.ansible.com/t/solved-replace-three-strings-in-one-line/9620/2 "2024-10-01T18:54:33Z")

</div>

One task might be a convoluted loop, but it certainly easily be done in a few tasks in one playbook.

Check out [ansible.builtin.replace](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/replace_module.html), that will probably be the simplest solution.

---

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [October 1, 2024, 7:10pm UTC](https://forum.ansible.com/t/solved-replace-three-strings-in-one-line/9620/3 "2024-10-01T19:10:34Z")

</div>

Hi,

Yes, although I did not mention, I’m assuming the replace module would be the option to go with.  
But I could not think/find a way to have it do 3 replacements in a single call/task.

Alex

---

<div class="post-metadata">

**Author:** ![jiholland](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jiholland/32/11273_2.png) [@jiholland](https://forum.ansible.com/u/jiholland)\
**Post date:** [October 1, 2024, 7:14pm UTC](https://forum.ansible.com/t/solved-replace-three-strings-in-one-line/9620/4 "2024-10-01T19:14:58Z")

</div>

Welcome to the forum!  
You might want to check out and consider using the [ansible.posix.mount](https://docs.ansible.com/ansible/latest/collections/ansible/posix/mount_module.html) module. Example:

```yaml
---
- name: Mount volume.
  hosts: localhost
  gather_facts: false
  vars:
    nfs_svm: example
    req_id: example
    hostname: example
  tasks:

    - name: Mount NFS volume.
      ansible.posix.mount:
        src: "{{ nfs_svm }}:/p_n_s_{{ req_id }}_oracle_{{ hostname }}_data"
        path: /mnt/shared_data
        fstype: nfs
        state: present
      become: true

```

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [October 1, 2024, 7:29pm UTC](https://forum.ansible.com/t/solved-replace-three-strings-in-one-line/9620/5 "2024-10-01T19:29:30Z")

</div>

I have successfully used key/value pairs in a loop to do something similar…like this:

```auto
- name: SSO - Modify FILENAME
  become: true
  ansible.builtin.replace:
    path: "/tmp/FILENAME"
    regexp: "{{ item.key }}"
    replace: "{{ item.value }}"
  loop:
    - key: "ORIG_VALUE"
      value: "REPLACE_WITH"
    - key: "2ND_ORIG_VALUE"
      value: "2nd_REPLACE_WITH"

```

Of course this works for me knowing exactly what I need to replace - but with replace you can use wildcards I believe, so you might still be able to use the idea…

---

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [October 1, 2024, 7:37pm UTC](https://forum.ansible.com/t/solved-replace-three-strings-in-one-line/9620/6 "2024-10-01T19:37:13Z")

</div>

Hi Jørn,

True, that would work.  
But the reason I was not yet considering using it is that I’d like to keep the existing fstab file structure. Unless I did something wrong, the last time I use it it added the new lines to the end of the file… (Although I may ending up not have a choice and use it.)

Thanks a lot…!

Alex

---

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [October 1, 2024, 7:39pm UTC](https://forum.ansible.com/t/solved-replace-three-strings-in-one-line/9620/7 "2024-10-01T19:39:27Z")

</div>

Hi,

That’s looking promising, thanks…! I haven’t seen/used that before.

I’ll later give that a try and post the results.

Alex

---

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [October 1, 2024, 9:48pm UTC](https://forum.ansible.com/t/solved-replace-three-strings-in-one-line/9620/8 "2024-10-01T21:48:36Z")

</div>

Hi,

So, using the replace module in a loop, as many would already know, did solve my “issue” the way I’d prefer it to be solved:

```
      - name: Replace fstab tags
        ansible.builtin.replace:
            path: "{{ fstab_file }}"
            regexp: "{{ item.key }}"
            replace: "{{ item.value }}"
        loop:
          - key: "NFS_SVM"
            value: "{{ nfs_svm }}"
          - key: "REQ_ID"
            value: "{{ request_id }}"
          - key: "HOSTNAME"
            value: "{{ inventory_hostname }}"

```

Thank you all for your help,

Alex

---

<div class="post-metadata">

**Author:** ![Leo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/leo/32/23_2.png) [@Leo](https://forum.ansible.com/u/Leo)\
**Post date:** [October 2, 2024, 12:49am UTC](https://forum.ansible.com/t/solved-replace-three-strings-in-one-line/9620/9 "2024-10-02T00:49:16Z")

</div>

Hi Alex\_Wanderley, welcome to the forum!

Let me share a couple of tips on using the forum to make it easier for people to help out. In this case for tagging @jrglynn2 post as the answer as that one solved your need. We have a feature for that instead of requiring a topic title edit;

## Solutions

If you get to a point where you consider a post by someone helped you to solve the issue, please tick ✅ the check-box below their message:

![image](https://us1.discourse-cdn.com/ansible/original/1X/de4bda8ca15f48c52ff19f12b452d10c02126f7f.png)

This will mark the message as the solution and show it in the first post for easy access, as well as recognize the person that helped:

![image](https://us1.discourse-cdn.com/ansible/original/1X/8964a515490358701e2c80d4ab69712f980e764c.png)

I’m doing that for you in this case but wanted to mention it for future posts!

For all other replies where someone helped (like @jiholland, which is a very good suggestion) clicking the ❤ is appreciated.

Let us know if you have any questions or doubts!

---

<div class="post-metadata">

**Author:** ![jiholland](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jiholland/32/11273_2.png) [@jiholland](https://forum.ansible.com/u/jiholland)\
**Post date:** [October 2, 2024, 5:58am UTC](https://forum.ansible.com/t/solved-replace-three-strings-in-one-line/9620/10 "2024-10-02T05:58:31Z")

</div>

Yes, the `ansible.posix.mount` module will add a new line and not replace the line `NFS_SVM:/p_n_s_REQ_ID_oracle_HOSTNAME_data` in `/etc/fstab`, but it will ensure idempotency and that entries added to `/etc/fstab` gets the right structure.

---

<div class="post-metadata">

**Author:** ![jiholland](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jiholland/32/11273_2.png) [@jiholland](https://forum.ansible.com/u/jiholland)\
**Post date:** [October 2, 2024, 6:09am UTC](https://forum.ansible.com/t/solved-replace-three-strings-in-one-line/9620/12 "2024-10-02T06:09:32Z")

</div>

While the `ansible.builtin.lineinfile` module has its caveats, I think it is better suited in this scenario than `ansible.builtin.replaced`:

- `ansible.builtin.replace` will replace _all_ matched instances.
- The loop will make the task run _multiple_ times, which takes more time.

Example with `lineinfile`:

```yaml
---
- name: Replace line in fstab file.
  hosts: localhost
  gather_facts: false
  vars:
    nfs_svm: example
    req_id: example
    hostname: example
  tasks:
    - name: Replace line in fstab file.
      ansible.builtin.lineinfile:
        path: "/etc/fstab"
        regexp: '^NFS_SVM:/p_n_s_REQ_ID_oracle_HOSTNAME_data$'
        line: "{{ nfs_svm }}:/p_n_s_{{ req_id }}_oracle_{{ hostname }}_data"
      become: true

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 1, 2024, 6:09am UTC](https://forum.ansible.com/t/solved-replace-three-strings-in-one-line/9620/13 "2024-11-01T06:09:50Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
