# Reusable constructed vars/library

**URL:** <https://forum.ansible.com/t/reusable-constructed-vars-library/13797>\
**Category:** Ansible Project\
**Created:** [October 29, 2013, 4:59pm UTC](https://forum.ansible.com/t/reusable-constructed-vars-library/13797 "2013-10-29T16:59:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Dewey](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@John\_Dewey](https://forum.ansible.com/u/John_Dewey)\
**Post date:** [October 29, 2013, 4:59pm UTC](https://forum.ansible.com/t/reusable-constructed-vars-library/13797/1 "2013-10-29T16:59:17Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 30, 2013, 1:57am UTC](https://forum.ansible.com/t/reusable-constructed-vars-library/13797/2 "2013-10-30T01:57:47Z")

</div>

" 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?

---

<div class="post-metadata">

**Author:** ![John\_Dewey](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@John\_Dewey](https://forum.ansible.com/u/John_Dewey)\
**Post date:** [October 30, 2013, 2:23am UTC](https://forum.ansible.com/t/reusable-constructed-vars-library/13797/3 "2013-10-30T02:23:51Z")

</div>

> " 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. 😕

---

<div class="post-metadata">

**Author:** ![John\_Dewey](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@John\_Dewey](https://forum.ansible.com/u/John_Dewey)\
**Post date:** [November 15, 2013, 12:40am UTC](https://forum.ansible.com/t/reusable-constructed-vars-library/13797/4 "2013-11-15T00:40:55Z")

</div>

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/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/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/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/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/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](https://github.com/blueboxgroup/ursula/blob/blueboxgroup/clustered-rabbit/roles/nova-common/templates/etc/nova/nova.conf#L96)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 20, 2013, 3:54am UTC](https://forum.ansible.com/t/reusable-constructed-vars-library/13797/5 "2013-11-20T03:54:33Z")

</div>

Sorry that doesn’t really exist.

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