首页 > 解决方案 > 使用 Ansible 将特定文件复制到特定服务器

问题描述

我有 2 个文件。文件 A 和文件 B。现在创建 2 个服务器后,我只想将文件 A 推送到 server1,将文件 B 推送到 server2。

我下面的解决方案适用于 2 个文件。但是当我必须将此模型扩展到 10 台以上的服务器时,它的效率并不高。

    - name: Copy file with owner and permissions
      copy:
        src: /srv/myfiles/serverA.file
        dest: /my/destination
      when: "serverA" in inventory_hostname

有人可以指出如何更有效地使用 Ansible
文件 A 到服务器 A 文件 B 到服务器 B 文件 C 到服务器 C 文件 Z 到服务器 Z 来更有效地完成以下场景。

标签: linuxansible

解决方案


If your server names host1, host2, host3

In Control node your files are in /tmp
host1.txt
host2.txt
host3.txt

The playbook to copy the files to respective host's /tmp/ :

- hosts: all
  gather_facts: no
  tasks:
  - copy:
      src: "/tmp/{{ item }}"
      dest: /tmp/
    with_lines: ls /tmp
    when: inventory_hostname in item

Depending on the hostname and file name change the logic if required.

推荐阅读