Search code examples
ansiblenamespaces

How does ansible search namespaces for modules?


I need to write ansible code compatible for multiple versions of ansible. Older versions of ansible do not have namespaces and newer ones do. In one instance, a role invoked an "ansible.builtin.yum" module, which failed in earlier versions of ansible. For now, it was sufficient remove the prefix. Where is the namespace behavior documented? Presumably, un-prefixed module names are searched in a list of namespaces - what is that list? Is there a namespace design document? The closest I've been able to find to a namespace design document is https://galaxy.ansible.com/docs/contributing/namespaces.html which makes invalid use-case assumptions.

Right now, removing the "ansible.builtin." from the names for such prefixed modules works, but I want to understand the namespace mechanism for when that stops working with newer versions of ansible.


Solution

  • What you get depends on the configuration. See

    For example, if you use yum

    - hosts: all
      tasks:
        - yum:
            name: nginx
    

    the first one found will be used

    shell> ansible-doc -t module -l | grep yum
    ansible.builtin.yum
    Manages packages with the ...
    ansible.builtin.yum_repository
    Add or re...
    community.general.yum_versionlock
    Locks / unlocks an installed package(s) from being updated b...
    

    In a standard Ansible installation, there is only one yum among the installed modules. Therefore the doc says:

    In most cases, you can use the short module name yum

    The same rules will apply to your custom module:

    • If you plan to use your module within your project(s) only put it into the DEFAULT_MODULE_PATH

    • If you have more custom stuff (roles, modules, filters, plugins, ...) you might want to create a collection

    • If you think the module might be useful for others contribute it.