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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM