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

查看:30
本文介绍了自定义 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.

如果要访问其他变量,需要通过play的变量管理器:

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天全站免登陆