flash
flash

Reputation: 1519

Replace all the lines in a file with some new lines through ansible?

I have a file /etc/init/test.list in which I need to remove all the lines which is previously present and then insert below lines in it:

deb [arch=amd64] hello
deb [arch=amd64] world

I want to do this through ansible. Is this possible to do? I was thinking to use lineinfile module but I am not sure how to do that here.

  - name: replace all lines
    lineinfile: 
      dest: /etc/init/test.list

Upvotes: 0

Views: 1713

Answers (1)

Alister Bulman
Alister Bulman

Reputation: 35169

If you want to replace a file with new contents, you can just copy a local (or from the remote system) file on top of it:

- name: example copying file with owner and permissions (from the sending machine)
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: 0644
    remote_src: no  # yes will look for the file on remote server

- name: Copy using the 'content' for inline data
  copy:
    content: '# This file was moved to /etc/other.conf'
    dest: /etc/mine.conf

If it was more complicated, you can use a template:

- name: Template a file to /etc/files.conf
  template:
    src: /mytemplates/foo.j2
    dest: /etc/file.conf
    owner: bin
    group: wheel
    mode: '0644'

Upvotes: 2

Related Questions