I tried to find an example where I can pull a file from a serverA to a group of servers.
mygroup consists of 10 servers. need to copy that file over to those 10 servers.
here is what I have but its not working exactly. I can do one to one copy no problem without the handlers part.
- hosts: serverA
tasks:
- name: Transfer file from serverA to mygroup
synchronize:
src: /tmp/file.txt
dest: /tmp/file.txt
mode: pull
handlers:
- name: to many servers
delegate_to: $item
with_items: ${groups.mygroup}
You should carefully read the documentation about how ansible works (what is host pattern, what is strategy, what is handler...).
Here's the answer to your question:
---
# Push mode (connect to xenial1 and rsync-push to other hosts)
- hosts: xenial-group:!xenial1
gather_facts: no
tasks:
- synchronize:
src: /tmp/hello.txt
dest: /tmp/hello.txt
delegate_to: xenial1
# Pull mode (connect to other hosts and rsync-pull from xenial1)
- hosts: xenial1
gather_facts: no
tasks:
- synchronize:
src: /tmp/hello.txt
dest: /tmp/hello.txt
mode: pull
delegate_to: "{{ item }}"
with_inventory_hostnames: xenial-group:!xenial1
Inventory:
[xenial-group]
xenial1 ansible_ssh_host=192.168.168.186 ansible_user=ubuntu ansible_ssh_extra_args='-o ForwardAgent=yes'
xenial2 ansible_ssh_host=192.168.168.187 ansible_user=ubuntu ansible_ssh_extra_args='-o ForwardAgent=yes'
xenial3 ansible_ssh_host=192.168.168.188 ansible_user=ubuntu ansible_ssh_extra_args='-o ForwardAgent=yes'
Keep in mind that synchronize
is a wrapper for rsync
, so for this setup to work, there must be ssh-connectivity between target hosts (usually you have ssh connection between control-host and target hosts). I use agent forwarding for this.