Ansible hostvars没有属性'ansible_nodename'

时间:2018-01-17 09:24:45

标签: ansible

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

'dict object' has no attribute 'ansible_nodename` 

我打印了hostvars的内容,但没有看到此属性。

我找不到hostvars的文档,以便能够看到我们可以安全使用的内容ansible_nodename。所以,问题是:

  • 我可以安全地将ansible_nodename替换为ansible_host吗?
  • 我在哪里可以找到hostvars的内容及其创建算法的描述?

1 个答案:

答案 0 :(得分:3)

  

但有时却没有,给出以下错误......

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

您可能希望添加空白游戏以收集游戏手册顶部所有主机的事实:

- 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