I'm using Ansible to manage a fleet of dissimilar cloud linux servers. I built a script that pulls backups down to a backup server via rync over ssh. All of the host vars are managed in host_vars/example.com.yml
files, each containing the following:
ansible_host: example.com
ansible_user: user
ansible_port: 22
ansible_ssh_private_key_file: "~/.ssh/id_ed25519"
They are grouped in the hosts
file like so:
[webservers]
example.com
foo.com
bar.com
[backup]
backupserver.domain.com
I have a backup role for the backup group containing the backupserver.domain.com server comprising the rsync scripts, but I have to manually replicate the ansible host_vars into that backup role to generate an SSH config containing all the information already in the host_vars/*.yml
files.
So my question is, within a role is it possible to iterate through the host variables of another host outside of the current host and group? Something like:
{% for item in var_hosts_group %}
Host {{ item.ansible_host }}
Hostname {{ item.ansible_host }}
User {{ item.ansible_user }}
Port {{ item.ansible_port }}
IdentityFile {{ item.ansible_ssh_private_key_file }}
{% endfor %}
Then an rsync script would be:
{% for item in var_hosts_group %}
rsync -a {{ item.ansible_host }}:/remotebackup /localbackup
{% endfor %}
This would enable me to generate the necessary templates/scripts for the webservers on the backup server and keep everything nice and DRY. Is this possible? Thanks!