Ansible 主机变量没有属性“ansible_nodename" [英] Ansible hostvars has no attribute 'ansible_nodename'

查看:114
本文介绍了Ansible 主机变量没有属性“ansible_nodename"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有几个使用 hostvars[item]['ansible_nodename'] 的 playbook,其中 item 是主机别名.它通常有效.但有时不,给出以下错误:

We have several playbooks that use hostvars[item]['ansible_nodename'] where item is a host alias. It usually works. But sometimes does not, giving the following error:

'dict object' has no attribute 'ansible_nodename` 

我打印了 hostvars 的内容,但在那里没有看到这个属性.

I printed the contents of hostvars and did not see this attribute there.

我找不到 hostvars 的文档来查看我们可以安全地使用什么来代替 ansible_nodename.所以,问题是:

I could not find the documentation of hostvars to be able to see what we can safely use instead ansible_nodename. So, the questions are:

  • 我可以用 ansible_host 安全地替换 ansible_nodename 吗?
  • 在哪里可以找到 hostvars 的内容及其创建算法的描述?
  • Can I safely replace ansible_nodename with ansible_host?
  • Where can I find a description of the contents of hostvars and algorithm of its creation?

推荐答案

但有时不,给出以下错误...

But sometimes does not, giving the following error...

这可能是因为没有及时收集这些主机的事实.

This can be because facts for those hosts are not gathered at that moment in time.

您可能希望在剧本的最顶部添加空剧本以收集所有主持人的事实:

You may want to add empty play to gather facts for all hosts at the very top of your playbook:

- name: gather facts
  hosts: all

- name: balancer configuration
  hosts: balancer
  tasks:
    - name: generate configs
      template:
        src: conf.j2
        dest: "/conf/{{ hostvars[item]['ansible_nodename'] }}.conf"
      with_hostnames: nodes

如果没有收集事实",平衡器的配置任务将失败,因为没有收集ansible_nodename.

Without 'gather facts' play, configuration task for balancer will fail because ansible_nodename is not gathered.

这篇关于Ansible 主机变量没有属性“ansible_nodename"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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