Push multiple configuration files to multiple servers

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

If you have ever used Puppet, this would be doable with Hiera. Very keen to hear how this sort of thing is done in Ansible.

GS

I am in Portland, OR, just a few blocks away from Puppet Labs. There is plenty of support for Puppet around here. But I like the Ansible model of using ssh and sudo better. That way I don’t have to install a client, another potential point of access to our servers, and it allows us to split out administrative roles to other groups; not everyone needs root access to perform a task. But not being able to iterate across multiple groups has been a major stumbling block for me. I figured out a way of doing that in templates because Jinja2 has better support for looping, but that means I am putting logic into the templates, something that purists consider to be a no-no. Frankly, IMHO there just needs to be better support for looping in playbooks. A real “for” statement would be very useful here. Better access to the group variables would be good too, something similar to the hostvars variable (groupvars[group_name].variable).
-Mark

  • hosts: some_servers
    tasks:
  • template: src={{ item }} dest = {{ where_to }}
    with_items: some_files

Happy to answer specific questions, but please avoid product comparisons so I can resist telling you what I think about them :slight_smile:

" But not being able to iterate across multiple groups has been a major stumbling block for me. "

I’m not sure what this means, in particular. You can iterate across the members of groups, but I’d need to see a specific example of something you are trying to model.