怎么用ansible传输文件
时间: 2024-10-23 22:19:24 AIGC 浏览: 82
Ansible是一个强大的自动化运维工具,其中有一个模块叫做`copy`,用于在远程主机之间传输文件。以下是基本步骤:
1. 安装必要的依赖:首先确保你的Ansible控制节点安装了`ansible`及其插件,如`ansible-galaxy`,如果你需要复制的文件不在默认路径下,可能还需要额外的插件如`unarchive`。
2. 创建Playbook:编写一个包含`copy`模块的任务的Playbook文件,例如`my_files.yml`:
```yaml
---
- name: Copy files to remote hosts
hosts: target_hosts
become: true
tasks:
- name: Copy file from local machine
copy:
src: /path/to/local/file.txt
dest: /remote/path/file.txt
mode: 0644 # set permissions if needed
- name: Copy directory recursively
copy:
src: /local/directory/
dest: /remote/path/
remote_src: yes # if the source is on remote host
```
3. 运行任务:在控制节点上运行`ansible-playbook my_files.yml`命令, Ansible会连接到指定的`target_hosts`并执行文件传输操作。
阅读全文
相关推荐




















