How to use ansible to synchronize directories between groups of servers

ansibleansible-playbook

I am currently migrating some data between servers and need to make sure I'm copying some data directories while still preserving mappings between old hosts and new hosts. I'm looking at using ansible to do this due to the sheer number of hosts that need files copied.

Example Ansible hosts file:

[source_hosts]
olddb1
olddb2
olddb3

[destination_hosts]
newdb1
newdb2
newdb3

I would like to copy files between servers like so:

olddbX:/var/opt/<unique dir with db in name>/data --> newdbX:/var/opt/<unique dir with db in name>/data

I've seen many examples of using ansible.posix.synchronize to perform an rsync between two servers, but not between groups of servers with directory names that are slightly variable. In addition I need to ensure that a particular data directory doesn't end up on the wrong host. (i.e. I need olddb1's data to end up on newdb1, and so on…)

My questions:

  • How can I use ansible to synchronize directories between groups of servers?
  • How do I handle the directory names not being static on either end?

Best Answer

I don't see a possible way to do it easilly between two groups as presented. Meanwhile, unless you have thousands of hosts, it is quite easy to setup with a little rework for your inventory. Here is one possible way to do it (untested, written on spot, probably needs some adjustements, requires rsync on both source and dest host)

(Note I used yaml as I find it more convenient and legible but feel free to keep this in ini format if you whish)

inventories/prod/hosts.yml

---
db_hosts:
  hosts:
    newdb1:
      source_host: olddb1
      db_name: my_db1
    newdb2:
      source_host: olddb2
      db_name: my_db2
    newdb3:
     source_host: olddb3
     db_name: some_other_db

playbook.yml

---
hosts: db_hosts

tasks:
  - name: Copy db files from old host
    synchronize:
     src: rsync://{{ source_host }}/var/opt/{{ db_name }}/data
     dest: /var/opt/{{ db_name }}/data

For more info and tunning this to your exact needs: https://docs.ansible.com/ansible/latest/collections/ansible/posix/synchronize_module.html