Rendering a template from a module?

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?

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

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.

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?

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

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

I meant “… ec2 instead of a module”

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

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!!

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