Hello,
I have been using Ansible in production for about a year now. I use it for both configuration management and deployments. I created a bunch of cruft since I was learning Ansible at the time and now trying to clean that up during a refactoring of the playbooks. So one issue I see in my playbooks is that the roles I created were very specific to my vars and are not general purpose (I would not be able to publish said role to Ansible Galaxy). To have general purpose roles the vars those roles except must verbose. So for example I have applications that need to be load balanced. Those applications have runtime parameters such as specific port number that need to be defined in vars files and the load balancer also needs to know about those same port numbers and the number of rules needed. I really really want to define those port variables once and only once, then mix those into the general purpose vars to allow the loadbalancer to know about those ports and rules. If I change the port number for some reason in my application vars file then I don’t need to worry about also changing them in other vars files.
I started looking for a solution for this. I see there is string interpolation in the yml files which would allow you to do some sharing but does not really solve the problem where the loadbalancer vars structure is different than the applications vars structure. So I was able to find a solution to the issue, however it feels very janky. Ansible’s templating module gives me a way to combine multiple vars sources into a new single vars file. I ended up creating a var/templates and vars/generated folders in my vars folder. In the template file I can describe how I want to combine the vars together to generate a new vars file. Then in my playbook I can run the template module locally to generate a vars file to work with my load balancer module. The result is a verbose loadbalancer.yml file, something that I would not want to maintain by hand, but keeps the loadbalancer role general purpose and if I did add another application into my applications vars or changed ports or names, I would not have to worry about the loadbalancer.yml file.
I think the templating vars will work ok, but again feel kind of wrong. I’m not sure what the better solution is. Maybe some kind of way to extend roles with some kind of vocabulary to combine multiple vars into a single variable? Any suggestions or feedback on how to do this a better way would be great.
Cheers!