I'm working with ansible and python, and I am making use of the f5networks module, particularly the bigip_device_info module.
The module parameters are currently:
user: The f5 host username password: The f5 host password validate_certs: A flag server: The f5 host to connect to.
I'd like it to instead take an array of servers, and return an array of responses, but I'd like to do this in python, because the .yml file gets messy with the jinja templating.
I've found that I can resolve the imports that I need:
from ansible_collections.f5networks.f5_modules.plugins.modules.bigip_device_info import ArgumentSpec, ModuleManager, F5ModuleError
The current implementation of the module has a main() function as follows:
def main():
spec = ArgumentSpec()
module = AnsibleModule(
argument_spec=spec.argument_spec,
supports_check_mode=spec.supports_check_mode
)
try:
mm = ModuleManager(module=module)
results = mm.exec_module()
ansible_facts = dict()
for key, value in iteritems(results):
key = 'ansible_net_%s' % key
ansible_facts[key] = value
module.exit_json(ansible_facts=ansible_facts, **results)
except F5ModuleError as ex:
module.fail_json(msg=str(ex))
if __name__ == '__main__':
main()
I would like to do something like this:
class BigipDeviceInfo:
@staticmethod
def get_device_info(**module_args):
# Do something to use module_args as the input for module
spec = ArgumentSpec()
mock_module = AnsibleModule(argument_spec=spec,
supports_check_mode=spec.supports_check_mode)
try:
mm = ModuleManager(module=mock_module)
results = mm.exec_module()
return results
except F5ModuleError as e:
return e
@staticmethod
def run(server_array: list):
res = []
for server in server_array:
res.extend(BigipDeviceInfo.get_device_info(**server))
return res
if __name__ == "__main__":
module_args = dict(
server_array=dict(type='list', required=True)
)
module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=False)
module.exit_json(**{"output": BigipDeviceInfo.run(**module.params)})
I'm new to ansible, and although I am sure I could figure out a hacky way to do things, I'm hoping for something that follows best practices. The main issue being that I'm not quite sure what's happening to collect the arguments using the ArgSpec, and where I can set those myself to allow for this to work properly, and the 17,000 SLOC file hasn't offered many insights. Is there a way to manually evaluate the module? (Other than importing main, and piping it directly through the shell, via something like os.system("python3 my_module.py"))