Ansible:从自定义模块中访问主机/组变量 [英] Ansible: Access host/group vars from within custom module

查看:35
本文介绍了Ansible:从自定义模块中访问主机/组变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从自定义编写的模块中访问主机/组变量?我想避免将所有必需的变量作为模块参数传递.

Is there a way how one can access host/group vars from within a custom written module? I would like to avoid to pass all required vars as module parameters.

我的模块是用 Python 编写的,我使用了样板文件.我检查了几乎所有可用的变量,但它们没有存储在任何地方:

My module is written in Python and I use the boilerplate. I checked pretty much all available vars but they are not stored anywhere:

def main():
    pprint(dir())
    pprint(globals())
    pprint(locals())
    for name in vars().keys():
        print(name)

现在我唯一的希望是可以通过未记录的模块实用程序以某种方式访问​​它们.

Now my only hope is they are somehow accessible through the undocumented module utils.

我想这是不可能的,因为模块在目标机器上运行,并且可能事实/主机/组变量没有与模块一起传输......

I guess it is not possible, since the module runs on the target machine and probably the facts/host/group vars are not transferred along with the module...

找到模块工具现在看起来并不乐观.

Found the module utils now and it doesn't look promising.

推荐答案

我认为您在这里的想法几乎一针见血:

I think you pretty much hit the nail on the head with your thinking here:

我想这是不可能的,因为模块在目标机器上运行,并且可能事实/主机/组变量没有与模块一起传输......

I guess it is not possible, since the module runs on the target machine and probably the facts/host/group vars are not transferred along with the module...

但是,话虽如此,如果您真的需要这样做,那么可能会有一种稍微凌乱的方法.从 Ansible 1.8 开始,您可以设置 事实缓存,它使用 redis 来缓存事实在戏剧的运行之间.由于 redis 非常易于使用并且具有大多数流行编程语言的客户端,您可以让您的模块查询 redis 服务器对于您需要的任何事实.这并不是最干净的方法,但它可能会奏效.

However, having said that, if you really have a need for this then there might be a slightly messy way of doing it. As of Ansible 1.8 you can set up fact caching, which uses redis to cache facts between runs of plays. Since redis is pretty easy to use and has clients for most popular programming languages, you could have your module query the redis server for any facts you need. It's not exactly the cleanest way to do it, but it just might work.

这篇关于Ansible:从自定义模块中访问主机/组变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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