# replace a line in a file with lineinfile

**URL:** <https://forum.ansible.com/t/replace-a-line-in-a-file-with-lineinfile/36205>\
**Category:** Ansible Project\
**Created:** [January 15, 2022, 2:27pm UTC](https://forum.ansible.com/t/replace-a-line-in-a-file-with-lineinfile/36205 "2022-01-15T14:27:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [January 15, 2022, 2:27pm UTC](https://forum.ansible.com/t/replace-a-line-in-a-file-with-lineinfile/36205/1 "2022-01-15T14:27:43Z")

</div>

I want to change replace a particular line in an exiting file (after having pushed a template of it to the remote)

this is ant extract of the file content

> cat bin/.myenv

# Mysql Environment

…  
MYBASE=/opt/db/mysql/mariadb  
**MYHOME=/opt/db/mysql/mariadb-10.0.23-linux-x86\_64**  
MYDATA=/opt/db/data/mysql  
MYBACK=/opt/db/backup/mysql/dumps

…

I want to replace the line

**MYHOME=/opt/db/mysql/mariadb-10.0.23-linux-x86\_64**

with this new line

MYHOME=/opt/db/mysql/mariadb-10.5.4-linux-x86\_64

my TASK looks like this

- name: replace mariadb version in " {{ mariadb\_base }}"/bin/.myenv  
lineinfile:  
path: " {{ mariadb\_base }}/bin/.myenv "  
regexp: ‘MYHOME=/opt/db/mysql/\*’  
line: MYHOME=/opt/db/mysql/mariadb-10.5.4-linux-x86\_64

where {{ mariadb\_base }} equals /opt/db/mysql

the error I am getting is:

TASK [mariadb\_install : replace mariadb version in " /opt/db/mysql"/bin/.myenv] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [vm-51150-0198]: FAILED! =\> changed=false  
msg: Destination /opt/db/mysql/bin/.myenv does not exist !  
rc: 257

however checking on the target I can see that the file /opt/db/mysql/bin/.myenv actually does exist

---

<div class="post-metadata">

**Author:** ![Parth\_Patel](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@Parth\_Patel](https://forum.ansible.com/u/Parth_Patel)\
**Post date:** [January 15, 2022, 2:37pm UTC](https://forum.ansible.com/t/replace-a-line-in-a-file-with-lineinfile/36205/2 "2022-01-15T14:37:24Z")

</div>

Hey,

Are you trying to use become method to be root or privileged user ? If file exist on target node can u show permissions on it.

And in line regexp change to below to look for starting with line using ^

regexp = ‘^MYHOME=/opt/db/mysql/\*’

Cheers,  
Parth

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [January 15, 2022, 2:58pm UTC](https://forum.ansible.com/t/replace-a-line-in-a-file-with-lineinfile/36205/3 "2022-01-15T14:58:49Z")

</div>

hm. adding a become: yes makes the TASK go through, so the hint was good, thx.

However the line is not being replaced.

can it be the regexp: ' **MYHOME=/opt/db/mysql/mariadb-10.0.23-linux-x86\_64’** does not identify the line to be changed correctly?  
that would explain that behavior somewhat.

If so … what may the the problem with this?

---

<div class="post-metadata">

**Author:** ![Parth\_Patel](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@Parth\_Patel](https://forum.ansible.com/u/Parth_Patel)\
**Post date:** [January 15, 2022, 3:00pm UTC](https://forum.ansible.com/t/replace-a-line-in-a-file-with-lineinfile/36205/4 "2022-01-15T15:00:32Z")

</div>

I doubt without using ^ it wont be able to find line starting with MYHOME………….

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 15, 2022, 4:55pm UTC](https://forum.ansible.com/t/replace-a-line-in-a-file-with-lineinfile/36205/5 "2022-01-15T16:55:21Z")

</div>

Hii

> I want to change replace a particular line in an exiting file (after having pushed a template of it to the remote)

It you are already using a template, why not just that instead of lineinfile…?

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [January 15, 2022, 7:25pm UTC](https://forum.ansible.com/t/replace-a-line-in-a-file-with-lineinfile/36205/6 "2022-01-15T19:25:09Z")

</div>

because came to my attention first, to be honest. But I’ll happily be looking into other options (as I can not make it work anyway)

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [January 15, 2022, 8:01pm UTC](https://forum.ansible.com/t/replace-a-line-in-a-file-with-lineinfile/36205/7 "2022-01-15T20:01:58Z")

</div>

You have a space on either end of your “path:” parameter. Change  
path: " {{ mariadb\_base }}/bin/.myenv "  
to  
path: “{{ mariadb\_base }}/bin/.myenv”  
and see if that works better.

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [January 15, 2022, 10:02pm UTC](https://forum.ansible.com/t/replace-a-line-in-a-file-with-lineinfile/36205/8 "2022-01-15T22:02:04Z")

</div>

> You have a space on either end of your “path:” parameter. Change  
> path: " {{ mariadb\_base }}/bin/.myenv "  
> to  
> path: “{{ mariadb\_base }}/bin/.myenv”  
> and see if that works better.

yes, much better. thx
