How to access tasks parameters from an Ansible task in the Python module code?

136 Views Asked by At

Is it possible to access Ansible tasks parameters from the Python module code?

Specifically, I would like to check if there is a register on the task in order to return a more complete info set.

1

There are 1 best solutions below

0
On BEST ANSWER

Is it possible to access tasks parameters from the Python code of an Ansible module?

Yes, of course. You may have a look into Developing modules and Creating a module, in example

def run_module():
    # define available arguments/parameters a user can pass to the module
    module_args = dict(
        name=dict(type='str', required=True),
        new=dict(type='bool', required=False, default=False)
    )

Specifically, I would like to check if there is a register on the task

Please take note that Registering variables of the Return Values is done

... from the output of an Ansible task with the task keyword register.

This means the task, respective the module called within doesn't know about the fact if the output will become registered or not and since that is done after the execution of the module code and providing the final result.

... in order to return a more complete info set.

Therefore you need to provide an other way of controlling the data structure of the result set.

In example

...
        supports_check_mode=True
...
    if module.check_mode:
...

Or just introduce a separate parameter on your Custom Module like

verbose: True

or

verbose_level: 1 # whereby 0 means false or OFF and i.e. up to 4

which can be checked within the module and simply controls the verbosity of the result set.