Can a developed Ansible module include or extend an Ansible Core module?

Generally speaking, ansible allows sharing code through “lib/ansible/module_common.py” to make writing functionality easier.

It does not, however, make it possible for one module to call another, which has not, to date, really been needed – that’s not entirely true, we used to have something like this for file and copy until we got smart and moved the file attribute code into common :slight_smile:

It seems like since url access is frequent enough we could make a common function in module common for url downloads – IF we modify the get_url code to also use it so we aren’t repeating ourselves.

Just to chip in, I’ve looked for something like this as well.
We have some specific needs for yum that the current yum module doesn’t quite provide (downgrade, exact version upgrade)
My choices were:

  1. Write our own proprietary localyum module and maintain it ourself (use current yum as a reference)

  2. Enhance ansible’s yum, and lobby to have our changes/enhancements accepted

  3. Try to express our incremental needs in Ansible itself, i.e. use Ansible’s yum, surround it with just enough Ansible logic to achieve our needs

  4. is a fair amount of effort given how comprehensive Ansible’s yum already is. It will be relatively high maintenance, and fails to leverage Ansible’s yum (or entails keeping constant tabs on Ansible’s yum on a regular basis to down-merge)

  5. Our needs may not align with the community’s needs

  6. is the path I experimented with, but Ansible is expressly not Python, so it’s cumbersome to express decision/logic

Long story short, an Option 4) “Ability to write a new module that uses an existing module” would have helped.

I have similar use cases too. In one of them, for instance, I just
need to produce a template at runtime and replace some variables.
Being able to reuse the code of the template module would be
wonderful.

.a.

I would recommend this.

So, seems like the short answer is no, this is not currently supported.

Changing the get_url functionality to be in module_common seems like it would be worth it, as this could be relatively common. Extending a module would be ideal, but would probably be a bigger change.

I already wrote my module, copying the get_url module. When I get a chance, I’ll move the get_url functionality to module_common and submit a pull request. If it gets accepted, when we upgrade our Ansible version, I’ll just refactor my module to use that.

Thanks for the help and advice.

Well, you can access the way template works by writing an action plugin, but it’s more involved than writing a simple client module.

+1 to moving get_url code into common, that’s come up a few times.

If you want downgrade in yum then you'll want to have that behave in
the face of rhel5. So check your versions of yum and have the downgrade
command error out appropriately.

Other than that - I don't think that will be difficult

-sv