在Ansible中,可以使用copy
模块和循环来实现从多个源复制多个文件到多个目标的操作。下面是一个示例代码:
- name: Copy files from multiple sources to multiple destinations
hosts: all
gather_facts: false
vars:
files_to_copy:
- src: /path/to/source1/file1
dest: /path/to/destination1/file1
- src: /path/to/source2/file2
dest: /path/to/destination2/file2
- src: /path/to/source3/file3
dest: /path/to/destination3/file3
tasks:
- name: Copy files
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
loop: "{{ files_to_copy }}"
在上面的示例中,我们定义了一个名为files_to_copy
的变量,其中包含了每个文件的源路径和目标路径。然后,在copy
任务中,我们使用loop
循环遍历files_to_copy
变量,并将每个文件复制到指定的目标路径中。
注意,这个示例假设你已经在Ansible的控制机上配置了适当的连接和授权方式,以便能够连接到目标主机并执行复制操作。