自定义Ansible回调未接收到group_vars/host_vars [英] Custom Ansible Callback not receiving group_vars/host_vars

查看:69
本文介绍了自定义Ansible回调未接收到group_vars/host_vars的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个自定义的ansible回调:

I have a custom ansible callback that I am writing:

class CallbackModule(CallbackBase):
  CALLBACK_VERSION = 2.0
  CALLBACK_TYPE = 'aggregate'
  CALLBACK_NAME = 'my_callback'

  def v2_runner_on_ok(self, result):
    print("v2_runner_on_ok")
    import pdb; pdb.set_trace()
    result._host.groups[0].get_vars()

当我检查 result._host.groups [0] .get_vars()的内容时,它将返回 {} ,这不是我期望的.我已验证主机正在接收group_vars(通过在我的剧本中放置 debug 任务并打印出预期的var).所以我的问题是,为什么回调不接收主机的group_vars?

When I inspect the contents of result._host.groups[0].get_vars(), it is returning {}, which is not what I expect. I have verified that the host is receiving group_vars (by placing a debug task in my playbook and printing out the expected var). So my question is, why does the callback not receive the group_vars for the host?

同样奇怪的是,host_vars也没有转换为我的回调插件.看来,滴入我插件的唯一变量是清单文件中定义的变量,还有一些魔术变量.

On another equally strange note, the host_vars are not commuted to my callback plugin either. It would seem that the only vars that trickle into my plugin are the ones defined in the inventory file, and some magic vars.

推荐答案

您正在访问 ansible.inventory 类下的变量,因此只能获得清单中定义的变量.

You are accessing variables under ansible.inventory class hence you get only those defined in the inventory.

如果要访问其他变量,则需要通过剧本的变量管理器进行操作:

If you want to access other variables, you need to go through the play's variable manager:

def v2_playbook_on_play_start(self, play):
    variable_manager = play.get_variable_manager()
    hostvars = variable_manager.get_vars()['hostvars']

这篇关于自定义Ansible回调未接收到group_vars/host_vars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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