Ansible copy file on remote machines on different location.



down votefavorite


|


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


|

I tried this but it copying the file on both the location on both the machines.

Can someone please help me on this.

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

Many thanks !!