在 Ansible 中,如何从多个节点获取文件并将其集中存储在一个文件中?

In Ansible, how can I fetch file from multiple nodes and store this in one file centralized?

所有我需要的都在标题中,例如我想知道如何做这样的事情:

---
- hosts: ansible-clients

  tasks:
    - name: Fetch source list from clients
      fetch: src=/etc/apt/sources.list
             dest=/tmp/allnodes.sourcelist

或者简单地说

echo remote@/etc/apt/sources.list >> local@/tmp/allnodes.sourcelist

我可以在本地创建和 运行 脚本,但我唯一的条件是在一个剧本中执行所有操作。

你可以使用他的剧本:

---
- hosts: ansible-clients
  tasks:
    - name: Fetch source list from clients
      fetch:
        src: /etc/apt/sources.list
        flat: yes
        dest: "/tmp/{{ inventory_hostname }}.sourcelist"
    - name: Merge files
      run_once: yes
      delegate_to: localhost
      shell: "cat /tmp/{{ item }}.sourcelist >> /tmp/allnodes.sourcelist"
      with_items: "{{ groups['ansible-clients'] }}"
  • 第一个任务用于从远程获取所有文件并将它们存储在 /tmp 中(inventory_hostname)用于文件名。

  • 第二个任务是 运行 一次,并在最终文件中追加所有文件(获取链接到组 ansible-clients 的主机列表)

注意:最终文件永远不会被删除,也许您在获取文件之前将其删除(使用 run_once=yes)