# Rendering a template from a module?

**URL:** <https://forum.ansible.com/t/rendering-a-template-from-a-module/18103>\
**Category:** Ansible Developer\
**Created:** [April 28, 2015, 12:28pm UTC](https://forum.ansible.com/t/rendering-a-template-from-a-module/18103 "2015-04-28T12:28:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [April 28, 2015, 12:28pm UTC](https://forum.ansible.com/t/rendering-a-template-from-a-module/18103/1 "2015-04-28T12:28:39Z")

</div>

I’m beefing up our DataDog module and want to create a module that adds monitor configs to /etc/dd-agent/conf.d. Essentially I’m trying to create friendly macro for:

vars:  
dd\_monog\_arg\_1: xyz  
dd\_monog\_arg\_2: abc

- template:  
src: ?datadog-role-templates-path?/monog.yml.j2  
dest: /etc/dd-agent/conf.d/mongo.yml

state: present

To look something:

- dd\_monitor:  
name: mongo  
arg\_1: xyz  
arg\_2: abc  
state: present

Two questions:

1. How can I invoke the template code from a module? I see that in lookup\_plugins it seems pretty simple. Can I used that as a guide?  
But I can’t find any actual modules that use template.

2. How would the module get access to the template files stored in its roles ./templates? Or do the templates need to packaged  
in in libraries?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 28, 2015, 3:46pm UTC](https://forum.ansible.com/t/rendering-a-template-from-a-module/18103/2 "2015-04-28T15:46:07Z")

</div>

this seems to make more sense as a role that uses the template module.

---

<div class="post-metadata">

**Author:** ![Timothy\_Appnel](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@Timothy\_Appnel](https://forum.ansible.com/u/Timothy_Appnel)\
**Post date:** [April 28, 2015, 4:00pm UTC](https://forum.ansible.com/t/rendering-a-template-from-a-module/18103/3 "2015-04-28T16:00:40Z")

</div>

What Brian said. A role will probably be less hassle and a better return on your investment. If you didn't implement this as a role you'd have to pair your module with an action plugin. None of the modules in core use template but the template module shows how you'd use one module (copy) inside of another (template). Look at runner/action\_plugins/template.py and notice the exec\_module calls.

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [April 28, 2015, 4:26pm UTC](https://forum.ansible.com/t/rendering-a-template-from-a-module/18103/4 "2015-04-28T16:26:10Z")

</div>

Certainly less hassle as a role embedded in the current project, but then it would need to repeated across every other project that needs to add datadog monitors.

Is an action plugin something special or different from the modules defined in the docs?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 28, 2015, 4:27pm UTC](https://forum.ansible.com/t/rendering-a-template-from-a-module/18103/5 "2015-04-28T16:27:11Z")

</div>

put the role in galaxy or common git/hg repo and ansible-galaxy can  
download for each project

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [April 28, 2015, 5:11pm UTC](https://forum.ansible.com/t/rendering-a-template-from-a-module/18103/6 "2015-04-28T17:11:54Z")

</div>

That’s not what I meant. Each project will want to provide different values for each of the datadog monitors. And there are dozens and dozens of monitors. It would be like using a role for ec2 instead of a monitor. How could that possibly work?

Anyway, an action plugin seems just perfect. I just need a little wrapper on template. Thanks Timothy for the pointer. It all seems pretty straight forward, if a bit verbose.

(I am assuming that the monitor action plugin packaged with the datadog role will have access to the templates also packed in the in the role…)

-barry

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [April 28, 2015, 5:13pm UTC](https://forum.ansible.com/t/rendering-a-template-from-a-module/18103/7 "2015-04-28T17:13:31Z")

</div>

I meant “… ec2 instead of a module”

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 28, 2015, 6:57pm UTC](https://forum.ansible.com/t/rendering-a-template-from-a-module/18103/8 "2015-04-28T18:57:22Z")

</div>

Actually not hard as you can pass data to a role, which can also have  
sane defaults.

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [April 29, 2015, 12:54am UTC](https://forum.ansible.com/t/rendering-a-template-from-a-module/18103/9 "2015-04-29T00:54:39Z")

</div>

Ha! I just woke up with that realization Brian.

I forgot that you can pass variables directly to each role and include a role multiple times. I’ve been writing Chef way too long.

Thanks!!

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 1, 2015, 6:15pm UTC](https://forum.ansible.com/t/rendering-a-template-from-a-module/18103/10 "2015-05-01T18:15:25Z")

</div>

I should have known! it tends to make us look for complicated coded  
solutions even when a simpler alternative is right there.
