replace a line in a file with lineinfile

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

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

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?

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

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

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)

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.

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