在模块代码中使用ansible_facts [英] use ansible_facts in module code

查看:90
本文介绍了在模块代码中使用ansible_facts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create my own ansible module (which will update cmdb) and i am looking how to use ansible_facts in module code ?

我的模块脚本的示例是:

example of my module script is :

#!/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? (甚至有可能吗?).

What variable i should use to call ansible_facts in module script? (And is it even possible? ).

推荐答案

我怀疑是否可以从模块内部进行此操作,因为它们是在具有预定义参数的远程计算机上下文中执行的.

I doubt this is possible from inside module itself, because they are executed in the context of remote machine with predefined parameters.

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

But you can wrap your module with action plugin (that is executed in local context), collect required data from available variables and pass them as parameters to your module.

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

Like this (./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的值.

In this case if your module expect mem_size parameter it will be set to ansible_memtotal_mb's value with action plugin.

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

Module example (./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:

这篇关于在模块代码中使用ansible_facts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆