Utilizing a template from a module

I am writing a module which builds a config file. This template requires a bit of logic to determine the proper directives to include/exclude. This is a no-brainer in jinja, but fairly complex in pure stdlib. However, modules look to execute on the target-host, so installing jinja as a requirement doesn’t seem ideal.

Has anyone had success levering templates inside a custom module? Ideally it is evaluated on the host executing the ansible run, where we have access to jinja and template’s idempotency.

John

templates get built on ‘master’, you do not need jinja on the targets​

I don’t see an example of how to do this from another module. Writing this as a task is trivial. I want to write a module which utilizes template functionality.

It seems your module may wish to just require jinja2. Action plugins are large overhead that are not intended to be written frequently, mostly there to facilitate server-side heavy lifting.

However, from discussion, I believe you’re trying to write a logrotate module - to me, logrotate.d is probably easiest to just template, .d directory systems are magic when available, making much of the module unneeded.

Yeah, you are right. I could simply require jinja2, I just felt it was somewhat of an unnecessary dep. I found it somewhat difficult to cleanly implement a template[1] in pure stdlib, which led me to this original post. Thx for the advice Brian and Michael.

[1] https://github.com/retr0h/ansible-logrotate/blob/master/templates/logrotate.conf.j2