1

How can I move or copy all the files in a directory to my localhost or another remote host with Ansible?

This question goes both to linux systems and windows.

What I've got so far:

- 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}}')

The output is as follows:

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 

It got all the files from the directory, I guess because of the with_items (though I don't know what the "u" stands for), but the second directory on my localhost remains empty.

Any suggestions?

gre_gor
  • 6,669
  • 9
  • 47
  • 52
Jointer
  • 19
  • 1
  • 1
  • 2
  • Re: the `u` prefix on things: the diagnostic output for each task is just a string dump of the internal Python data structures. In python, a string of the form`u'...'` is a Unicode string. – larsks Aug 22 '16 at 16:14

2 Answers2

4

You may have more luck with the synchronize module Example below pulls a dir from inventory host to localhost:

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

Additional info based on comment: Here is how you would delete the source files after transferring them:

- synchronize:
    mode: pull
    src: "/dir/"
    dest: "/second_dir/"
    rsync_opts:
    - "--remove-source-files"
MillerGeek
  • 3,057
  • 20
  • 23
  • hey thanks. The first dir is a folder that accepts files every few minutes. what if after I sync the current files, I would like to delete them, whitout deleting the newly accepted files that has not been synced? – Jointer Aug 22 '16 at 18:27
  • It sounds like you might have some bad patterns going on here, but I'll update my answer with additional info since I can't code format in a comment. – MillerGeek Aug 22 '16 at 18:32
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 }}"
  • replace user with desired {{ user }} with the user you use to access another machine and owner and group with the name file need to have permission. – Prateek patel Dec 07 '17 at 06:03
  • Please do not post [the same answer](https://stackoverflow.com/a/47688445/2947502) under multiple questions. Read this https://meta.stackoverflow.com/a/256849/2947502 – techraf Dec 07 '17 at 07:46