Develope Module and include non python files

When developing a module, how can i include some non python files (force ansiballz to include it)? Why do i need this? I’ve modeled a large structure of configuration objects and it’s dependencies in yaml format for easier writing. the module needs this structure to work. Furthermore, i also have some default data (jsonschemas, xmlschemas…) as files which the module needs. I dont want to have a dedicated “copy task” in the playbook since these files should be part of the module itself.

There is no current way to do this as they were designed ONLY for python code, I would suggest creating an action plugin of the same name that takes care of copying the data files and then executing the module. unarchive might be simplest example to follow, it copies archive in this case before running module.

2 Likes

Another option would be to transform them into valid Python code that the module machinery can ship as a module util:

plugins/module_utils/yamldata.py:

FOO = r'''
a: eh
b: bee
c: sea
'''

plugins/modules/testyamldata.py:

import yaml

from ansible.module_utils.basic import AnsibleModule
from ..module_utils.yamldata import FOO

def main():
    module = AnsibleModule(
        argument_spec={},
    )

    module.exit_json(changed=False, data=yaml.safe_load(FOO))


if __name__ == '__main__':
    main()

Result:

TASK [flowerysong.test.testyamldata] *******************************************
ok: [localhost] => 
    changed: false
    data:
        a: eh
        b: bee
        c: sea
3 Likes

Many thanks for your hint! Even more easy, since i’m using it only on the control node, i changed the whole stuff to be a action module instead of a normal module… this way it works like a charm and dont get “ansiballed”

cheers, thom