Making a collection compatible with data tagging

As mentioned in Data Tagging preview and testing, collection maintainers need to test their collections to ensure compatibility with core-2.19.

This is the topic where you can add your advice to other collection maintainers on how to achieve this.

Related Links

1 Like

If your unit tests start failing because you mock module arguments like this:

from ansible.module_utils import basic
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
basic._ANSIBLE_ARGS = to_bytes(args)
... execute the module ...

This no longer works, you need to use the ansible.module_utils.testing.patch_module_args() context manager instead:

from ansible.module_utils.testing import patch_module_args
with patch_module_args(args):
    ... execute the module ...

One way to handle this for both older and newer ansible-core versions is to create your own context manager:

@_contextlib.contextmanager
def set_module_args(args):
    """
    Context manager that sets module arguments for AnsibleModule
    """
    if '_ansible_remote_tmp' not in args:
        args['_ansible_remote_tmp'] = '/tmp'
    if '_ansible_keep_remote_files' not in args:
        args['_ansible_keep_remote_files'] = False

    try:
        from ansible.module_utils.testing import patch_module_args
    except ImportError:
        # Before data tagging support was merged, this was the way to go:
        from ansible.module_utils import basic
        serialized_args = to_bytes(json.dumps({'ANSIBLE_MODULE_ARGS': args}))
        with patch.object(basic, '_ANSIBLE_ARGS', serialized_args):
            yield
    else:
        # With data tagging support, we have a new helper for this:
        with patch_module_args(args):
            yield

Then you can always run:

with set_module_args({...}):
    ... execute the module ...
1 Like