从另一个 Ansible 模块调用 Ansible 模块? [英] Calling an Ansible Module from another Ansible Module?
问题描述
是否可以通过编程方式从另一个 Ansible 模块调用 Ansible 模块?
Is it possible to call an Ansible module from another Ansible module programmatically?
我一直通过 Python (ucsmsdk) 和 Ansible 与 Cisco UCS 合作,以创建一种自动化服务配置文件模板(从现在开始的 SPT)的方法.我已经创建了 apis 和 模块 符合相应 Git 存储库中设置的标准.
I have been working with Cisco UCS via Python (ucsmsdk) and Ansible to create a means of automating service profile templates (SPTs from now on). I have created apis and modules conforming to the standards set in the respective Git repos.
虽然我能够使用 Ansible playbook 创建这些 SPT,但它需要大量重复使用的属性来创建每个单独的项目并遵循它们的父/子关系的长链.我想删除所有这些重用,并通过提供所有参数来简化项目的创建,我需要一次性完成它们的结构.
While I am able to create these SPTs with an Ansible playbook, it requires a lot of reused properties to create each individual item and follow their long chains of parent/child relationships. I would like to remove all this reuse and simplify the creation of the items by feeding all the parameters I need to do this in one go with their structure in tact.
下面的例子显示了我想要的当前系统.
The example below shows the current system to what I would like.
tasks:
- name: create ls server
ls_server_module:
name: ex
other_args:
creds:
- name: create VNIC Ether
vnic_ether_module:
name: ex_child
parent: ex
other_args:
creds:
- name: create VNIC Ether If (VLAN)
vnic_ether_if_module:
name: ex_child_child
parent: ex_child
creds:
- name: create VNIC Ether
vnic_ether_module:
name: ex_child_2
parent: ex
other_args:
creds:
需要
tasks:
- name: create template
spt_module:
name: ex
other_args:
creds:
LAN:
VNIC:
- name: ex_child
other_args:
vlans:
- ex_child_child
- name: ex_child_2
other_args:
目前,我唯一的障碍是通过调用这些以动态和编程方式创建对象的模块来诱导一些代码重用.
Currently, my only barrier is inducing some code reuse by calling these modules that create the object in a dynamic and programmatic way.
推荐答案
你不能从其他模块中执行一个模块,因为 Ansible 中的模块是自包含实体,它们被打包在控制器上并传送到远程主机执行.
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.
但是有针对这种情况的操作插件.您可以创建动作插件 spt_module
(它将在 Ansible 控制器上本地执行),该插件可以根据 lan/vnic
参数依次执行多个不同的模块.
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.
这就是你的 action (spt_module.py
) 插件的样子(非常简化):
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
代码未经测试(可能存在错误、拼写错误).
这篇关于从另一个 Ansible 模块调用 Ansible 模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!