replace a line in a file with lineinfile

38 views
Skip to first unread message

dulh...@mailbox.org

unread,
Jan 15, 2022, 9:28:00 AM1/15/22
to ansible...@googlegroups.com
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

Parth Patel

unread,
Jan 15, 2022, 9:37:45 AM1/15/22
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/32365982.212197.1642256863668%40office.mailbox.org.

dulh...@mailbox.org

unread,
Jan 15, 2022, 9:59:07 AM1/15/22
to ansible...@googlegroups.com
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?


regexp: 'MYHOME=/opt/db/mysql/mariadb-10.0.23-linux-x86_64'
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


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/32365982.212197.1642256863668%40office.mailbox.org.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Parth Patel

unread,
Jan 15, 2022, 10:01:00 AM1/15/22
to ansible...@googlegroups.com
I doubt without using ^ it wont be able to find line starting with MYHOME………….

Dick Visser

unread,
Jan 15, 2022, 11:55:43 AM1/15/22
to ansible...@googlegroups.com
Hii

On Sat, 15 Jan 2022 at 15:28, dulhaver via Ansible Project <ansible...@googlegroups.com> wrote:
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...?



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/32365982.212197.1642256863668%40office.mailbox.org.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

dulh...@mailbox.org

unread,
Jan 15, 2022, 2:25:30 PM1/15/22
to ansible...@googlegroups.com


On 15.01.22 17:55, Dick Visser wrote:
Hi

On Sat, 15 Jan 2022 at 15:28, dulhaver via Ansible Project <ansible...@googlegroups.com> wrote:
I want to change replace a particular line in an exiting file (after having pushed a template of it to the remote)

If you are already using a template, why not just that instead of lineinfile...?

because lineinfile 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)




Todd Lewis

unread,
Jan 15, 2022, 3:01:58 PM1/15/22
to Ansible Project
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.

dulh...@mailbox.org

unread,
Jan 15, 2022, 5:02:54 PM1/15/22
to ansible...@googlegroups.com

On 01/15/2022 9:01 PM Todd Lewis <uto...@gmail.com> wrote:


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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages