Copy files between two remote servers WITHOUT synchronize module

28 views
Skip to first unread message

Glen Collins

unread,
Apr 30, 2019, 9:10:26 PM4/30/19
to Ansible Project
Hello all!

   I have an issue in my environment where I cannot use the synchronize module to transfer data between two remote nodes. So I need a different way to do this.

ServerA: Ansible Server (Tower Instance)
ServerA1: Ansible Server (Tower Instance)
ServerA2: Ansible Server (Tower Instance)

ServerB: Data Server

ServerC: User Server

I want to run a playbook on ServerA and have it copy a/many file(s) from ServerB to ServerC. I cannot use synchronize do to environmental issues. So how can I get this done ether using copy or fetch modules. Or something else like SCP from the shell or command module.

I cannot find a good example of doing this other than the synchronize module. I do not want to just copy the data files to the tower servers. I need to keep my environment the same as it is today without changes.

I'm using ansible 2.7. These are all Redhat Linux Servers.

Here is a sample I tried. Really simple but...Inventory comes from tower server which is why the hosts says "all"

---
- name: Playbook1
  hosts: all
  gather_facts: yes
  become: yes
  remote_user: user1

  tasks:

    - name: Copy file to dest server
      fetch:
        src: "/var/tmp/file1"
        dest: "/var/tmp/"
        flat: yes
      delegate_to: ServerB
    
---
- name: Playbook2
  hosts: all
  gather_facts: yes
  become: yes
  remote_user: user1

  tasks:

    - name: Copy file to dest server
      copy:
        src: "/var/tmp/file1"
        dest: "/var/tmp/"
remote_src: yes
      delegate_to: ServerB

Any help would be appreciated.

Regards,

Glen



shivha...@gmail.com

unread,
May 1, 2019, 9:36:25 AM5/1/19
to Ansible Project
Hi Glen,

You may use copy and fetch module as shown below:
In  the below playbook the files would be fetch from source machine and stored temporarily on the control node and then copied to the destination. 

---
 
- hosts: src_apps
   tasks
:
     
- name: List of files to be copied
       command
: "find /opt/dummyfiles/ -type f"
       
register: list_files

     
- name: Fetch the file from the source to control node
       run_once
: yes
       fetch
: src={{item}} dest=buffer/ flat=yes
       with_items
: "{{ list_files.stdout_lines }}"

 
- hosts: dest_apps
   tasks
:
     
- name: Copy the file from control node to destination
       copy
: src=buffer/ dest=/opt/


OR

If you do not want to use the control node to store the files, you may use the command module and execute the SCP command from the source machine itself. Below is the playbook:
Please note, before executing the playbook you would need to enable password-less SSH between the two remote nodes. 
---
 
- hosts: src_apps
   tasks
:
     
- name: List of files to be copied
       command
: "find /opt/dummyfiles/ -type f"
       
register: list_files

     
- name: Execute SCP command
       command
: scp -r {{item}} ServerB:/destination
       with_items
: "{{ list_files.stdout_lines }}"


Hope this is helpful !

Best Regards,
Shivharsh
Reply all
Reply to author
Forward
0 new messages