2016-08-22 4591 views
1

如何使用Ansible將目錄中的所有文件移動或複製到本地主機或其他遠程主機?使用Ansible移動/複製目錄中的所有文件

這個問題同時適用於linux系統和windows。

我已經走到這一步:

- hosts: all 
    tasks: 
    - name: list the files in the folder 
     command: ls /dir/ 
     register: dir_out 

    - name: do the action 
     fetch: src=/dir/{{item}} dest=/second_dir/ flat=yes 
     with_items: ('{{dir_out.stdout_lines}}') 

輸出如下:

TASK [setup] ******************************************************************* 
ok: [remote_host] 

TASK [list the files in the folder] ******************************************** 
changed: [remote_host] 

TASK [move those files] ******************************************************** 
ok: [remote_host] => (item=('[u'file10003', u'file10158', u'file1032', u'file10325', u'file10630', u'file10738', u'file10818', u'file10841', u'file10980', u'file11349', u'file11589', u'file11744', u'file12003', u'file12008', u'file12234', u'file12734', u'file12768', u'file12774', u'file12816', u'file13188', u'file13584', u'file14560', u'file15512', u'file16020', u'file16051', u'file1610', u'file16610', u'file16642', u'file16997', u'file17233', u'file17522', u'file17592', u'file17908', u'file18149', u'file18311', u'file18313', u'file18438', u'file185', u'file18539', u'file18777', u'file18808', u'file18878', u'file18885', u'file19313', u'file19755', u'file19863', u'file20158', u'file20347', u'file2064', u'file20840', u'file21123', u'file21422', u'file21425', u'file21711', u'file21770', u'file21790', u'file21808', u'file22054', u'file22359', u'file22601', u'file23609', u'file23763', u'file24208', u'file24430', u'file24452', u'file25028', u'file25131', u'file25863', u'file26197', u'file26384', u'file26398', u'file26815', u'file27025', u'file27127', u'file27373', u'file2815', u'file28175', u'file28780', u'file28886', u'file29058', u'file29096', u'file29456', u'file29513', u'file29677', u'file29836', u'file30034', u'file30216', u'file30464', u'file30601', u'file30687', u'file30795', u'file31299', u'file31478', u'file31883', u'file31908', u'file32251', u'file3229', u'file32724', u'file32736', u'file3498', u'file4173', u'file4235', u'file4748', u'file4883', u'file5812', u'file6126', u'file6130', u'file6327', u'file6462', u'file6624', u'file6832', u'file7576', u'file8355', u'file8693', u'file8726', u'file8838', u'file8897', u'file9112', u'file9331', u'file993']')) 

PLAY RECAP ********************************************************************* 
remote_host      : ok=3 changed=1 unreachable=0 failed=0 

它得到了所有從目錄中的文件,我猜是因爲with_items的(雖然我不知道「u」代表什麼),但我本地主機上的第二個目錄仍然是空的。

有什麼建議嗎?

+0

Re:關於事物的'u'前綴:每個任務的診斷輸出僅僅是內部Python數據結構的字符串轉儲。在python中,形式爲'u'...'的字符串是一個Unicode字符串。 – larsks

回答

3

您可能有更多的運氣與下面的synchronize module 例子是直接從庫存主機目錄爲localhost:

- synchronize: 
    mode: pull 
    src: "/dir/" 
    dest: "/second_dir/" 
基於評論

附加信息:這是你如何將它們轉移後刪除源文件:

- synchronize: 
    mode: pull 
    src: "/dir/" 
    dest: "/second_dir/" 
    rsync_opts: 
    - "--remove-source-files" 
+0

嘿謝謝。第一個目錄是每隔幾分鐘接受文件的文件夾。如果在同步當前文件後,我想刪除它們,請刪除尚未同步的新接受的文件? – Jointer

+0

聽起來你可能會在這裏發生一些不好的模式,但我會用更多的信息更新我的答案,因爲我無法在評論中編碼格式。 – smiller171

0
--- # copying yaml file 
- hosts: localhost 
    user: {{ user }} 
    connection: ssh 
    become: yes 
    gather_facts: no 
    tasks: 
    - name: Creation of directory on remote server 
    file: 
     path: /var/lib/jenkins/.aws 
     state: directory 
     mode: 0755 
    register: result 
    - debug: 
     var: result 

    - name: get file names to copy 
    command: "find conf/.aws -type f" 
    register: files_to_copy 

    - name: copy files 
    copy: 
     src: "{{ item }}" 
     dest: "/var/lib/jenkins/.aws" 
     owner: {{ user }} 
     group: {{ group }} 
     remote_src: True 
     mode: 0644 
    with_items: 
     - "{{ files_to_copy.stdout_lines }}" 
+0

用您期望的用戶{{user}}替換您用來訪問另一臺機器的用戶,而擁有該名稱文件的所有者和組需要具有權限。 –

+0

請不要在多個問題下發布[相同的答案](https://stackoverflow.com/a/47688445/2947502)。閱讀此https://meta.stackoverflow.com/a/256849/2947502 – techraf

相關問題