damaged1988
damaged1988

Reputation: 57

How to modify a file but only one occurrence using ansible

I am using ansible, currently trying replace but open to lineinfile modules. But I want to edit only the last column in the row that starts with root (the first occurrence of 0). https://docs.python.org/2/library/re.html I have been looking at the documentation and cannot edit only the first occurrence. What am I missing?

foo.txt as follows then the ansible code below the contents of the file.

/root/bar/foo /                           defaults        1 0
/foo/bar/foo /                           defaults        1 0
/foo/bar/foo /                           defaults        1 0
/foo/bar/foo /                           defaults        1 0
/foo/bar/foo /                           defaults        1 0



- name: edit
  replace:
   path: /path/to/foo.txt
   regexp: '0+$'
   replace: '1'

Upvotes: 2

Views: 1849

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68104

Q: "Edit only the last column in the row that starts with root."

A: The regexp and replace below do the job

    regexp: '^/root/(.*) 0+$'
    replace: '/root/\1 1'

Upvotes: 2

Related Questions