chaining modules

Ansible has a concept of chaining modules – e.g., template both templates and then uploads. I’m working on an ldap module that, naturally, takes and ldif file as an argument.

Present usage is like so:

  • name: create temp dir
    action: file path=/tmp/ansible-ldap/ state=directory
  • name: template & upload sample.ldif
    action: template src=files/sample.ldif.j2 dest=/tmp/ansible-ldap/sample.ldif
  • name: apply ldif
    action: ldap action=modify ldif=/tmp/ansible-ldap/sample.ldif
  • name: delete temp file
    action: file path=/tmp/ansible-ldap/sample.ldif state=absent
  • name: delete temp dir
    action: file path=/tmp/asnbile-ldap/ state=absent

… which is pretty verbose …

What would be nice, I think, would be this:

  • name: apply ldif
    action: ldap action=modify ldif=files/sample.ldif.j2

I’m pretty sure it would be hard to do this with the current code base, but wonder if there’s any planned refactoring that would make this – or something similar – a little more feasible.

On a more practical note, I think it could be reduced to two tasks if ansible provided a var pointing to the current tmp dir which it would automatically clean up. E.g.:

  • name: template & upload sample.ldif
    action: template src=files/sample.ldif.j2 dest=${ansible_tmp_dir}/sample.ldif
  • name: apply ldif
    action: ldap action=modify ldif=/tmp/ansible-ldap/sample.ldif

… and ansible would take care of the rest.

I’m fishing for ideas… or for the whole idea to be shot down ;-).

Ansible has a concept of chaining modules – e.g., template both templates and then uploads. I’m working on an ldap module that, naturally, takes and ldif file as an argument.

For purposes of explaining what you are talking about to other folks on list, this is entirely a internals thing, and not a user land thing.

It’s how you can specify “mode=700” when using ‘template’ or ‘file’, or ‘copy’, without the system needing to have 3 separate modules.

It’s basically transparent and only used for file/copy/template.

(Ok, educational segment over…)

…snip…

What would be nice, I think, would be this:

  • name: apply ldif
    action: ldap action=modify ldif=files/sample.ldif.j2

I’m pretty sure it would be hard to do this with the current code base, but wonder if there’s any planned refactoring that would make this – or something similar – a little more feasible.

I don’t think there’s going to be a generic way to say “template this into a tempdir and then run against this template” in Python or anything.

However, this looks like a good case for using parameterized include files to hide that complexity, which is why the feature exists:

include ldapdiff somevar=xyz someothervar=xyz

So that the implementation is mostly hidden away from the play, and also reusable between plays.

So you still end up having whatever steps in playbook land, you just can reuse them as an include file.

On a more practical note, I think it could be reduced to two tasks if ansible provided a var pointing to the current tmp dir which it would automatically clean up. E.g.:

  • name: template & upload sample.ldif
    action: template src=files/sample.ldif.j2 dest=${ansible_tmp_dir}/sample.ldif
  • name: apply ldif
    action: ldap action=modify ldif=/tmp/ansible-ldap/sample.ldif

This seems reasonable.

And if and when implemented, another item to be added to my hoped-for http://ansible.github.com/variables.html page.