Ansible copy file on remote machines on different location.

545 views
Skip to first unread message

niraj pandey

unread,
Mar 3, 2017, 4:33:04 AM3/3/17
to Ansible Project

How can I copy a file from machine A to machine B and machine C on different location. ie:

On machine A I have file abc and I want to copy it on the /tmp area of machine B and /opt area of  machine C.


I tried this so far:


My inventory file:


[webserver]

machine-B dst=/tmp

machine-C dst=/opt


Playbook:


- hosts: webserver

  tasks:

    - name: Transfer file from ServerA to ServerB

      synchronize:

        src: /home/niraj/ansible_testing

        dest: "{{ hostvars[item]['dst'] }}"

      with_items: "{{ groups['webserver'] }}"

        #dest: /home/niraj

        #mode: pull

      delegate_to: machine-A

niraj pandey

unread,
Mar 3, 2017, 4:54:46 AM3/3/17
to Ansible Project
I tried this but it copying the file on both the location on both the machines.

niraj pandey

unread,
Mar 10, 2017, 2:04:54 AM3/10/17
to Ansible Project
Can someone please help me on this.

Kai Stian Olstad

unread,
Mar 10, 2017, 10:25:09 AM3/10/17
to ansible...@googlegroups.com
On 03. mars 2017 10:33, niraj pandey wrote:
> How can I copy a file from machine A to machine B and machine C on
> different location. ie:
>
> On machine A I have file abc and I want to copy it on the /tmp area of
> machine B and /opt area of machine C.
>
>
> I tried this so far:
>
>
> *My inventory file:*
>
>
> [webserver]
> machine-B dst=/tmp
> machine-C dst=/opt
>
> *Playbook:*
>
> - hosts: webserver
> tasks:
> - name: Transfer file from ServerA to ServerB
> synchronize:
> src: /home/niraj/ansible_testing
> dest: "{{ hostvars[item]['dst'] }}"
> with_items: "{{ groups['webserver'] }}"
> #dest: /home/niraj
> #mode: pull
> delegate_to: machine-A

This should do it.

- hosts: webserver
tasks:
- name: Transfer file from ServerA to ServerB
synchronize:
src: /home/niraj/ansible_testing
dest: "{{ dst }}"
delegate_to: machine-A

--
Kai Stian Olstad

niraj pandey

unread,
Mar 22, 2017, 4:31:46 AM3/22/17
to Ansible Project
Many thanks !!


On Friday, March 3, 2017 at 3:03:04 PM UTC+5:30, niraj pandey wrote:
Reply all
Reply to author
Forward
0 new messages