I am trying to figure out a method of pushing configuration files to groups of servers. The requirement is that all servers receive file1, most servers receive file2, some servers receive file3, other servers receive file4, and so on. I am trying to use the ‘copy’ task for this, but I haven’t found a good method of looping over the file names.
I have set up the inventory file with the servers in multiple groups based upon the files they need to receive:
[all_servers]
server1
server2
server3
server4
[most_servers]
server1
server3
server4
[some_servers]
server1
server3
[other_servers]
server2
server4
Then in the group_vars/groupname files, I have:
group_vars/all
config_file: file1
group_vars/most_servers
config_file:file2
group_vars/some_servers
config_file:file3
group_vars/other_servers
config_file:file4
I am trying to find a way to loop through all of the groups that a server is a member of and collect the configuration file names. I have tried using with_items and hostvars[inventory_hostname].group_names, but I haven’t found a way to extract the contents of config_file for each of the groups. The only other way I can think of to do this is to put all of the configuration files names that are to be installed on a group of servers into that group’s group file:
group_vars/some_servers
config_file:
- file1
- file2
- file3
group_vars/other_servers
config_file:
- file1
- file4
This works, but it means that I have to make sure that all of the group files are updated together each time we make a change to the configuration file list instead of making the change to just one file. The former can present a greater chance of introducing errors because a group file might be missed, there are more places where typos can be introduced, etc.
So, is there a way to loop through the groups that a server is a member of?
Thanks,
-Mark