通过ansible将文件中的所有行替换为一些新行?

通过ansible将文件中的所有行替换为一些新行?,ansible,Ansible,我有一个文件/etc/init/test.list,其中我需要删除以前存在的所有行,然后在其中插入以下行: deb [arch=amd64] hello deb [arch=amd64] world 我想通过ansible做到这一点。这可能吗?我曾想使用lineinfle模块,但我不知道如何在这里做到这一点 - name: replace all lines lineinfile: dest: /etc/init/test.list 如果要用新内容替换文件,可以只在

我有一个文件
/etc/init/test.list
,其中我需要删除以前存在的所有行,然后在其中插入以下行:

deb [arch=amd64] hello
deb [arch=amd64] world
我想通过ansible做到这一点。这可能吗?我曾想使用
lineinfle
模块,但我不知道如何在这里做到这一点

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

如果要用新内容替换文件,可以只在其上添加一个本地(或来自远程系统)文件:

- 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
如果更复杂,可以使用:


那么,用全新的内容替换文件?您可以使用“复制”或“模板”命令。是与全新内容一起使用。你能举个例子吗?
- name: Template a file to /etc/files.conf
  template:
    src: /mytemplates/foo.j2
    dest: /etc/file.conf
    owner: bin
    group: wheel
    mode: '0644'