当前位置: 首页 > 面试题库 >

在模块代码中使用ansible_facts

丁高峯
2023-03-14
问题内容

我正在尝试创建自己的ansible模块(它将更新cmdb),并且正在寻找如何在模块代码中使用ansible_facts?

我的模块脚本的示例是:

#!/usr/bin/python

from ansible.module_utils.basic import *

import json, ast

from servicenow import ServiceNow
from servicenow import Connection


def __get_server_info(table,server_name="", sys_id=""):
     if sys_id == "":
       return table.fetch_one({'name': server_name})

     if server_name == "":
       return table.fetch_one({'sys_id': sys_id})

def __update_cmdb_hwinfo(table, sys_id, server_name=""):
    return table.update({'sys_id': sys_id,{'hw_ram': 'Here for example i want to put ansible_facts about server ram size'})


def main():


    fields = {
       "snow_instance": {"required": True, "type": "str"},
       "snow_username": {"required": True, "type": "str"},
       "snow_password": {"required": True, "type": "str"},
       "server_name":   {"required": True, "type": "str" },
       "api_type":      {"default": "JSONv2", "type": "str"},
    }

    module = AnsibleModule(argument_spec=fields)
    snow_connection = Connection.Auth(username=module.params['snow_username'], password=module.params['snow_password'], instance=module.params['snow_instance'], api=module.params['api_typ
e'])
    server = ServiceNow.Base(snow_connection)
    server.__table__ = 'cmdb_ci_server_list.do'

    machine = __get_server_info(server, )
    ## Define connection object to ServiceNow instance
    module.exit_json(changed=False, meta=module.params, msg=machine)


if __name__ == '__main__':
    main()

我应该使用什么变量在模块脚本中调用ansible_facts?(还有可能吗?)。


问题答案:

我怀疑这是否可以从模块本身内部实现,因为它们是在具有预定义参数的远程计算机的上下文中执行的。

但是,您可以使用动作插件(在本地上下文中执行)包装模块,从可用变量中收集所需的数据,并将其作为参数传递给模块。

像这样(./action_plugins/a_test.py):

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)

        module_args = self._task.args.copy()
        module_args['mem_size'] = self._templar._available_variables.get('ansible_memtotal_mb')

        return self._execute_module(module_args=module_args, task_vars=task_vars, tmp=tmp)

在这种情况下,如果您的模块期望mem_size参数将通过ansible_memtotal_mb操作插件将其设置为的值。

模块示例(./library/a_test.py):

#!/usr/bin/python

def main():
    module = AnsibleModule(
        argument_spec = dict(
            mem_size=dict(required=False, default=None),
        ),
        supports_check_mode = False
    )

    module.exit_json(changed=False, mem_size=module.params['mem_size'])

from ansible.module_utils.basic import *
from ansible.module_utils.urls import *

main()

测试手册:

---
- hosts: all
  tasks:
    - a_test:


 类似资料:
  • 我需要在我的脚本中直接安装PyPi的包。也许有一些模块或(,等)功能允许我只执行类似的操作,请求将被安装到我的virtualenv中。

  • https://confluence.atlassian.com/doc/noformat-macro-139545.html

  • 模块的定义 模块是自动运行在严格模式下并且没有办法退出运行的JavaScript代码。 模块可以是函数、数据、类,需要指定导出的模块名,才能被其他模块访问。 //数据模块 const obj = {a: 1} //函数模块 const sum = (a, b) => { return a + b } //类模块 class My

  • 问题内容: 我很好奇在使用模块后是否有任何好的方法可以卸载模块。在某些情况下,我需要使用引入大量代码的模块,但是很少使用它们(例如作为管理工具),但是我犹豫使用它们,因为在此之后它们可能只会浪费内存,这可能会更好在其他地方使用。是否有任何方法可以卸载它们,无论是显式卸载还是在一段时间不使用它们时允许系统卸载它们? 问题答案: 是的,可以直接访问模块缓存: 请注意,如果您的代码中包含对您想摆脱的这些

  • examples.adjacency_list.adjacency_list examples.association.basic_association examples.association.dict_of_sets_with_default examples.association.proxied_association examples.asyncio.async_orm example

  • 我有一个在kotlin的‘基地’项目配置与gradle DSL。在基我有模块'app'和模块'library'。模块应用程序使用模块库。我想从模块应用程序的测试(准确地说是库的testUtils包)访问模块库的测试。 我还检查了代码所在的问题: 我需要在库的源代码中添加,这样在应用程序的测试中,我就可以使用库的测试包,而在应用程序的源代码中,我就不能使用库的测试了。 谢谢你。