zgdy 发表于 2018-1-2 10:57:11

Ansible在节点间传输文件

  1. 在控制节点(the control machine )与远程节点( the current remote host)之间传输文件
  1.1 如果需要传输文件,可以使用copy模块,注意copy模块不能递归目录:
  

copy: src=/path/on/control_machine/{{ item }} dest=/path/on/reomte_host remote_src=yes  with_items:
  - "file_1"
  - "file_2"
  

  

  1.2 可以使用unarchive模块传输打包好的文件夹,在远程节点上自动解压:
  

- name: copy your folder from control machine to remote host  unarchive: src=/path/on/control_machine/{{ item }} dest=/path/on/reomte_host
  with_items:
  - "file_1"
  - "file_2"
  

  1.3 使用synchronize模块,设置mode=pull,即远程节点从控制机器拉取数据:
  

- name: copy file from control machine to remote host  synchronize: mode=pull src=/path/on/control_machine/{{ item }} dest=/path/on/reomte_host
  with_items:
  - "file_1"
  - "file_2"
  

  

  2.当需要在两个远程主机(remote host )上面互相传输数据时候:
  2.1 当远程节点为ServerB的时候,将ServerA的文件夹传输到ServerB上:
  

- hosts: ServerB  tasks:
  - name: Transfer file from ServerA to ServerB
  synchronize:
  src: /path/on/server_a
  dest: /path/on/server_b
  delegate_to: ServerA
  

  2.2 当远程节点为ServerA的时候,将ServerA的文件夹传输到ServerB上:
  

- hosts: ServerA  tasks:
  - name: Transfer file from ServerA to ServerB
  synchronize:
  src: /path/on/server_a
  dest: /path/on/server_b
  mode: pull
  delegate_to: ServerB
  

  2.3 或者使用SCP命令:
  

- name: Transfer file from ServerB to ServerA  shell: scp-r /path/on/server_b node@ServerA:/path/on/server_a
  ignore_errors: True
  delegate_to: ServerB
  

  

  REF: http://stackoverflow.com/questions/25505146/how-to-copy-files-between-two-nodes-using-ansible
  http://stackoverflow.com/questions/25576871/ansible-best-practice-to-copy-directories
页: [1]
查看完整版本: Ansible在节点间传输文件