使用Ansible移动/复制目录中的所有文件

时间:2016-08-22 15:52:38

标签: linux file ansible fetch ansible-playbook

如何使用Ansible将目录中的所有文件移动或复制到我的localhost或其他远程主机?

这个问题适用于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”代表什么),但是我的localhost上的第二个目录仍然是空的。

有什么建议吗?

2 个答案:

答案 0 :(得分:4)

synchronize module你可能会有更多的运气 下面的示例将目录从库存主机拉到localhost:

- synchronize:
    mode: pull
    src: "/dir/"
    dest: "/second_dir/"

基于评论的其他信息:以下是传输源文件后删除源文件的方法:

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

答案 1 :(得分: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 }}"