Reusable constructed vars/library

I have a case where I am constructing the same string in three different role templates.
https://github.com/blueboxgroup/ursula/blob/master/roles/nova-common/templates/etc/nova/nova.conf#L55

It is getting to the point where I should have a library or register a var which constructs this. However, it doesn’t
look as though a library has access to hostvars, unless explicitly passed in, and I can’t figure out how I could
register a var, that simply constructs this string.

Having the string constructed is necessary, to keep the manual configuration to a minimum, but I’m not sure how
to tackle such an issue. Sure, I can leave it as is, but it feels like I should clean up the duplication now that I’ve done
it three times in three diff places.

John

" However, it doesn’t look as though a library has access to hostvars,"

By library do you mean module?

Anyway, I don’t see your template as being too unwieldy, I think perhaps maybe you should not worry about it?

" However, it doesn’t look as though a library has access to hostvars,"

By library do you mean module?

Ya, I am referring to a module.

Anyway, I don’t see your template as being too unwieldy, I think perhaps maybe you should not worry about it?

It’s not too bad, it gives me bad feelings duplicating the exact same logic in three other places. Maybe I am worrying about something insignificant. :confused:

Hi Michael -

To follow-up on our twitter thread.

Having the jinja templates construct strings feels a bit naughty. Especially when I am copy pasting code into different templates.
I was hoping a module would be able to deal with iterating over a given group, constructing the string/list, and returning it and/or registering a fact.
That type of code feels like it should be in python vs a template. I attempted to pass the result of groups[‘ROLE’] to a module, but they can
only accept strings.

Anyways, I added some use cases [1] for what we are doing. Was hoping for some reasonable feedback on ways of improving this.

John

[1]:

https://github.com/blueboxgroup/ursula/blob/master/roles/horizon/templates/opt/stack/horizon/openstack_dashboard/local/local_settings.py#L85
https://github.com/blueboxgroup/ursula/blob/master/roles/keystone-common/templates/etc/keystone/keystone.conf#L21

https://github.com/blueboxgroup/ursula/blob/master/roles/nova-common/templates/etc/nova/nova.conf#L55
https://github.com/blueboxgroup/ursula/blob/master/roles/keystone-common/templates/etc/keystone/keystone.conf#L21
https://github.com/blueboxgroup/ursula/blob/blueboxgroup/clustered-rabbit/roles/neutron-common/templates/etc/neutron/neutron.conf#L9
https://github.com/blueboxgroup/ursula/blob/blueboxgroup/clustered-rabbit/roles/nova-common/templates/etc/nova/nova.conf#L96

Sorry that doesn’t really exist.

You could probably make use of Jinja2 includes to pull in pieces of templates.