Can someone help me with nested loop construction?
I am creating simple backup role, which will prepare client servers for backup and it will prepare backup server also. Backups will be downloaded from clients by backup server at defined time. I want to define different time and different folders for each client (by host_vars).
My workflow:
backup server
- install packages
- create backup user
- create backup directory
client
- install packages
- add backup's server ssh key to authorized_keys (with restrictions)
- some ssh settings
- reload sshd
backup server
- set cron jobs for download backups (each client should has it's own cron entry - different time and different set of backuped folders)
I have first version of this role, which was applied to client group of servers. Backup server was configured with help of delegate_to: statement. But there was problem with creating cron file on backup server, because there was something like 'race condition'. When playbook finishes, there was only one random entry in the backup's server cron file. But I expected that there should be entries for all client servers. I tried to open issue https://github.com/ansible/ansible/issues/74189 - I got answer, that I should change my access to this problem.
My second attempt was that I rewrite ansible role. Then I can apply it on backup server instead of client group. Now I am using delegate_to: on the group of client servers.
Example of (simplified) expected cron job on backup server:
0 0 * * * backup -include /home --include /var/www --include /srv --exclude '**' backup.lab.local::/ /home/backup/srv1.lab.local
# -> similar entry for srv2
# -> similar entry for srv3
My scenario:
# hosts file
[backup_servers]
backup.lab.local
[testing_servers]
srv1.lab.local
srv2.lab.local
srv3.lab.local
Example of host_vars file:
# host_vars/srv1.lab.local
backup_folders:
- /home
- /var/www
- /srv
I am stuck on task which creates cron entries on backup server. I need to loop groups['testing_servers'] and inside this loop I need to create another loop of hostvars[<each_host_from_group>]['backup_folder'].
How to do it, please?