You can't execute a module from within other module, because modules in Ansible are self-contained entities, that are packaged on the controller and delivered to remote host for execution.
But there are action plugins for this situations. You can create action plugin spt_module
(it will be executed locally on Ansible controller) that can in turn execute several different modules, based on lan/vnic
parameters.
This is how your action (spt_module.py
) plugin may look like (very simplified):
from ansible.plugins.action import ActionBase
class ActionModule(ActionBase):
def run(self, tmp=None, task_vars=None):
result = super(ActionModule, self).run(tmp, task_vars)
vnic_list = self._task.args['LAN']['VNIC']
common_args = {}
common_args['name'] = self._task.args['name']
res = {}
res['create_server'] = self._execute_module(module_name='ls_server_module', module_args=common_args, task_vars=task_vars, tmp=tmp)
for vnic in vnic_list:
module_args = common_args.copy()
module_args['other_args'] = vnic['other_args']
res['vnic_'+vnic['name']] = self._execute_module(module_name='vnic_ether_module', module_args=module_args, task_vars=task_vars, tmp=tmp)
return res
Code is not tested (bugs, typos possible).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…