Thanks a lot Michael, "delegate_to" is indeed what I was looking for!
When writing the task to copy the ssh pub key from server A to server B, I encountered what seems to be
https://github.com/ansible/ansible/pull/2981 (null MD5 returned on fetch used with sudo). I found this workaround using a registered variable :
- name: Backupninja | Fetch public ssh key
command: cat /root/.ssh/id_rsa.pub
register: root_pub_key
- name: Backupninja | Add public ssh key to backup account
delegate_to: $backup_host
authorized_key: user=$backup_user key="{{root_pub_key.stdout}}"
I also added this task to add server B's public ssh key to server A's known_host file:
- name: Backupninja | Add backup host to known_keys
shell:
touch ~/.ssh/known_hosts &&
ssh-keygen -R {{ backup_host }} &&
ssh-keyscan -H {{ backup_host }} >> ~/.ssh/known_hosts
Works like a charm!
Sylvain