使用 Ansible 替换文件的全部内容

Cha*_*pop 4 scripting yaml ansible

我希望能够使用 Ansible playbook 替换文件的全部内容。

我已经编写了那段代码,但似乎使用通配符是错误的方法。

---
  - hosts: my_hosts
    tasks:
     - name: Replace the content of my file
       lineinfile:
         path: /home/user-ansible/test/conf
         regexp: "*"
         line: "{{ lookup('file', '/home/ansible/config/conf') }}"
Run Code Online (Sandbox Code Playgroud)

Smi*_*ily 8

要替换目标服务器 ( ) 上文件的内容dest并确保它始终与源 ( src) 一致,请使用该copy模块

---
- name: Demo play to copy file
  hosts: my_hosts
  tasks:
  - name: Replace the content of my file
    copy:
      src: /home/user-ansible/test/conf
      dest: /home/ansible/config/conf
Run Code Online (Sandbox Code Playgroud)

附加说明:您发布的 playbook 中存在 yaml 缩进问题,可能会在将来引起问题。请花Y分钟学习yaml并注意缩进和换行。您可以在发布之前使用yamllintansible-lint验证您的剧本